Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 21089

Problem with serial port and over-lapping events

$
0
0
Hi all,

I'm writing a relatively simple program which requires some data exchange over the serial port. I'm using the standard MSComm control that comes with VB6 (SP3).

High level requirement:

1) Every 100ms, send a request to the serial port, receive 36 bytes, parse + load into array

2) Ad-hoc, button press, tx request to serial port, receive either a single byte or a packet of 12, perform some actions


Ok, so the actual issue. When I do any of the above routines, I am setting a public busy flag. The routine checks if the flag is set, and if so sit in a Do Loop until the flag clears. This isn't working.

To test, ignoring the timer part on the requirement 1, I put a 1 second delay in the routine which handles requirement 2. If I hit the button twice within one second, it hangs and gets stuck in the Do Loop which does the busy test.

My theory so far is multiple clones of the button click code are executing at the same time.. I was hoping using a public var to check status would fix, but it appears not...

Any thoughts/advice/fixes folks? :)


Here's a partial code extract:

Code:

Private Sub cmdReadConf_Click()
Dim strOutput  As String

    strOutput = CommsFunctions.strGlobalRead("V", 21)
    txtStatusWindow.Text = ConversionFunctions.StringToHex(strOutput) & vbCrLf & txtStatusWindow.Text
 
End Sub


Public Function strGlobalRead(strCommand As String, bytLength As Byte) As String
Dim strBuffer      As String
Dim dtmStartTime    As Single
Dim strRawPacket    As String

    Do While blnSerialBusy
        DoEvents
    Loop
    blnSerialBusy = True

    ' Empty Serial Buffer
    frmMain.mscom1.InBufferCount = 0

    ' Send request to serial port
    frmMain.mscom1.Output = strCommand
    ' Grab current time for timeout routine
    dtmStartTime = Timer()
    ' Receive bytLength bytes from serial buffer - otherwise assume failure and time out after 3 seconds
    Do
        strBuffer = strBuffer & frmMain.mscom1.Input
        DoEvents
        If Timer() >= dtmStartTime + 3 Then
            MsgBox "Incomplete packet!", vbExclamation + vbOKOnly, "Comms Error!"
            Exit Do
        End If
    Loop Until Len(strBuffer) = bytLength
   
    GeneralFunctions.wait (1000)
   
    strGlobalRead = strBuffer
    blnSerialBusy = False

End Function


Viewing all articles
Browse latest Browse all 21089

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>