Hello,
I am creating a program on C# and I am using the function "string SerialPort.ReadTo(string value)" to read data from the serialport.
This is supposed to read until a character or string that you provide.
If it is not able to find this string it will return a Timeout Exception after the value in ms of SerialPort.ReadTimeout.
My problem is that my code works fine only on pc`s that has the Visual Studio environment with C# installed.
In every other PC I tried it, I am always instantly having the "<font size=2><span style="font-family:Courier System.TimeoutException" <span style="font-family:Arial without reading any data at all and without waiting 2500ms for the TimeoutException to come up..</font>
My code is the following:
<font size=2>
</font>
<div style="text-align:left
<div style="text-align:left <font size=2><span style="font-family:Courier
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
public string ReceiveSerialData(SerialPort sp)
<font size=2><span style="font-family:Courier {<br style="font-family:Courier <span style="font-family:Courier string Result = "";<br style="font-family:Courier <span style="font-family:Courier if (sp.IsOpen)<br style="font-family:Courier <span style="font-family:Courier {<br style="font-family:Courier <span style="font-family:Courier try<br style="font-family:Courier <span style="font-family:Courier {
</font>
<div style="margin-left:160px;font-family:Courier <font size=2>sp.ReadTimeout = 2500;
</font>
<div style="margin-left:160px <font style="font-family:Courier" size=2>sp.WriteTimeout = 2500;</font>
<div style="margin-left:80px;font-family:Courier <font size=2> Result = sp.ReadTo("rn");
</font> <font size=2><span style="font-family:Courier }<br style="font-family:Courier <span style="font-family:Courier catch (System.TimeoutException ex)<br style="font-family:Courier <span style="font-family:Courier {<br style="font-family:Courier </font>
<div style="margin-left:80px;font-family:Courier <font size=2> MessageBox.Show(ex.Message, "Receive Data Error. Read Timeout", MessageBoxButtons.OK, MessageBoxIcon.Error);
</font> <font size=2><span style="font-family:Courier }<br style="font-family:Courier <span style="font-family:Courier catch { }<br style="font-family:Courier <span style="font-family:Courier }<br style="font-family:Courier <br style="font-family:Courier <span style="font-family:Courier return Result;</font>
} <br style="font-family:Courier <span style="font-family:Courier </font><font size=2><span style="font-family:Courier <br style="font-family:Courier <span style="font-family:Courier </font> Is there a way to implement proper ReadTimeout waiting times for Serial ports on C#?
View the full article
I am creating a program on C# and I am using the function "string SerialPort.ReadTo(string value)" to read data from the serialport.
This is supposed to read until a character or string that you provide.
If it is not able to find this string it will return a Timeout Exception after the value in ms of SerialPort.ReadTimeout.
My problem is that my code works fine only on pc`s that has the Visual Studio environment with C# installed.
In every other PC I tried it, I am always instantly having the "<font size=2><span style="font-family:Courier System.TimeoutException" <span style="font-family:Arial without reading any data at all and without waiting 2500ms for the TimeoutException to come up..</font>
My code is the following:
<font size=2>
</font>
<div style="text-align:left
<div style="text-align:left <font size=2><span style="font-family:Courier
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
public string ReceiveSerialData(SerialPort sp)
<font size=2><span style="font-family:Courier {<br style="font-family:Courier <span style="font-family:Courier string Result = "";<br style="font-family:Courier <span style="font-family:Courier if (sp.IsOpen)<br style="font-family:Courier <span style="font-family:Courier {<br style="font-family:Courier <span style="font-family:Courier try<br style="font-family:Courier <span style="font-family:Courier {
</font>
<div style="margin-left:160px;font-family:Courier <font size=2>sp.ReadTimeout = 2500;
</font>
<div style="margin-left:160px <font style="font-family:Courier" size=2>sp.WriteTimeout = 2500;</font>
<div style="margin-left:80px;font-family:Courier <font size=2> Result = sp.ReadTo("rn");
</font> <font size=2><span style="font-family:Courier }<br style="font-family:Courier <span style="font-family:Courier catch (System.TimeoutException ex)<br style="font-family:Courier <span style="font-family:Courier {<br style="font-family:Courier </font>
<div style="margin-left:80px;font-family:Courier <font size=2> MessageBox.Show(ex.Message, "Receive Data Error. Read Timeout", MessageBoxButtons.OK, MessageBoxIcon.Error);
</font> <font size=2><span style="font-family:Courier }<br style="font-family:Courier <span style="font-family:Courier catch { }<br style="font-family:Courier <span style="font-family:Courier }<br style="font-family:Courier <br style="font-family:Courier <span style="font-family:Courier return Result;</font>
} <br style="font-family:Courier <span style="font-family:Courier </font><font size=2><span style="font-family:Courier <br style="font-family:Courier <span style="font-family:Courier </font> Is there a way to implement proper ReadTimeout waiting times for Serial ports on C#?
View the full article