MATLAB Answers


sending binary data using Serial port

Asked by Swapnil Marathe on 25 Oct 2011

i will tell u my purpose. i want to glow led's(light emitting diodes) using directly my serial port using matlab. so for that i need to send ' 0 ' or ' 1 ' the way i want through my serial port.(i will manage the voltage level manipulations at the led end) can i send a single bit through serial port. 8-12 bit data is also fine with me but in binary format.?



No products are associated with this question.

1 Answer

Answer by Walter Roberson
on 26 Oct 2011

No, you cannot.

The "serial ports" supported by MATLAB (under that name, anyhow) are all asynchronous pulsed-bit ports, in which there is a change of voltage state at the start of the bit and a change of voltage state at the end of the bit, with the voltage state change being interpreted by the receiver. The ports also must change state to indicate a "start bit", and then must change state to indicate at least 5 data bits (with minimums of 7 far more common these days), and then must change state to represent a "stop bit".

I was careful above to not write in terms of the voltage going high or low: having a single transmit pin whose state goes high or low is the most common use of serial ports for this purpose, but MATLAB also supports a form of serial port in which there is instead two transmit voltage pins and the bit state is encoded by the voltage difference between the two (this form of differential signal representation has desirable noise reduction properties, especially at very high speeds.)

If you were operating in single-pin transmit mode (standard RS-232) then about the best you could do would be to have your hardware read and latch an 8 bit byte of data, and ignore 7 of the bits, with the remaining bit indicating what the new state of the LED should be.

There are alternatives that do not involve an receiver, but they would have to involve timing to ensure that sufficient quiet time had been seen, then to determine that the bit received matched a start bit, then to latch and act upon the meaning of the next bit, and then to go back to the "ignore" timer to wait through the rest of the byte. Caution: if you do this and study the voltage diagrams carefully, you will find that a data logic "1" bit corresponds to a negative voltage, and a data logic "0" bit corresponds to a positive voltage, but that those logic levels are not the same as are used for the "start" and "stop" bits! (yes, the diagrams are enough to give me a headache.) Adding a standard UART chip is a lot less of a headache than working through all the hardware yourself. Just make sure that your UART isn't expecting TTL levels when the serial port is transmitting RS-232 levels...

A hint: you might find it considerably easier to change your approach a fair bit and instead output values to your sound-card LINE OUT port -- the output of it is already analog!


Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!