Пример #1
0
 private void DataReceive(object sender, SerialDataReceivedEventArgs e)
 {
     int i = 0;
     byte j;
     MyportData myRecData = new MyportData();
     while (true)
     {
         try
         {
             j = (byte)serialPort1.ReadByte();
             i = myRecData.saveChar(j);//返回值 1 正在接收 2接收完成 负数有错误自动重新开始
             if (i == 2)
             {
                 QueueData qd = new QueueData();
                 qd.cmd = "port2Thread";
                 qd.o = myRecData;
                 portRecQueue.Enqueue(qd);
                 myRecData = new MyportData();
                 myRecData.num = 0;
             }
         }
         catch (Exception)
         {
             myRecData.num = 0;
             return;
         }
     }
 }
Пример #2
0
        private int runForwarding(byte cmd, byte[] b, int len, int time)
        {
            
            QueueData c;
            MyportData sendData = new MyportData();

            byte[] b2;
            sendData.start = 0xbb;
            sendData.cmd = cmd;
            sendData.len = len;
            sendData.data = b;
            sendData.end = 0xee;
            b2 = sendData.getByte();

            byte[] b3 = new byte[12 + b2.Length];
            int[] i3 = new int[] { type,id, time };
            Buffer.BlockCopy(i3, 0, b3, 0, 12);
            Buffer.BlockCopy(b2, 0, b3, 12, b2.Length);

            sendData.start = 0xbb;
            sendData.cmd = 0x61;
            sendData.len = b3.Length;
            sendData.data = b3;
            sendData.end = 0xee;
            b2 = sendData.getByte();

            while (portRecQueue.TryDequeue(out c));
            serialPort1.Write(b2, 0, b2.Length);            //发送命令
            time += 10;
            while (true)
            {
                time--;

                while (portRecQueue.TryDequeue(out c))
                {
                    if (c.cmd == "port2Thread")
                    {
                        MyportData tem = (MyportData)c.o;
                        if (tem.cmd == (0x61 | 0x10))
                        {
                            MyportData myPData = new MyportData();
                            for (int i = 0; i < tem.len; i++)
                            {
                                myPData.saveChar(tem.data[i]);
                            }
                            g_tem = myPData;
                            if (myPData.cmd == (cmd | 0x10))
                                return 1;
                            else
                                return -1;
                        }else
                        {
                            return -1;
                        }
                    }
                }

                if (time < 0)
                    break;
                Thread.Sleep(1);
            }
            return -1;
        }