Beispiel #1
0
        private void Lol()
        {
            Console_textBox.Invoke(new Action(() => { Console_textBox.AppendText("Опрос Начат" + "\r\n"); }));
            Thread.Sleep(2000);
            for (int i = 8; i < 12; i++)
            {
                work_msg.SetSendFlag();
                if (work_msg.isSendFlag())
                {
                    Console_textBox.Invoke(new Action(() => { Console_textBox.AppendText("Запрос данных..." + "\r\n"); }));

                    byte   SAD     = (byte)((byte)SAD_numericUpDown.Value | 0x80);
                    byte   DAD     = (byte)DAD_numericUpDown.Value;
                    byte   FNC     = 0x18;
                    string SendMsg = "\t0\t65530\f\t5\t2\t20\t" + i + "\t0\t0\f";
                    byte[] msg     = bus.CreateMessage(DAD, SAD, FNC, SendMsg);
                    bus.StackClear();
                    bus.AddToStack(msg);
                    if (HEX_radioButton.Checked)
                    {
                        Console_textBox.Invoke(new Action(() => {
                            Console_textBox.AppendText(bus.StackToHEX() + "\r\r\n");
                        }));
                    }
                    else if (Text_radioButton.Checked)
                    {
                        Console_textBox.AppendText(bus.StackToString() + "\r\r\n");
                    }
                    bus.StackClear();
                    if (_serialPort.IsOpen)
                    {
                        _serialPort.Write(msg, 0, msg.Length);
                    }
                    else
                    {
                        Console_textBox.Invoke(new Action(() => { Console_textBox.AppendText("Ошибка: порт не открыт!"); }));
                        break;
                        //MessageBox.Show("Порт не открыт", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                while (work_msg.isSendFlag())
                {
                    Thread.Sleep(1000);
                }
            }
            Console_textBox.Invoke(new Action(() => { Console_textBox.AppendText("Опрос завершён" + "\r\n"); }));
            auto_button.Invoke(new Action(() => { auto_button.Enabled = true; }));
        }
Beispiel #2
0
        private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int ByteToRead = _serialPort.BytesToRead;

            byte[] buff = new byte[ByteToRead];

            _serialPort.Read(buff, 0, ByteToRead);

            Array.Copy(buff, 0, ByteBuffer, buffseek, ByteToRead);
            buffseek += ByteToRead;

            // Проверяем наличие начала, конца сообщения и его контрольных чисел
            // если успешно, то выполняем вложенный код
            if (work_msg.isFullMessage(ByteBuffer, buffseek))
            {
                // Находим индексы вхождения кодов soh, isi, stx и etx в буфере приёма
                int soh = work_msg.GetSOH(ByteBuffer);
                //int isi = work_msg.GetISI(ByteBuffer);
                int stx = work_msg.GetSTX(ByteBuffer);
                int etx = work_msg.GetETX(ByteBuffer);

                // Объявляем массив для целого сообщения и переносим в него сообщение из буфера приёма
                byte[] finalbuff = new byte[etx + 4];
                Array.Copy(ByteBuffer, soh, finalbuff, 0, etx + 4);

                // Объявляем и заполняем массивы заголовка, тела и контрольного числа
                byte[] Head = work_msg.GetHead(finalbuff, soh, stx);
                byte[] Body = work_msg.GetBody(finalbuff, stx, etx - stx + 2);
                byte[] CRC  = work_msg.GetBody(finalbuff, etx + 2, 2);

                string result = "";
                for (int i = 0; i < finalbuff.Length; i++)
                {
                    result += finalbuff[i].ToString("X2") + " ";
                }
                Console_textBox.Invoke(new Action(() => { Console_textBox.AppendText("Message Received: => {" + result + " }"); }));

                // Меняем местами значения в массиве CRC и сравниваем его с подсчитанным функцией CrCode2 контрольным числом
                // Если коды верны, то выполняем вложенный код
                Array.Reverse(CRC);
                if (BitConverter.ToInt16(CRC, 0) == bus.CrCode2(finalbuff))
                {
                    Console_textBox.Invoke(new Action(() => { Console_textBox.AppendText(" CRC: OK" + "\r\n"); }));
                }
                else
                {
                    Console_textBox.Invoke(new Action(() => { Console_textBox.AppendText(" CRC: FAILED" + "\r\n"); }));
                }

                string[] result_str = work_msg.GetStrings(Body);
                for (int i = 0; i < result_str.Length; i++)
                {
                    Console_textBox.Invoke(new Action(() => { Console_textBox.AppendText(result_str[i] + "\r\n"); }));
                }
                //MessageBox.Show(bodyStr);

                Array.Clear(ByteBuffer, 0, ByteBuffer.Length);
                buffseek = 0;
                work_msg.DropSendFlag();
            }
        }