golang:ioutil.ReadAll()的网络响应为空,连接被对等方重置。

huangapple go评论194阅读模式
英文:

golang : network response from ioutil.ReadAll() is empty, connection reset by peer

问题

我正在尝试测试从设备模拟器中执行简单的TCP MODBUS读取单个寄存器的操作。运行代码时,显示的响应是0字节,并出现"connection reset by peer"的消息。有任何关于为什么它不起作用的想法吗?

更新,我的请求是不正确的,正确的工作MODBUS TCP轮询代码如下:

  1. package main
  2. import (
  3. "fmt"
  4. "net"
  5. )
  6. // TCP MODBUS client
  7. func main() {
  8. conn, err := net.Dial("tcp", "192.168.98.114:502")
  9. if err != nil {
  10. fmt.Println(err)
  11. }
  12. numRegs := 1
  13. // 发送MODBUS TCP请求(注意,格式与MODBUS串行不同)
  14. request := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x01, 0x00, 0x01}
  15. n, err := conn.Write(request)
  16. if err != nil {
  17. fmt.Println(err)
  18. }
  19. expectedResponseLen := 5 + 2 * numRegs
  20. response := make([]byte, expectedResponseLen)
  21. n, err = conn.Read(response)
  22. conn.Close()
  23. if err != nil {
  24. fmt.Println(err)
  25. }
  26. for i := 0; i < n; i++ {
  27. fmt.Printf("%02x ", response[i])
  28. }
  29. fmt.Println("\n")
  30. }

希望对你有帮助!

英文:

I'm trying to test out performing a simple TCP MODBUS read of a single register from a device emulator. When running the code it is showing a response of 0 bytes and I get the message "connection reset by peer". Any ideas as to why it's not working?

UPDATE, my request was incorrect, the correct working MODBUS TCP poll code is:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;net&quot;
  5. )
  6. // TCP MODBUS client
  7. func main() {
  8. conn, err := net.Dial(&quot;tcp&quot;, &quot;192.168.98.114:502&quot;)
  9. if err != nil {
  10. fmt.Println(err)
  11. }
  12. numRegs := 1
  13. # make a MODBUS TCP request (be careful, the format is different to MODBUS serial)
  14. request := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x01, 0x03, 0x00, 0x01, 0x00, 0x01}
  15. n, err := conn.Write(request)
  16. if err != nil {
  17. fmt.Println(err)
  18. }
  19. expectedResponseLen := 5 + 2 * numRegs
  20. response := make([]byte, expectedResponseLen)
  21. n, err = conn.Read(response)
  22. conn.Close()
  23. if err != nil {
  24. fmt.Println(err)
  25. }
  26. for i := 0; i &lt; n; i++ {
  27. fmt.Printf(&quot;%02x &quot;, response[i])
  28. }
  29. fmt.Println(&quot;\n&quot;)
  30. }

答案1

得分: 0

最初我认为fmt.Fprintf可能会在发送请求数据时进行更改,但是这个示例似乎工作正常。

然而,我仍然建议使用较低级别的Write/Read而不是fmt.Fprintf/ioutil.ReadAll

  1. req := []byte { 0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0xd5, 0xca }
  2. n, err := conn.Write(req)
  3. if err != nil {
  4. fmt.Println("写入错误:", err)
  5. return
  6. }
  7. fmt.Printf("已写入 %d 字节的请求:%#v", n, req)
  8. rsp := make([]byte, 64)
  9. n, err = conn.Read(rsp)
  10. fmt.Printf("接收到 %d 字节的响应:%#v", n, rsp[:n])
  11. if err != nil {
  12. fmt.Println("读取错误:", err)
  13. }
英文:

Originally I thought that fmt.Fprintf may be changing the request data on the way out, but this example seems to work OK.

However, I would still recommend using the lower-level Write/Read instead of fmt.Fprintf/ioutil.ReadAll:

  1. req := []byte { 0x01, 0x03, 0x00, 0x01, 0x00, 0x01, 0xd5, 0xca }
  2. n, err := conn.Write(req)
  3. if err != nil {
  4. fmt.Println(&quot;write error:&quot;, err)
  5. return
  6. }
  7. fmt.Printf(&quot;wrote %d bytes for request: %#v&quot;, n, req)
  8. rsp := make([]byte, 64)
  9. n, err = conn.Read(rsp)
  10. fmt.Printf(&quot;received %d bytes in response: %#v&quot;, n, rsp[:n])
  11. if err != nil {
  12. fmt.Println(&quot;read error:&quot;, err)
  13. }

huangapple
  • 本文由 发表于 2013年11月20日 11:01:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/20086504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定