How do I obtain binary (in 0's and 1's) data from the serial port?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Walter Roberson
le 11 Juil 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Waveform Generation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!