No announcement yet.

Weird Thread Create problem

  • Filter
  • Time
  • Show
Clear All
new posts

  • Weird Thread Create problem

    I have a serial comms app that reads data from a COM port. Some items are displayed for monitoring purposes, i.e. we can see the app is running properly and receiving data. Since system the app is running on (NT 4 SP 6A) has a Digi AccelePort 8r 920 boeard installed. It has 8 extra COM ports numbered 3 through 10 (the system itself has the usual 2 ports).

    Receiving data from the AccelePort ports works fine in itself, but I get an Illegal function call error (error # 5) in the Thread Create statement of the PortOpen() function. This only happens when I use the ports on the AccelePort - if I use COM1 or COM2 this error does not occur. The really odd thing about it is that the thread is created and runs just fine. My error handling routine pops a message box with the error, but I can see the main dialog getting updated in the background. I even rewrote the error trap to ignore error # 5, and everything worked flawlessly (in the sense that the error doesn't seem to have any noticeable effect).

    I wrote a small test app that reads data from the same port but does everything in the main thread - no problems or errors at all.

    Any hints & tips as to why this occurs would be very much appreciated. I've included the code below, reducing it as much as possible while making sure it still compiles & runs in case anyone has a similar setup and wants to try it. You don't have to have any inbound data on the port to see the error, but then you'll just have to take my word that the dialog would update if there were any data


    Here's the code:

    #Compile Exe "FORUM.exe"
    #Dim All
    #Register None
    #Option Version4
    #Debug Error On
    #Include ""
    %IDC_STATIC         = -1
    %IDC_SYSINFO01      = 1201
    Declare Function CreateDlg(ByVal hParent As Long) As Long
    Declare Function PortOpen() As Long
    Global hMain        As Long             '   handle to main dialog
    Global hComm        As Long             '   handle to COM port
    Global hThread      As Long             '   handle to data receive thread
    Global fThreadClose As Long             '   flag to close port listen thread
    Function PbMain() As Long
        Local iDlgMainResult As Long
        hMain = CreateDlg(0)
        iDlgMainResult = -1
        Dialog Show Modeless hMain To iDlgMainResult
        If Not IsTrue PortOpen() Then Function = 1: Exit Function
            Dialog DoEvents
        Loop Until iDlgMainResult = 0
        Call PortClose
    End Function
    Function PortClose() As Long
        Local sDummy As String
        Local iResult As Long
        On Error GoTo PortClose_Error
        fThreadClose = %TRUE
            Thread Close hThread To iResult
            Sleep 0
        Loop Until IsTrue iResult
        Sleep 1000
        If Comm(#hComm, RxQue) Then
            Comm Recv #hComm, Comm(#hComm, RxQue), sDummy
        End If
        Comm Close #hComm
        hComm = 0
        Function = %TRUE
        Exit Function
        Function = %FALSE
        MsgBox "PortClose() error # " & Format$(Err)
        Resume PortClose_Exit
    End Function
    Function PortOpen() As Long
        On Error GoTo PortOpen_Error
        hComm = FreeFile
        Comm Open "\\.\COM3" As #hComm
        If ErrClear Then Function = %FALSE: Exit Function
        Comm Set #hComm, Baud = 9600
        Comm Set #hComm, Byte = 8
        Comm Set #hComm, Parity = %FALSE
        Comm Set #hComm, Stop = 0
        Comm Set #hComm, DtrLine = %TRUE
        Comm Set #hComm, TxBuffer = 1024
        Comm Set #hComm, RxBuffer = 16384
        Thread Create ReadPort(hMain) To hThread
        Function = %TRUE
        Exit Function
        Function = %FALSE
        MsgBox "PortOpen() error # " & Format$(Err)
        Resume PortOpen_Exit
    End Function
    Function ReadPort(ByVal hWnd As Long) As Long
        Local sRecv As String
        Local cFrames As Long
        Local iRxQue As Long
        On Error GoTo ReadPort_Error
        Do While IsFalse fThreadClose
            iRxQue = Comm(#hComm, RxQue)
            If iRxQue < 800 Then Sleep 50: Iterate Loop
            Comm Recv #hComm, 800, sRecv
            cFrames = cFrames + (Len(sRecv) / 16)
            Control Set Text hMain, %IDC_SYSINFO01, Format$(cFrames, ",")
        Function = %TRUE
        Exit Function
        Function = %FALSE
        MsgBox "ReadPort() Error: " & Format$(Err)
        Resume ReadPort_Reentry
    End Function
    Function CreateDlg(ByVal hParent As Long) As Long
        Local hDlg    As Long
        Dialog  New hParent,"HI Sensor Reader",0,0,250,203,%DS_MODALFRAME Or %DS_CENTER Or %WS_MINIMIZEBOX Or %WS_CAPTION Or %WS_SYSMENU , To hDlg
        Control Add Label, hDlg, %IDC_STATIC,"Frames read",68,160,45,10,%SS_RIGHT Or %WS_VISIBLE Or %WS_CHILD
        Control Add TextBox, hDlg, %IDC_SYSINFO01,"",118,160,40,12,%ES_RIGHT Or %WS_DISABLED Or %WS_VISIBLE Or %WS_CHILD Or %WS_TABSTOP,%WS_EX_CLIENTEDGE
        Function = hDlg
    End Function

  • #2
    Can anyone here (with sufficient serial ports) try this?

    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    mailto:[email protected]