C ++ CLI串口丢失收到的数据

时间:2017-04-08 12:25:58

标签: visual-studio serial-port c++-cli

我遇到串口丢失数据的问题。当我运行第一次代码时,所有数据都是正确的。但是每次下载数据都会丢失。

例如:

第一次:01 00 00 00 01 0A CA - 正确

第二次:0A CA - 缺少数据

第三次:CA - 缺少数据

private: System::Void serialPort1_DataReceived(System::Object^  sender, System::IO::Ports::SerialDataReceivedEventArgs^  e) {

                  numbytes = serialPort1->BytesToRead;
                  encodedBytes = gcnew array<Byte>(numbytes);
                  for (ck = 0; ck < numbytes; ck++){
                      encodedBytes[ck] = serialPort1->ReadByte();
                  }
             this->Invoke(gcnew EventHandler(this,&MyForm3::SetTextCallback));
    }
    private: System::Void SetTextCallback(System::Object^ sender, System::EventArgs^ e){
   textBox9->Text = "";
     for (ck = 0; ck <numbytes; ck++){
         this->textBox9->AppendText((encodedBytes[ck].ToString("X2")));
    }
    numbytes = 0;
    ck = 0;

    }

0 个答案:

没有答案