tbrandon / mbserver

Golang Modbus Server (Slave)
MIT License
250 stars 135 forks source link

serial read only returns one byte #6

Open cbrake opened 4 years ago

cbrake commented 4 years ago

I have created a simple server and client apps:

https://github.com/simpleiot/simpleiot/blob/feature-modbus/cmd/modbus-client/main.go

https://github.com/simpleiot/simpleiot/blob/feature-modbus/cmd/modbus-server/main.go

When I send a packet to the server, it only ever receives one byte.

What causes the the Read() at this location:

https://github.com/tbrandon/mbserver/blob/master/servertu.go#L26

func (s *Server) acceptSerialRequests(port serial.Port) {
    for {
        buffer := make([]byte, 512)

        bytesRead, err := port.Read(buffer)
        if err != nil {
            if err != io.EOF {
                log.Printf("serial read error %v\n", err)
            }
            return
        }

        if bytesRead != 0 {

            // Set the length of the packet to the number of read bytes.
            packet := buffer[:bytesRead]

            frame, err := NewRTUFrame(packet)
            if err != nil {
                log.Printf("bad serial frame error %v\n", err)
                return
            }

            request := &Request{port, frame}

            s.requestChan <- request
        }
    }
}

To read an entire packet, instead of just several bytes?

Thanks, Cliff

cbrake commented 4 years ago

closing this as this project appears to not be maintained. I started a new modbus library that will implement both client and server functionality:

https://github.com/simpleiot/simpleiot/tree/master/modbus

Examples:

https://github.com/simpleiot/simpleiot/blob/master/modbus/rtu-end-to-end_test.go https://github.com/simpleiot/simpleiot/blob/master/cmd/modbus-client/main.go https://github.com/simpleiot/simpleiot/blob/master/cmd/modbus-server/main.go