Пример #1
0
 public void TestMBMessage()
 {
     byte[] bs = ValueHelper.StrToToHexByte("00 86 00 00 00 06 01 01 01 01 00 02");
     MBMessage mb = new MBMessage(bs);
     Assert.AreEqual(0x86, mb.TID);
     Assert.AreEqual(0, mb.PID);
     Assert.AreEqual(6, mb.Length);
     Assert.AreEqual(1, mb.UID);
     Assert.AreEqual(1, mb.FC);
     Assert.AreEqual(0x101, mb.GetWord(0));
     Assert.AreEqual(0x2, mb.GetByte(3));
     Assert.AreEqual(0x2, mb.GetWord(2));
 }
Пример #2
0
 private MBMessage SendRequest(MBMessage req)
 {
     try
     {
         SocketError err;
         byte[] buff = req.encode();
         //log.DebugFormat("> {0:##}", ValueHelper.BytesToHexStr(buff));
         byte[] ack = _client.SSend(req.encode(), out err);
         //log.DebugFormat("< {0:##}, err={1}", ValueHelper.BytesToHexStr(ack), err);
         if (SocketError.Success != err)
         {
             log.ErrorFormat("异常: {0}", err);
         }
         MBMessage resp = new MBMessage(ack);
         if (resp.FC > 0x80)
         {
             byte ec = resp.GetByte(0);
             log.ErrorFormat("Modbus 异常: {0}-{1} ", ec, MBException.NameOf(ec));
         }
         return resp;
     }
     catch (Exception)
     {
         return null;
     }
 }
Пример #3
0
 /* WriteCoil
 REQ:
 Byte 0:FC = 05
 Byte 1-2: Reference number
 Byte 3:= FF to turn coil ON, =00 to turn coil OFF
 Byte 4:= 00
 Resp:
 Byte 1-2:Reference number
 Byte 3:= FF to turn coil ON, =00 to turn coil OFF (echoed)
 Byte 4:= 00"
 */
 private MBMessage AckWriteCoil(MBMessage req)
 {
     MBMessage resp = new MBMessage(req); // copy header
     ushort refNum = req.GetWord(0); //
     byte val = req.GetByte(2);
     byte result = _cms.MB_WriteCoil(refNum, val); // exception
     if (MBException.MB_SUCCESS == result)
     {
         // OK, return resp
     }
     else
     {
         // ERROR
         resp.FC = 0x85;
         resp.SetBody(new byte[] { result });
     }
     return resp;
 }