0

リストに保存したコマンドがいくつかあります。リストを通過し、コマンドを送信し、特定の時間応答を待つ必要があります (時間もリストに含まれています)。

これは私のコードです:

SerialPort myPort = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

bool _doSend;
string _inSensor;
int _threadWait;

Thread myThread = new Thread(myStartingMethod);

checkPortStatus();

private void checkPortStatus()
{
    if (!(myPort.IsOpen == true))
        {
            myPort.Open();
            myPort.DataReceived += new SerialDataReceivedEventHandler(myPort_DataReceived);
            _doSend = true;
            myThread.Start();
        }
        else
        {
            _doSend = false;
            myPort.Close();
            myThread.Abort();
        }
}

public void myStartingMethod()
    {
        //Create list of commands
        List<List<String>> atCommands = new List<List<string>>();

        //Create list of time to wait for informaition (recieve on setial port)
        List<List<String>> thSleep = new List<List<string>>();

        thSleep.Add(new List<string> { "1", "1000" }); //{"Sensor ID", "Time to wait for replay"}
        thSleep.Add(new List<string> { "2", "5000" });

        atCommands.Add(new List<string> { "1", "A111", "B111"});  //{"Sensor ID", "command 1", "command 2"}
        atCommands.Add(new List<string> { "2", "C222", "D222"});
        atCommands.Add(new List<string> { "1", "A111", "B111", "E111", "F111"});  
        atCommands.Add(new List<string> { "2", "D111"});

        while(_doSend)
        {
            foreach (var _subList in thSleep)
            {
                _inSensor = _subList.ElementAt(0);
                _threadWait = Convert.ToInt32(_subList.ElementAt(1));

                foreach(var subCommands in atCommands)
                {
                     if(_inSensor == subCommand.AlementAt(0)
                     {
                        foreach (string command in subCommands.Skip(1))
                        {
                            myPort.Write(command + "\r\n");

                            Thread.Sleep(_threadWait);
                        }
                     }
                }
            }

        }
    }
void myPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        //Do something with data and save to Database
    }

私の質問は次のとおりです。送信したコマンドに対する返信を 100% 確実に受け取るにはどうすればよいですか。一定時間内に応答がない場合は、別のコマンドを実行する必要があります。「myPort_DataReceived」では、最後に送信されたコマンドと受信したデータを mySql データベースに保存したいと考えています。データベースへの接続方法は知っていますが、応答待ちに問題があります。

4

0 に答える 0