Lost characters while sending text through serial port
4 次查看(过去 30 天)
显示 更早的评论
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
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
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
2023-4-18
Following could be the reasons of characters being lost:
- 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.
- 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.
- Instead of sending large amount of data at once, try sending smaller chunks using the write method instead of writeRead.
- Try using a different baud rate which sometimes may improve the reliability of data transmission.
Hope it helps!
1 个评论
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!