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

     1  package modbus
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/simpleiot/simpleiot/test"
     9  )
    10  
    11  // examples from MPE SC2000
    12  // Reading the Wet Well Level in Modbus register 40011, with slave address of 1, Function code 03.
    13  //ModScan32 Request: [001] [003] [000] [010] [000] [001] [164] [008]
    14  //SC2000 Response: [001] [003] [002] [000] [098] [057] [173]   (9.8ft)
    15  //SC2000 Response: [001] [003] [002] [000] [090] [056] [127]   (9.0ft)
    16  //
    17  //Reading the High Level Alarm in Modbus coil 129, with slave address of 1, Function code 01,  .
    18  // ModScan32 Request: [001] [001] [000] [128] [000] [001] [252] [034]
    19  // SC2000 Response: [001] [001] [001] [000] [081] [136] (no high level alarm)
    20  // SC2000 Response: [001] [001] [001] [001] [144] [072] (high level alarm)
    21  
    22  var rtuSc2000LevelPrompt = []byte{1, 3, 0, 10, 0, 1, 164, 8}
    23  var rtuSc2000LevelResp = []byte{1, 3, 2, 0, 98, 57, 173}
    24  
    25  var rtuSc2000CoilPrompt = []byte{1, 1, 0, 128, 0, 1, 252, 34}
    26  var rtuSc2000CoilResp = []byte{1, 1, 1, 1, 144, 72}
    27  
    28  func TestRtuSc2000Level(t *testing.T) {
    29  	err := CheckRtuCrc(rtuSc2000LevelPrompt)
    30  	if err != nil {
    31  		t.Fatal("rtuSc2000LevelPrompt CRC check failed")
    32  	}
    33  
    34  	rtu := NewRTU(nil)
    35  
    36  	prompt := ReadHoldingRegs(10, 1)
    37  	promptRtu, err := rtu.Encode(1, prompt)
    38  
    39  	if err != nil {
    40  		t.Fatal("error encoding")
    41  	}
    42  
    43  	if !reflect.DeepEqual(rtuSc2000LevelPrompt, promptRtu) {
    44  		t.Fatal("encoded packet is not as expected")
    45  	}
    46  
    47  	regs := Regs{}
    48  	regs.AddReg(10, 1)
    49  	_ = regs.WriteReg(10, 98)
    50  
    51  	_, resp, err := prompt.ProcessRequest(&regs)
    52  	if err != nil {
    53  		t.Fatal("error processing: ", err)
    54  	}
    55  
    56  	respRtu, err := rtu.Encode(1, resp)
    57  	if err != nil {
    58  		t.Fatal("resp encode error: ", err)
    59  	}
    60  
    61  	if !reflect.DeepEqual(rtuSc2000LevelResp, respRtu) {
    62  		fmt.Println("Expected: ", test.HexDump(rtuSc2000LevelResp))
    63  		fmt.Println("Got:      ", test.HexDump(respRtu))
    64  		fmt.Println("resp packet is not right")
    65  	}
    66  
    67  }
    68  
    69  func TestRtuSc2000Coil(t *testing.T) {
    70  	err := CheckRtuCrc(rtuSc2000CoilPrompt)
    71  	if err != nil {
    72  		t.Error("rtuSc2000CoilPrompt CRC check failed")
    73  	}
    74  
    75  	rtu := NewRTU(nil)
    76  
    77  	prompt := ReadCoils(128, 1)
    78  	promptRtu, err := rtu.Encode(1, prompt)
    79  
    80  	if err != nil {
    81  		t.Error("Error encoding packet: ", err)
    82  	}
    83  
    84  	if !reflect.DeepEqual(rtuSc2000CoilPrompt, promptRtu) {
    85  		t.Error("packet encoding error")
    86  	}
    87  
    88  	regs := Regs{}
    89  	regs.AddCoil(128)
    90  	_ = regs.WriteCoil(128, true)
    91  
    92  	_, resp, err := prompt.ProcessRequest(&regs)
    93  	if err != nil {
    94  		t.Fatal("error processing: ", err)
    95  	}
    96  
    97  	respRtu, err := rtu.Encode(1, resp)
    98  	if err != nil {
    99  		t.Fatal("resp encode error: ", err)
   100  	}
   101  
   102  	if !reflect.DeepEqual(rtuSc2000CoilResp, respRtu) {
   103  		fmt.Println("Expected: ", test.HexDump(rtuSc2000CoilResp))
   104  		fmt.Println("Got:      ", test.HexDump(respRtu))
   105  		fmt.Println("resp packet is not right")
   106  	}
   107  }