Chapter 10
The safest approach is to check before each write to a port to verify that the
transmit buffer has room for the data:
Dim stringToWrite as String
stringToWrite = "hello, world"
' Find out if the transmit buffer has room for the new data.
If ((myComPort.WriteBufferSize - myComPort.BytesToWrite) > _
stringToWrite.Length) Then
myComPort.Write(stringToWrite)
Else
Console.WriteLine("Not enough room in transmit buffer.")
End If
String stringToWrite;
stringToWrite = "hello, world";
// Find out if the transmit buffer has room for the new data.
if ((myComPort.WriteBufferSize - myComPort.BytesToWrite) > stringToWrite.Length)
{
myComPort.Write(stringToWrite);
}
else
{
Console.WriteLine("Not enough room in transmit buffer.");
}
Even writing a small amount of data can overflow the write buffer if previous
write operations have filled the buffer. A loop or timer routine can check for
room in the buffer at intervals and write the data when the buffer has room.
0
033
1 3
As with read operations, each write operation adds overhead. To keep from
hanging the application, perform the write operations in a separate thread as
described above. Write large amounts of data in one operation or a series of
large blocks. Set WriteTimeout long enough to prevent timeouts under normal
conditions, including delays due to flow control.