示例#1
0
        void inSerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] data = new byte[_inSerialPort.BytesToRead];
            _inSerialPort.Read(data, 0, data.Length);
            string message = "";

            Dispatcher.Invoke(() => message = message_out.Text);


            if (DataWrapper.isToken(data))
            {
                byte reservPr = DataWrapper.getReservPriority(data);
                if ((_priority > reservPr) && (isWrite == true))
                {
                    data = DataWrapper.setReservPriority(data, _priority);
                }
                if ((_priority == reservPr) && (isWrite == true))
                {
                    Dispatcher.Invoke(() => message = message_out.Text);
                    data = new byte[message.Length];

                    int index = 0;
                    foreach (var ch in message)
                    {
                        data[index++] = (byte)ch;
                    }
                    byte DA = ComToSendToId();
                    data    = DataWrapper.createMessage(data, DA, _id, _priority);
                    isWrite = false;
                }
                MessageSend(data);
            }
            else
            {
                byte DA = DataWrapper.getDestAdress(data);
                if (DA == _id)
                {
                    byte[] temp         = DataWrapper.getInfo(data);
                    string message_temp = "";
                    foreach (var a in temp)
                    {
                        message_temp += (char)a;
                    }
                    message_in.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() => { message_in.Text = message_temp; }));
                    MessageSend(data);
                }
                else
                {
                    byte SA = DataWrapper.getSourceAdress(data);
                    if (SA == _id)
                    {
                        byte   reservPr = DataWrapper.getReservPriority(data);
                        byte[] message1 = DataWrapper.getToken(reservPr);
                        MessageSend(message1);
                    }
                    else
                    {
                        MessageSend(data);
                    }
                }
            }
            Thread.Sleep(5000);
        }