Lost characters while sending text through serial port

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 Comments

Is this a true serial port, or is it Serial Over USB ? If it is Serial Over USB is the destination internally converting the USB stream into something it is stuffing into an RS232 or TTL port (so the end code believes it is reading from RS232 or TTL but there happens to be a media convertor in the middle), or is the endpoint making the data available more directly to the destination (for example DMA or through device registers that have access to the memory) ?
If I recall correctly, some of the arduino series have implemented Serial Over USB effectively as a media convertor, with the bytes being pushed into an RS232 or TTL device; I think I have encounted the occasional OEM-type board (System on a Chip or DSP devices or programmable HDL type devices) that used the same kind of technique to provide the USB interface.
Any case in which the data has to pass through a true RS232 or true TTL serial device, risks data overrun if run at a higher baud rate than can be reliably processed. The cases in which there is a more direct connection between the USB controller and the remote CPU are more reliable for Serial Over USB.
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.
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.

Sign in to comment.

Answers (1)

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 Comment

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

Sign in to comment.

Products

Release

R2021b

Asked:

on 28 Jan 2023

Commented:

on 18 Apr 2023

Community Treasure Hunt

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

Start Hunting!