Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为什串口有数据时,读取的数据总是将一帧发送数据的第一字节和剩余的字节数,分2次才能完全的读到? #132

Open
ruixule opened this issue Jun 11, 2024 · 1 comment

Comments

@ruixule
Copy link

ruixule commented Jun 11, 2024

No description provided.

@ruixule
Copy link
Author

ruixule commented Jun 11, 2024

image
image
c := &serial.Config{
Name: "COM3",
Baud: 9600,
Parity: 0,
StopBits: 1,
ReadTimeout: 100 * time.Millisecond,
}
port, err := serial.OpenPort(c)

if err != nil {
	u.Lg.Error("Failed to open port:", err)
	return
}
defer port.Close()

var receivedData []byte

for {
	data := make([]byte, bufferSize) // 创建一个固定大小的缓冲区
	n, err := port.Read(data)
	if err != nil {
		u.Lg.Error(err)
		errCount++
		if errCount > maxErrCount {
			u.Lg.Error("errCount > maxErrCount")
			return
		}
	} else {
		errCount = 0
		if n > 0 {
			//receivedData = append(receivedData, data[:n]...)
			u.Lg.Infof("Received data so far: %v", data[:n])
			if n > processBytes {
				processSerialData(receivedData)
				receivedData = nil // 重置接收的数据
			}
		}
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant