Beispiel #1
0
        private void SocketReceiveMsg()
        {
            byte[] buffer = new byte[1024 * 1024];
            int    n;
            String s;

            while (true)
            {
                if (isTerminating)
                {
                    break;
                }
                if (mySocket == null)
                {
                    break;
                }
                try
                {
                    n = mySocket.Receive(buffer);
                    s = StringByteHelper.BytesToString(buffer, 0, n);
                    ReceiveMsg(s);
                    virtualDeviceManager.receiveMsg(this, s);
                }
                catch (Exception ex)
                {
                }
            }
        }
Beispiel #2
0
        public static string forshow(string msg)
        {
            string tmp;

            byte[] tmp2;
            tmp2 = StringByteHelper.StringToBytes(msg);
            tmp  = Encoding.GetEncoding("gb2312").GetString(tmp2, 5, tmp2.Length - 6);
            return(tmp);
        }
Beispiel #3
0
 public override void SendMsg(String s)
 {
     lock (mySocket)
     {
         if (mySocket != null)
         {
             try
             {
                 byte[] buffer = StringByteHelper.StringToBytes(s);
                 mySocket.Send(buffer);
                 virtualDeviceManager.sendMsg(this, s);
             }
             catch (Exception ex)
             {
             }
         }
     }
 }
Beispiel #4
0
        public static String createModbusMessage(byte func, byte[] data, byte dev = 0x88)
        {
            byte[] cmd = new byte[6 + data.Length];
            cmd[0] = 0x55;
            cmd[1] = 0xAA;
            cmd[2] = (byte)(data.Length + 3);
            cmd[3] = dev;
            cmd[4] = func;
            for (int i = 0; i < data.Length; i++)
            {
                cmd[i + 5] = data[i];
            }
            byte crc = 0x00;

            for (int i = 0; i < data.Length + 5; i++)
            {
                crc ^= cmd[i];
            }
            cmd[data.Length + 5] = crc;
            return(StringByteHelper.BytesToString(cmd, 0, cmd.Length));
        }
Beispiel #5
0
        public static ModbusMessage decodeModbusMessage(String msg)
        {
            ModbusMessage res = new ModbusMessage();

            byte[] cmd = StringByteHelper.StringToBytes(msg);
            if (!check_modbus_message(cmd))
            {
                return(null);
            }
            res.Dev     = cmd[3];
            res.MsgType = ModbusMessage.byteToMessageType(cmd[4]);
            int len = cmd[2] - 3;

            byte[] data = new byte[len];
            for (int i = 0; i < len; i++)
            {
                data[i] = cmd[i + 5];
            }
            String    s   = Encoding.Default.GetString(data);
            Hashtable map = new Hashtable();

            String[] words = s.Split(';');
            String[] parts;
            for (int i = 0; i < words.Length; i++)
            {
                if (words[i].Length == 0)
                {
                    continue;
                }
                parts = words[i].Split('=');
                if (parts.Length != 2)
                {
                    return(null);
                }
                map.Add(parts[0], parts[1]);
            }
            res.Data = map;
            return(res);
        }