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 lammps拉伸应力应变曲线分析
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
    • ¥30 python代码,帮调试
    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python