フィルターのクリア

Lost characters while sending text through serial port

11 ビュー (過去 30 日間)
Xiang LI
Xiang LI 2023 年 1 月 28 日
コメント済み: Walter Roberson 2023 年 4 月 18 日
I am trying to use the serialport to send commands to a device.
The configurations are
serialport("COM4",115200)
Terminator={"CR","CR"}
And I am sending the commands using writeread() function.
Most times it works properly. However, sometimes some characters are lost while sending.
It occurs totally randomly. I can't tell when it happens or which character will lost.
Any ideas?
  3 件のコメント
chrisw23
chrisw23 2023 年 1 月 31 日
This happens when the input buffer of the receiving device is full. In most of your tests the receiver is fast enough to read and clear the buffer, but if not ...characters/bytes get lost.
Benjamin Thompson
Benjamin Thompson 2023 年 1 月 31 日
Also make sure software handshaking is not enabled on either end, as the handshaking characters will be stripped out. If this is over a physical cable, make sure the cable length is within spec for the 115200 bits per second data rate. RS422 allows longer length than RS232.

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

回答 (1 件)

Anshuman
Anshuman 2023 年 4 月 18 日
Following could be the reasons of characters being lost:
  1. The serialport function has a default timeout of 10 seconds, which may not be enough in some cases. You can try to adjust the timeout value by setting the Timeout property of the serialport object.
  2. Make sure that the flow control settings and Terminator settings on the device and in MATLAB match, which you can set using FlowControl property and Terminator property of the serialport object respectively.
  3. Instead of sending large amount of data at once, try sending smaller chunks using the write method instead of writeRead.
  4. Try using a different baud rate which sometimes may improve the reliability of data transmission.
Hope it helps!
  1 件のコメント
Walter Roberson
Walter Roberson 2023 年 4 月 18 日
However, flow control is not used for Serial over USB, or only used in emulation
For USB over serial, the most efficient data packet size is (if I recall correctly) either 1021 bytes or 1022 bytes, depending on configuration. If a device cannot accept a packet that size, then it should be responding as not ready when the host controller polls it. This is at the device driver level, not at the host program level.
Baud rate does not affect Serial over USB, except possibly affecting how an endpoint program might choose its buffering strategy. The clockrate used for transfers over USB is negotiated at the time the device synchronizes with the host controller shortly after the device is plugged in. The host controller might potentially degrade the transfer rate according to the capabilities of the slowest device currently plugged in to the USB controller channel. Any emulated serial baud rate is not at all taken into consideration

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

カテゴリ

Help Center および File ExchangeSerial and USB Communication についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by