示例#1
0
        public void HandleClient(object obj)
        {
            // retrieve client from parameter passed to thread
            TcpClient client = (TcpClient)obj;

            // sets two streams
            StreamWriter sWriter = new StreamWriter(client.GetStream(), Encoding.ASCII);
            StreamReader sReader = new StreamReader(client.GetStream(), Encoding.ASCII);
            // you could use the NetworkStream to read and write,
            // but there is no forcing flush, even when requested

            Boolean bClientConnected = true;


            while (bClientConnected)
            {
                // reads from stream
                //sData = sReader.ReadLine();

                Byte[] bData = new Byte[25];

                Int32 count = client.GetStream().Read(bData, 0, 25);

                if (count > 0)
                {
                    Byte[] messageDataCRC = (Byte[])bData.Skip(2).Take(14).ToArray();
                    Byte[] messageData    = (Byte[])bData.Skip(2).Take(12).ToArray();


                    Console.WriteLine(BitConverter.ToString(bData));
                    Console.WriteLine(string.Format("Message: {0} - Qty: {1}", BitConverter.ToString(messageDataCRC), messageDataCRC.Count()));
                    Console.WriteLine(String.Format("CRC-ITU: {0}", BitConverter.ToString((Byte[])messageDataCRC.Skip(messageDataCRC.Count() - 2).Take(2).ToArray())));
                    Console.WriteLine(String.Format("Calculated (CRC-ITU over {0} = {1}", BitConverter.ToString(messageData), BitConverter.ToString(BitConverter.GetBytes(CrcGT06.GetGT06CRC(messageData)))));
                    Console.WriteLine(string.Empty);
                }

                // to write something back.
                // sWriter.WriteLine("Meaningfull things here");
                // sWriter.Flush();
            }
        }
示例#2
0
 public static ushort GetGT06CRC(byte[] data)
 {
     return(flipEndian(CrcGT06.GetCrc16(data, 0, data.Count())));
 }