0

I am using a hardware that works with 2 baud rate and I can set this baud rate with my rs 232 command.

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
  2. send command - if answered received then no change
  3. if no answer then - close port
  4. open port at baud rate - 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. 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;
        }

}

Alex K.
  • 171,639
  • 30
  • 264
  • 288
Parnal p
  • 40
  • 6
  • I didn't understand the whole question. My eyes stopped on a Thread.Sleep; Can you use a Task for this method to run in background? Then your interface wouldn't freeze. – Boris P Oct 24 '18 at 09:49
  • Forget about Thread.sleep .. see the steps I added – Parnal p Oct 24 '18 at 10:08
  • I can't give an answer to your question, but I remember one of my colleagues struggling with the .NET serial port class some time ago and swearing a lot ;) He ended up using ZylSerialPort.NET component, which finally allowed him to make some progress. It might be worth trying it out. I don't do comms myself, don't ask me for details. – Thinko Oct 24 '18 at 14:12

1 Answers1

0

Where is your code for the DataReceived event? 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. 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).

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;
Baddack
  • 1,947
  • 1
  • 24
  • 33