从serialport填充datagrid

时间:2012-01-19 16:12:22

标签: wpf datagrid serial-port

我正在研究一个解码器程序,它从通过串口连接的接口读取,然后我解释信息并将其放入数据网格中。我遇到的问题是,并非所有信息都传输到数据网格中,但我不确定原因。

// Declare a delegate for method DoUpdate().
    private delegate void MethodDelegate();
    private MethodDelegate ReadSerialData;

    bool loggerRunning;
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        if (!loggerRunning)
        {
            // Delegate for DoUpdate.
            ReadSerialData = new MethodDelegate(DoUpdate);

            //Create new serialport.
            Sp = new SerialPort();


            //Set the appropriate properties.
            Sp.PortName = SetPortName(Sp.PortName);
            Sp.BaudRate = SetBaudRate(Sp.BaudRate);
            Sp.Parity = Parity.None;
            Sp.DataBits = SetDataBits(Sp.DataBits);
            Sp.StopBits = StopBits.One;
            Sp.Handshake = Handshake.None;
            Sp.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);

            Sp.Open();
            loggerRunning = true;
        }
        else
        {
            Sp.Close();
            loggerRunning = false;
        }

    }

 private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        Dispatcher.Invoke(DispatcherPriority.Normal, ReadSerialData);
    }

    private List<IEBusData> sortedData = new List<IEBusData>();
    // Process received data.
    // Via delegate ReadSerialData.
    private void DoUpdate()
    {
        string[] tempRow = new string[35];
        try
        {
            do
            {
                try
                {
                    String Temp = Sp.ReadByte().ToString("X");         // Read from Input Buffer
                    Byte_Read++;
                    if ((Temp == "7B") && (Rx_State != HDR0 || Rx_State != Rx_Data))
                    {
                        Rx_State = HDR0;
                    }
                    switch (Rx_State)
                    {
                        case HDR0:                                              // Check for Header byte 0
                            // Check to see if matches Header0
                            if (Temp == "7B")
                            { Rx_State++; }
                            break;
                        case HDR1:                                              // Check for Header byte 1
                            if (Temp == "7D")
                            {
                                Rx_State++;
                            }
                            else
                            { Rx_State = HDR0; }
                            break;
                        case MSTR_ADDR:                                         // Clock in Master Address
                            MASTER += Temp;
                            if (MASTER.Length == 3)                             // Master Address is 3 bytes
                            { Rx_State++; }
                            break;
                        case SLVE_ADDR:                                         // Clock in Slave Address
                            SLAVE += Temp;
                            if (SLAVE.Length == 3)                              // Slave Address is 3 bytes
                            {          
                                tempRow[0] = MASTER;            // Put Master Address into Grid
                                tempRow[1] = SLAVE;             // Put Slave Address into Grid
                                Rx_State++;                                     // Move to next Receive State
                                MASTER = "";                                    // Reset MASTER
                                SLAVE = "";                                     // Reset SLAVE
                            }
                            break;
                        case Rx_Len:                                            // Clock in Message Length
                            Length = Convert.ToInt16(Temp, 16);                 // Convert Length back to Hex digit
                            if (Length <= 0x20)
                            {
                                if (Temp.Length == 1)
                                {
                                    tempRow[2] = "0" + Temp;
                                }
                                else
                                {
                                    tempRow[2] = Temp;
                                }
                                Rx_State++;
                            }
                            else if (Length == 0x7B)
                            { Rx_State = HDR1; }
                            else if (Length == 0x7D)
                            { Rx_State = MSTR_ADDR; }
                            else
                            { Rx_State = HDR0; }
                            break;
                        case Rx_Data:                                           // Clock in Data Bytes
                            if (Temp.Length == 1)
                            {
                                tempRow[Data_Column] = "0" + Temp;
                            }
                            else
                            {
                                tempRow[Data_Column] = Temp;
                            }
                            Length--;
                            Data_Column++;
                            if (Length < 1)                                    // If all data Received
                            {
                                Rx_State = HDR0;                                // Reset Receive State
                                Data_Column = 3;                                // Reset Columns
                                Curr_Row++;                                     // Increment Row Number
                                sortedData.Add(new IEBusData(tempRow));
                                dgIEBus.ItemsSource = sortedData;
                                dgIEBus.Items.Refresh();
                            }
                            break;
                        default:
                            break;
                    }
                   // this.Update();
                }
                catch (SystemException)
                { }
            } while (Sp.BytesToRead > Byte_Read);
            Byte_Read = 0;
        }
        catch (InvalidOperationException) { }
    }

}

0 个答案:

没有答案