package modbus

import (
	"testing"

	"golib/v2/gnet"
)

func TestTCPRequest_Pack(t *testing.T) {
	r := TCPRequest{
		TransactionID: 1,
		ProtocolID:    2,
		UnitID:        3,
		FunctionCode:  4,
		StartNo:       5,
		RegisterLen:   6,
		Data:          []byte{0x10, 0x20},
	}
	b := r.Pack()
	t.Log(gnet.Bytes(b).HexTo())
	// 00 00 00 00 00 00 03 27 10 00 0b 00
	r.FunctionCode = FuncCode16
	r.Data = []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xa, 0x0b}
	b1 := r.Pack()
	t.Log(gnet.Bytes(b1).HexTo())
}

func TestTCPResponse_Unpack(t *testing.T) {
	b := gnet.String("00 01 00 00 00 0B 01 03 06 01 02 03 04 05 06").Hex()
	var resp TCPResponse
	if err := resp.Unpack(b); err != nil {
		t.Error(err)
		return
	}
	t.Logf("%+v\n", resp)
}
