Using MATLAB to send multiple serial signals through the same port

1.4k views Asked by At

I'd like to send multiple signals (4 inputs and outputs and 7 outputs) from my Laptop to a microcontroller. I'm thinking of using a USB to serial converter and multiplexing the data through the port. I'll need to write codes both in the laptop end and in the microcontroller to multiplex the data.

Eg: Tx of microcontroller: 1.Temperature sensor ADC output->Laptop 2.Voltage sensor to laptop 3.Current Sensor to Laptop 4.Photodiode current to Laptop

So I need to write a program in the microcontroller to send the data in this order. How can I accomplish this? I was thinking of an infinite loop which sends the data with time delays in between.

At the Rx pin of Microcontroller,

Seven bit sequences. Each bit sequence will be used to set the duty cycle of a PWM generated by the microcontroller.

I also need the same multiplexing or demultiplexing arrangement in the matlab end. Here too, I'm thinking of allotting some virtual 'channels' at different instants of time. What kind of algorithm would I need?

1

There are 1 answers

0
Bas Swinckels On BEST ANSWER

In case you always send all the inputs/outputs at the same rate, you could simply pack them into 'packets', which always start with one or more bytes with a fixed value that form a 'packet header'. The only risk is that one of the bytes of the sensor data might have the same value as the start-byte at the moment you try to start receiving bytes and you are not yet synchronized. You can reduce this risk by making the header longer, or by choosing a start-byte that is illegal output for the sensors (typically OxFF or so).

The sending loop on the microcontroller is really easy (pseudocode):

while True:
    measure_sensors()
    serial.send(START_BYTE)
    serial.send(temperature)
    serial.send(voltage)
    serial.send(current)
    serial.send(photodiode)
end while

The receiving loop is a bit more tricky, since it needs to synchronize first:

while True:
    data = serial.receive()
    if data != START_BYTE:
        print 'not synced'
        continue #restart at top of while
    end if
    temperature = serial.receive()
    voltage = serial.receive()
    current = serial.receive()
    photodiode = serial.receive()
    do_stuff_with_measurements()
end while

This same scheme can be used for communication in both directions.