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

조회 수: 3 (최근 30일)
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
Ashish Uthama
Ashish Uthama 2011년 7월 11일
how about using dec2bin to convert to binary?
Adi Mico
Adi Mico 2011년 7월 12일
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?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2011년 7월 11일
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
Walter Roberson
Walter Roberson 2011년 7월 27일
After assigning to bitk, try
if bitk(end) ~= 0; bitk(end+1) = 0; end
Adi Mico
Adi Mico 2011년 7월 27일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by