How do I obtain binary (in 0's and 1's) data from the serial port?

I want to plot the bit data signal from the serial port. I use the command fread as Matlab say that fread is the funtion to read binary data. But when I use fread, I got numbers like 10,41,83,etc instead of 0 and 1. I want to view the signal as 0 and 1 so the plot is like squarewave. What am I supposed to do?
Here is my code:
x=0:0.01:10;
y=fread(s);
cla;drawnow;
h=plot(handles.axes1,x(1),y(1));grid on; hold on; legend data;
for idx = 1 : length(x);
set(h,'xdata',x(1:idx),'ydata',y(1:idx));drawnow; end;
% s is the serial.

2 Comments

how about using dec2bin to convert to binary?
I've tried with dec2bin.
But the result is
[11001010]
instead of
[1 1 0 0 1 0 1 0]
so I can't plot it. How can I make the result be
[1 1 0 0 1 0 1 0] ?
Is there a function to break the cell array apart?

Sign in to comment.

 Accepted Answer

fread() from serial port does not support reading bit by bit.
You can read as uint8 and use dec2bin() and plot that result.
Caution: the waveform actually transmitted over the serial line will not match the above plot.
  • if you are using RS422 or PS/2 instead of RS232, or if you are using one of the advanced forms of RS232, then the transmitted waveform will be differential rather than single-ended
  • RS232 has a mandatory "start bit" and mandates a "stop-bit" interval. The start bit does more or less resemble a bit waveform I seem to recall, but the stop-bit waveform is not a valid bit waveform, and is instead a protocol-violation condition held for between one and two bit-times
  • RS232 in single-ended mode (the more common form) defines bits in terms of ranges of negative and positive voltages, with "mark" and "space" conditions defined at the wire level, with logic 1 corresponding to a negative voltage, not a positive voltage
  • RS232 mandates that the LSB (least significant bit) of each byte be sent first.
  • RS232 defines a minimum and maximum byte length. If a stop bit interval is not seen within the maximum number of bits, a framing error condition exists for the byte and the serial port hardware will probably throw the byte away.
Thus, if the intent is to use fread() to plot the waveform that existed on the serial wires themselves, you will not be able to do so: for such a task, you would need to use an ADC (analog to digital converter) sampling at a sufficiently high rate and plot the discretized waveform that resulted.

10 Comments

The same with my comment for Mr Ashish Utama,
I've tried with dec2bin.
But the result is
[11001010]
instead of
[1 1 0 0 1 0 1 0]
so I can't plot it. How can I make the result be
[1 1 0 0 1 0 1 0] ?
Is there a function to break the cell array apart?
NumericData = dec2bin(YourSerialData) - '0';
Wow. It works. But there still one problem.
Say I have the result is
1 1 0 1 0 0 0
1 1 0 0 1 0 1
1 1 0 1 1 0 0
1 1 0 1 1 0 0
1 1 0 1 1 1 1
What will I do so it become one row?
Thank you very much Mr.Roberson...
bits = reshape( dec2bin(YourSerialData,8).' - '0', 1, []);
Thank you Mr.Roberson. It works again. But the plot is triangle waveform not square waveform.
How can I make so the point have the width so the plot become square waveform that represented the binary bit?
You could try plotting with stairs()
Great! Finally I can plot the binary data.
Thank you very much, Mr. Roberson. You're such a great help.
I have another problem..
here is my code
x=0:5:5000;
a='q';
bitk = reshape(dec2bin(double(a),8).' - '0', 1, []);
stairs(handles.axes1,x(1:length(bitk)),bitk(1:length(bitk)));
xlim(handles.axes1,[0 50]); ylim(handles.axes1,[-1 2]);
the result is the graph isn't a squarewaveform. but the plot stairs is cut at bit 8. how can I fix this problem?
After assigning to bitk, try
if bitk(end) ~= 0; bitk(end+1) = 0; end
I have the range for xlim [0 50], so the plot end when the bitk has no more value.
how the rest of plot can give the value 0.
I don't understand why but if I transmit the character via transceiver and read it with fscanf in receiver and plot it as the plot before, It work just fine. The plot can make the squarewaveform until the end of the plot with the range xlim given.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!