I have a program that communicates with a device over the serial port, and I am using the MSComm control to handle this. Everything seems to be working fine most of the time.
The issue is that if the user has a window open (we did this with Internet Explorer) and not maximized, and then the user clicks the scroll bar at the right time, several of the characters being transmitted are not received, and a few others are corrupted (received as "?" character). This is easily reproduceable and not a fluke.
I can reduce (but not completely eliminate) the occurrence of this by changing the priority of my program to High, but this hogs CPU processing time, and makes it almost impossible to run other programs. It seems as if the scrolling action is causing Windows to not pull characters out of the UART in a timely fashion.
The data is 130 characters transmitted at 9600 baud. I've had as little as about 110 characters successfully transmitted with the missing and corrupted characters occurring randomly in the string.
Below is a sample of my code. I use the DoEvents function to release control back to Windows to handle things such as minimizing or restoring my program, or handling other programs. Although I want to protect my data, I don't want to keep them from using their computer. Here is some sample code:
Public Function CheckForData(ByRef timeout as Boolean) As String
On Error GoTo CheckForDataErr
Dim gotdata As Boolean
Dim t1 As Double
Dim t2 As Double
gotdata = False
timeout = False
t1 = Timer
t2 = READTIMEOUT + t1
While (gotdata = False) And (timeout = False)
If MyForm.comPort.InBufferCount = EXPECTEDDATASIZE Then
MyForm.comPort.InputLen = 0
gotdata = True
End If
End If
If gotdata = False Then
t1 = Timer
If t1 > t2 Then
timeout = True
End If
DoEvents
End If
Wend
CheckForData = MyForm.comPort.Input
Exit Function
CheckForDataErr:
HandleError Err.Number, Err.Description, AppTitle
Resume Next
End Function
The way I am currently doing it, I am using a specified READTIMEOUT constant as the maximum length of time to wait (currently set to 10 seconds). In my loop, I'm checking to see if all of the data is there. If it's not there, I'm checking the current time to see if we have timed out. I then allow Windows to do what it needs to, and restart the loop.
I appreciate any advice you can give on how I might better handle this.
The issue is that if the user has a window open (we did this with Internet Explorer) and not maximized, and then the user clicks the scroll bar at the right time, several of the characters being transmitted are not received, and a few others are corrupted (received as "?" character). This is easily reproduceable and not a fluke.
I can reduce (but not completely eliminate) the occurrence of this by changing the priority of my program to High, but this hogs CPU processing time, and makes it almost impossible to run other programs. It seems as if the scrolling action is causing Windows to not pull characters out of the UART in a timely fashion.
The data is 130 characters transmitted at 9600 baud. I've had as little as about 110 characters successfully transmitted with the missing and corrupted characters occurring randomly in the string.
Below is a sample of my code. I use the DoEvents function to release control back to Windows to handle things such as minimizing or restoring my program, or handling other programs. Although I want to protect my data, I don't want to keep them from using their computer. Here is some sample code:
Public Function CheckForData(ByRef timeout as Boolean) As String
On Error GoTo CheckForDataErr
Dim gotdata As Boolean
Dim t1 As Double
Dim t2 As Double
gotdata = False
timeout = False
t1 = Timer
t2 = READTIMEOUT + t1
While (gotdata = False) And (timeout = False)
If MyForm.comPort.InBufferCount = EXPECTEDDATASIZE Then
MyForm.comPort.InputLen = 0
gotdata = True
End If
End If
If gotdata = False Then
t1 = Timer
If t1 > t2 Then
timeout = True
End If
DoEvents
End If
Wend
CheckForData = MyForm.comPort.Input
Exit Function
CheckForDataErr:
HandleError Err.Number, Err.Description, AppTitle
Resume Next
End Function
The way I am currently doing it, I am using a specified READTIMEOUT constant as the maximum length of time to wait (currently set to 10 seconds). In my loop, I'm checking to see if all of the data is there. If it's not there, I'm checking the current time to see if we have timed out. I then allow Windows to do what it needs to, and restart the loop.
I appreciate any advice you can give on how I might better handle this.