2 myway19891107 myway19891107 于 2014.09.23 06:33 提问

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;
            }
        }
    }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!