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:
- Open Port at baud rate - 38400
- send command - if answered received then no change
- if no answer then - close port
- open port at baud rate - 9600
- send command - if answer received continue with this baud rate
- 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;
}
}