robinson / gos7

Implementation of Siemens S7 protocol in golang
BSD 3-Clause "New" or "Revised" License
314 stars 121 forks source link

Suggestion: ADD new func "NewTCPClientHandlerWithConnectType" to change connectionType when allocates a new TCPClientHandler #68

Closed Hongliukai closed 6 months ago

Hongliukai commented 6 months ago

I noticed that using NewTCPClientHandler(address string, rack int, slot int) to create a TCPClientHandler limits the connectType to 1 (AS PG_OR_PC)

func NewTCPClientHandler(address string, rack int, slot int) *TCPClientHandler {
    h := &TCPClientHandler{}
    h.Address = address
    h.Timeout = tcpTimeout
    h.IdleTimeout = tcpIdleTimeout
    h.ConnectionType = connectionTypePG // Connect to the PLC as a PG
    remoteTSAP := uint16(h.ConnectionType)<<8 + (uint16(rack) * 0x20) + uint16(slot)
    h.setConnectionParameters(address, 0x0100, remoteTSAP)
    return h
}

I often encountered when using gos7 to connect to Siemens PLCs that the PLC supports fewer PG connections and more OP connections, so I'd like to add a parameter to change this connect parameter.

// NewTCPClientHandlerWithConnectType allocates a new TCPClientHandler with connection type.
func NewTCPClientHandlerWithConnectType(address string, rack int, slot int, connectType int) *TCPClientHandler {
    h := &TCPClientHandler{}
    h.Address = address
    h.Timeout = tcpTimeout
    h.IdleTimeout = tcpIdleTimeout
    h.ConnectionType = connectType
    remoteTSAP := uint16(h.ConnectionType)<<8 + (uint16(rack) * 0x20) + uint16(slot)
    h.setConnectionParameters(address, 0x0100, remoteTSAP)
    return h
}
Hongliukai commented 6 months ago

67

robinson commented 6 months ago

thanks for the PR!