シリアルポートを介して定期的にデータを受信しています。これをプロットして、さらに多くのことを行うためです。この目的を達成するために、各パケットの長さを指定するヘッダーを使用して、マイクロコントローラーからコンピューターにデータを送信します。
最後の1つの詳細を除いて、プログラムは完全に実行および動作しています。ヘッダーが長さを指定している場合、そのバイト数に達するまでプログラムは停止しません。したがって、何らかの理由で1つのパケットの一部のデータが失われた場合、プログラムは待機して次のパケットの開始を取得します...その後、実際の問題を開始します。その瞬間以来、すべてが失敗します。
私は0.9秒ごとにタイマーを上げることを考えました(パッケージは毎秒来る)。これは、変数を待ってリセットするために戻ってくるためにコマンドを出します。方法がわからないので試してみましたが、実行中にエラーが発生します。IndCom(次のコードを参照)が一部の関数の途中でリセットされ、「インデックスが範囲外」になるとエラーが発生するため。
コードを添付します(タイマーなし)
private void routineRx(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
try
{
int BytesWaiting;
do
{
BytesWaiting = this.serialPort.BytesToRead;
//Copy it to the BuffCom
while (BytesWaiting > 0)
{
BuffCom[IndCom] = (byte)this.serialPort.ReadByte();
IndCom = IndCom + 1;
BytesWaiting = BytesWaiting - 1;
}
} while (IndCom < HeaderLength);
//I have to read until I got the whole Header which gives the info about the current packet
PacketLength = getIntInfo(BuffCom,4);
while (IndCom < PacketLength)
{
BytesWaiting = this.serialPort.BytesToRead;
//Copy it to the BuffCom
while (BytesWaiting > 0)
{
BuffCom[IndCom] = (byte)this.serialPort.ReadByte();
IndCom = IndCom + 1;
BytesWaiting = BytesWaiting - 1;
}
}
//If we have a packet--> check if it is valid and, if so, what kind of packet is
this.Invoke(new EventHandler(checkPacket));
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
私はオブジェクト指向プログラミングとc#に慣れていないので、気を付けてください。そして、どうもありがとうございました