myway19891107 2014-09-22 22:33
浏览 921

c# serial port接收信息后转换格式再显示的问题

可以确保发送出来的信息是对的,接收到的也是对的。可是我再把它从byte转换成float并显示为string,就不是每次都能成功了。拜托大神们具体说说怎么改。相信问题出在switch statement里面。谢谢! 我的通讯协议是EE, instruction,number of data, data ,FF

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
    {
        byte rxChar=0;
        byte[] rxDataBuffer = new byte[240];
        int rxdata = 0;

        rxNumberDataBytes = (byte)serialPort.BytesToRead;
        while (serialPort.BytesToRead > 0)
        {
            rxChar = (byte)serialPort.ReadByte();  // read data
            // to display bytes received in hex,check was all data received
            Invoke(new Action(() =>
            {
                textBox2.Text += " " + rxChar.ToString("X") + "\r\n";
            }));
            switch (rxState)
            {
                case RxIdleState:
                    if (rxChar == 0xEE)
                    {
                        rxState = RxInstructionState;
                    }
                    break;

                case RxInstructionState:
                    if (rxChar == 0)
                    {
                        rxInstruction = 0;  // coordinates
                    }
                    else if (rxChar == 1)
                    {
                        rxInstruction = 1;   // voltage
                    }
                    else
                    {
                        rxInstruction = 2;    //temperature
                    }
                    rxState = RxNumberofbytesState;
                    break;

                case RxNumberofbytesState:
                    rxChar = rxNumberDataBytes;
                    rxState = RxDataState;
                    rxdata = 0;
                    break;

                case RxDataState:
                    if (rxInstruction == 0)
                    {
                        //  count number of rxdata until 8 bytes real data 
                        rxDataBuffer[rxdata] = rxChar;
                        rxdata++;
                        if (rxdata == 8)
                        {
                            float f11 = BitConverter.ToSingle(rxDataBuffer, 0);
                            float f22 = BitConverter.ToSingle(rxDataBuffer, 4);
                            Invoke(new Action(() =>
                            {
                                textBox3.Text += "Lat="+f11.ToString() + "    " + "Lng="+f22.ToString() + "\r\n";
                            }));
                            rxState = RxStopState;
                        }
                    }
                    else if (rxInstruction == 1)
                    {
                        rxDataBuffer[rxdata] = rxChar;
                        rxdata++;
                        if (rxdata == 1)
                        {
                            float voltage = BitConverter.ToSingle(rxDataBuffer, 0);
                            Invoke(new Action(() =>
                            {
                                textBox2.Text += "Current voltage is " + voltage.ToString() + "V"  + "\r\n";
                            }));
                            rxState = RxStopState;
                        }
                    }
                    else
                    {
                        rxDataBuffer[rxdata] = rxChar;
                        rxdata++;
                        if (rxdata == 1)
                        {
                            float temp = BitConverter.ToSingle(rxDataBuffer, 0);
                            Invoke(new Action(() =>
                            {
                                textBox4.Text += "Current temperature is " + temp.ToString() + " degrees" + "\r\n";
                            }));
                            rxState = RxStopState;
                        }
                    }
                    break;

                case RxStopState:
                        if (rxChar == 0xFF)
                        {
                            rxState = RxIdleState;
                        }
                    break;

                default:
                    rxState = RxIdleState;
                    break;
            }
        }
    }
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 高德地图点聚合中Marker的位置无法实时更新
    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办