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

3 visualizzazioni (ultimi 30 giorni)
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 Commenti
Adi Mico
Adi Mico il 12 Lug 2011
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?

Accedi per commentare.

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Specifying Target for Graphics Output in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by