github.com/simpleiot/simpleiot@v0.18.3/modbus/tcp_test.go (about)

     1  package modbus
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestTCPEncodeDecode(t *testing.T) {
    10  	pdu := PDU{
    11  		FunctionCode: FuncCodeWriteMultipleCoils,
    12  		Data:         []byte{1, 2, 3},
    13  	}
    14  
    15  	tport := NewTCP(nil, 500*time.Millisecond, TransportClient)
    16  	data, err := tport.Encode(1, pdu)
    17  
    18  	if err != nil {
    19  		t.Fail()
    20  	}
    21  
    22  	_, pdu2, err := tport.Decode(data)
    23  
    24  	if err != nil {
    25  		t.Fail()
    26  	}
    27  
    28  	if pdu2.FunctionCode != pdu.FunctionCode {
    29  		t.Error("Function code not the same")
    30  	}
    31  
    32  	if !reflect.DeepEqual(pdu2.Data, pdu.Data) {
    33  		t.Error("Data compare failed")
    34  	}
    35  }