简体   繁体   English

串口问题

[英]Serial port Issue

I am using a hardware that works with 2 baud rate and I can set this baud rate with my rs 232 command. 我使用的是工作于2波特率的硬件,可以通过rs 232命令设置此波特率。

The problem is I don't want to set the baud rate manually, I want the software to set one baud rate and send a command, if i get an answer continue with this baud rate or else change it to second baud rate and send a command, if answered, continue with this baud rate. 问题是我不想手动设置波特率,我希望软件设置一个波特率并发送命令,如果我得到答复,请继续此波特率,否则将其更改为第二个波特率并发送命令,如果已回答,则继续此波特率。

Steps: 脚步:

  1. Open Port at baud rate - 38400 波特率开放端口-38400
  2. send command - if answered received then no change 发送命令-如果收到答复,则无变化
  3. if no answer then - close port 如果没有答案,则关闭端口
  4. open port at baud rate - 9600 以波特率打开端口-9600
  5. send command - if answer received continue with this baud rate 发送命令-如果收到答复,则继续此波特率
  6. if no answer - error message 如果没有答案-错误消息

I wrote a code, but the command is not send to the hardware and If I give Thread.sleep() then my interface freezes. 我写了一个代码,但是命令没有发送到硬件,如果我给Thread.sleep(),那么我的界面就会死机。 How can I achieve this? 我该如何实现?

Following is my code: 以下是我的代码:

if (!SCP.IsOpen) {
            SCP.PortName = cBoxComPort.Text;
            SCP.BaudRate = 38400;
            SCP.Parity = Parity.None;
            SCP.DataBits = 8;
            SCP.StopBits = StopBits.One;

            SCP.DataReceived += SerialPort1DataReceived;

            SCP.ReceivedBytesThreshold = 1;

            SCP.Open();

            dataout = "get rs232";    // test command 
            SCP.Write(dataout + "\r");
            progressBar1.Value = 100;


            fnLogFile = new StreamWriter("Logfile_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".log", true);
            //string valueofdatain = datain;


            string stringvalue = "command syntax error at cursor position 000";


            if (stringvalue.CompareTo(datain) == 0 ) {
            SCP.Close();
            fnLogFile.Close();


                SCP.PortName = cBoxComPort.Text;
                SCP.BaudRate = 9600;
                SCP.Parity = Parity.Even;
                SCP.DataBits = 8;
                SCP.StopBits = StopBits.One;
                SCP.Open();
                SCP.DataReceived += SerialPort1DataReceived;
                SCP.ReceivedBytesThreshold = 1;

                dataout = "get rs232";
                SCP.Write(dataout + "\r");
                progressBar1.Value = 100;

                fnLogFile = new StreamWriter("Logfile_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".log", true);

        } else {
            SCP.Close();
            fnLogFile.Close();
            progressBar1.Value = 0;
        }

} }

Where is your code for the DataReceived event? 您的DataReceived事件的代码在哪里? You need a state machine to accomplish what you are trying to do. 您需要状态机来完成您要尝试执行的操作。 See this link , I have a nice write up on how to do this. 看到这个链接 ,我对如何做到这一点写得很好。

You will basically send command 1 at 38400, if you get a response, great, move onto your next state where you do what you need to do. 基本上,您将在38400发送命令1,如果您得到响应,那很好,请转到下一个状态,在此状态下您需要执行的操作。 Otherwise, if you don't get a response, time out, set the baud rate to 9600 and go back to command 1 state (note you can switch the baud rate while the port is open I believe). 否则,如果没有响应,请超时,将波特率设置为9600,然后返回到命令1状态(请注意,我相信您可以在端口打开时切换波特率)。

Using my example from the link, here is how you add a timeout: 使用链接中的示例,这是添加超时的方法:

Stopwatch sw = new Stopwatch();

case StateMachine.SendCmd1:
    sw.Reset();
    sw.Start();

    CallbackResponse = Cmd1Response;    //set our delegate to the first response
    sp.Write("Send first command1");    //send our command through the serial port

    currentState = StateMachine.Cmd1Response;   //change to cmd1 response state
    break;
case StateMachine.Cmd1Response:
    //waiting for a response....you can put a timeout here
    if (sw.ElapsedMilliseconds > (5000) && sw.ElapsedMilliseconds != 0)
    {
        //set the baud rate
        //set current state back to SendCmd1
    }
    break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM