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

Serial only receive 1 byte #84

Open
andarm opened this issue Jul 26, 2018 · 2 comments
Open

Serial only receive 1 byte #84

andarm opened this issue Jul 26, 2018 · 2 comments

Comments

@andarm
Copy link

andarm commented Jul 26, 2018

run on window

  • used modusslave-tool test .
	sendbuf := []byte{0x40, 0x02, 0x00, 0x00, 0x00, 0x6E, 0xF6, 0xF7}
		n, err := s.Write(sendbuf)
	if err != nil {
		s.Close()
		fmt.Println("write close")
		log.Fatal(err)
	}

	buf := make([]byte, 128)

	n, err = s.Read(buf)
	if err != nil {
		fmt.Println("read close")
		log.Fatal(err)
	}
  • receive len n=1 byte . // Normal receive is : 40 02 0E 00 00 00 00 00 00 00 00 00 00 00 00 00 00 4E C1 ,but I only got one byte.
  • serial config
  	c := &serial.Config{Name: "COM5", Baud: 9600, ReadTimeout: time.Second * 5}

@xiegeo
Copy link

xiegeo commented Jul 27, 2018

You need to put read in a loop. Use the third byte to calculate total length.

If you need something that also takes timing into consideration and handles more opcodes, you can check out my code here https://github.com/xiegeo/modbusone/blob/master/packet_reader.go

@andarm
Copy link
Author

andarm commented Jul 28, 2018

Thanks ,I got it.

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

2 participants