Exemple #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)
                {
                }
            }
        }
Exemple #2
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));
        }