フィルターのクリア

How exactly Matlab sent bits in a serial communication?

2 ビュー (過去 30 日間)
Pablo Medina
Pablo Medina 2016 年 12 月 17 日
コメント済み: Walter Roberson 2016 年 12 月 19 日
I am using serial communication in orden to send an specific binary secuence. For example: DataBits = [0 0 0 0 0 0 0 1], which represent the number 1 (decimal). But Matlab send data to the channel like this [0(StartBit) (0 0 0 0 0 0 1 0) DataBits 1(StopBit)]. I realized this using an Oscilloscope.
Commands Used:
s = serial('COM3','BaudRate',9600,'DataBits',8,'StopBits',1,'InputBufferSize',500);
fopen(s)
fwrite(s,1,'async')
So, Matlab is adding a 0 at the 8th position of the DataBits [0 0 0 0 0 0 1 0] = 2 (decimal). My questions is if there is a way to send exactly a binary sequence using fwrite() function.
Thanks for future answers !

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 12 月 18 日
You should fwrite uint8(1) to avoid questions about what happens when you fwrite double values.
If your oscilloscope showed you the order you indicated and you were using an rs232 port then one of three things was happening:
  • you were not writing the value you thought you were; or
  • your oscilloscope is wrong; or
  • you have a hardware error in your rs232
I say this because rs232 requires that the least significant bit be sent first, so the 1 bit would have had to be sent immediately after the start bit.
  3 件のコメント
Walter Roberson
Walter Roberson 2016 年 12 月 19 日
The default for fwrite() is to uint8() the values before writing, so fwrite(s,1) and fwrite(s,uint8(1)) are the same, but the explicit cast makes it easier for the reader to be sure they know what is happening.
If you want to send something outside the range 0 to 255, you should use the precision argument, and the byte order argument is a good idea too, such as fwrite(s, value, 'int16', 'ieee-be')
Walter Roberson
Walter Roberson 2016 年 12 月 19 日
Correction, the byte order cannot be specified for serial.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by