필터 지우기
필터 지우기

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 CenterFile Exchange에서 Serial 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