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

     1  package modbus
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"reflect"
     7  	"testing"
     8  )
     9  
    10  // the below data is Modbus ASCII
    11  var testData1 = []byte{
    12  	// request, adr 3, reading holding reg: 3, count: 6
    13  	0x3A,
    14  	0x30,
    15  	0x33,
    16  	0x30,
    17  	0x33,
    18  	0x30,
    19  	0x30,
    20  	0x30,
    21  	0x30,
    22  	0x30,
    23  	0x30,
    24  	0x30,
    25  	0x36,
    26  	0x46,
    27  	0x34,
    28  	0x0D,
    29  	0x0A,
    30  }
    31  
    32  var testData2 = []byte{
    33  	// response, adr 3, reading holding reg: 3, 12 bytes of data
    34  	0x3A,
    35  	0x30,
    36  	0x33,
    37  	0x30,
    38  	0x33,
    39  	0x30,
    40  	0x43,
    41  	0x30,
    42  	0x31,
    43  	0x30,
    44  	0x36,
    45  	0x30,
    46  	0x30,
    47  	0x30,
    48  	0x30,
    49  	0x30,
    50  	0x31,
    51  	0x35,
    52  	0x44,
    53  	0x30,
    54  	0x30,
    55  	0x36,
    56  	0x33,
    57  	0x30,
    58  	0x33,
    59  	0x31,
    60  	0x41,
    61  	0x32,
    62  	0x30,
    63  	0x30,
    64  	0x30,
    65  	0x45,
    66  	0x39,
    67  	0x0D,
    68  	0x0A,
    69  }
    70  
    71  var testData3 = []byte{
    72  	// broadcast (adr 0), Write Multiple registers (0x10), address 32768, count 6, byte count 12, data
    73  	0x3A,
    74  	0x30,
    75  	0x30,
    76  	0x31,
    77  	0x30,
    78  	0x38,
    79  	0x30,
    80  	0x30,
    81  	0x30,
    82  	0x30,
    83  	0x30,
    84  	0x30,
    85  	0x36,
    86  	0x30,
    87  	0x43,
    88  	0x30,
    89  	0x30,
    90  	0x32,
    91  	0x32,
    92  	0x30,
    93  	0x30,
    94  	0x36,
    95  	0x33,
    96  	0x30,
    97  	0x32,
    98  	0x30,
    99  	0x30,
   100  	0x32,
   101  	0x31,
   102  	0x33,
   103  	0x34,
   104  	0x30,
   105  	0x30,
   106  	0x30,
   107  	0x35,
   108  	0x30,
   109  	0x30,
   110  	0x30,
   111  	0x30,
   112  	0x37,
   113  	0x44,
   114  	0x0D,
   115  	0x0A,
   116  }
   117  
   118  var testData = append(testData1, append(testData2, testData3...)...)
   119  
   120  func TestScanner(t *testing.T) {
   121  	buf := bytes.NewBuffer(testData)
   122  	m := NewModbus(buf)
   123  
   124  	count := 0
   125  
   126  	for {
   127  		data, err := m.Read()
   128  		if err != nil {
   129  			fmt.Println(err)
   130  			break
   131  		}
   132  
   133  		fmt.Println(string(data))
   134  		count++
   135  	}
   136  
   137  	if count != 3 {
   138  		t.Error("Expected 3 frames")
   139  	}
   140  }
   141  
   142  func TestDecodeASCIIByte(t *testing.T) {
   143  	data := []byte("f423")
   144  
   145  	dec, data, err := DecodeASCIIByte(data)
   146  	if dec != 0xf4 {
   147  		t.Errorf("dec error, got 0x%x\n", dec)
   148  	}
   149  
   150  	if string(data) != "23" {
   151  		t.Errorf("returned data is %v\n", string(data))
   152  	}
   153  
   154  	if err != nil {
   155  		t.Error(err)
   156  	}
   157  }
   158  
   159  func TestASCIIDecode1(t *testing.T) {
   160  	pdu, err := DecodeASCIIPDU(testData1)
   161  	if err != nil {
   162  		t.Error("error decoding: ", err)
   163  	}
   164  
   165  	if pdu.Address != 3 {
   166  		t.Error("Error, wrong address, expected 3 got: ", pdu.Address)
   167  	}
   168  
   169  	if pdu.FunctionCode != FuncCodeReadHoldingRegisters {
   170  		t.Error("Error reading function code, expected 3, got: ", pdu.FunctionCode)
   171  	}
   172  }
   173  
   174  func TestASCIIDecode3(t *testing.T) {
   175  	pdu, err := DecodeASCIIPDU(testData3)
   176  	if err != nil {
   177  		t.Error("error decoding: ", err)
   178  	}
   179  
   180  	if pdu.Address != 0 {
   181  		t.Error("Error, wrong address, expected 0 got: ", pdu.Address)
   182  	}
   183  
   184  	if pdu.FunctionCode != FuncCodeWriteMultipleRegisters {
   185  		t.Error("Error reading function code, got: ", pdu.FunctionCode)
   186  	}
   187  
   188  	fd, err := pdu.DecodeFunctionData()
   189  
   190  	if err != nil {
   191  		t.Error(err)
   192  	}
   193  
   194  	wmr := fd.(FuncWriteMultipleRegisterRequest)
   195  
   196  	if wmr.StartingAddress != 32768 {
   197  		t.Error("Wrong starting address: ", wmr.StartingAddress)
   198  	}
   199  
   200  	if wmr.RegCount != 6 {
   201  		t.Error("Wrong reg count: ", wmr.RegCount)
   202  	}
   203  
   204  	if wmr.ByteCount != 12 {
   205  		t.Error("Wrong byte count: ", wmr.ByteCount)
   206  	}
   207  
   208  	expectedRegData := []uint16{
   209  		0x22,
   210  		0x63,
   211  		0x200,
   212  		0x2134,
   213  		0x5,
   214  		0x0,
   215  	}
   216  
   217  	if !reflect.DeepEqual(expectedRegData, wmr.RegValues) {
   218  		for i, r := range wmr.RegValues {
   219  			fmt.Printf("exp: 0x%x, got: 0x%x\n", expectedRegData[i], r)
   220  		}
   221  		t.Error("reg values are not correct")
   222  	}
   223  
   224  }