Hi I have a serial port which I send data and receive data from.
I can happily send data to the serial port and can receive data from the serial port, however I can only receive data successfully during the first iteration of my code, on the second iteration I can receive data but I then receive a timeout error.
private void ComSetUp(string inputString)
{
OutputString = inputString;
InitializeComponent();
//button2.Enabled = false;
//button2.Visible = false;
sp.BaudRate = 9600;
sp.DataBits = 8;
sp.StopBits = System.IO.Ports.StopBits.One;
sp.Parity = System.IO.Ports.Parity.None;
sp.ReadTimeout = 7000;
InitizeCount++;
_continue = true;
if (sp.IsOpen ==false)
sp.PortName = "COM5";
sp.Open();// change this to COM3 egs visit
}
sp.NewLine = "\r";
sp.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serial_DataReceived);
localStatus = "RequestSent";
t1 = new Thread(SendRequestData);
t1.IsBackground = true;
t1.Name = "SendRequestThread";
t1.Start();
private void serial_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
try
byte[] buffer = new byte[sp.ReadBufferSize];
int bytesRead = sp.Read(buffer, 0, buffer.Length);
tString += Encoding.ASCII.GetString(buffer, 0, buffer.Length);
if (tString.IndexOf((char)_terminator) > -1)
string workingString = tString.Substring(0, tString.IndexOf((char)_terminator));
tString = tString.Substring(tString.IndexOf((char)_terminator));
sp.DiscardOutBuffer();
sp.DiscardInBuffer();
tString = "";
ProcessSerialBuffer(workingString);
catch (Exception ex)
MessageBox.Show(ex.ToString());