Results 1 to 2 of 2

Thread: WaitForSingleObject for reading serial port?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2000
    Location
    Québec City
    Posts
    73

    WaitForSingleObject for reading serial port?

    Hi

    I want to read data from the serial port using api. Why don't I want to use the ms comm control? Who told you I was working in VB?

    Right now, I'm opening the com port as a file, change the settings of it and I use ReafFile to read data from it.

    The problem is, I want to read the port only when new data arrive.
    If I use ReadFile, I have 3 options:
    1- the function returns even if there are no data, so I need to put it in an infinite loop that takes 100% of cpu usage(not good)
    2- Infinite timeout - It needs to be in a separate thread so the app will not lock. But there is no way to kill a thread doing a ReadFile, the ReadFile need to return before the thread can be killed.(not good)
    3- A not infinite timeout, also in another thread but I only have to test if the thread should die before reading again. This is the best method up to date.
    I'd like to read the port ONLY when new data arrive.

    So, how would I read the serial port using a blocking reading, reading that I could unlock everytime I want to?

    I think that the key to this answer is with ovelapping I/O, WaitFoSingleOBject and CreateEvent but I can't figure out how to use them.

    Any help would be appreciated.

    Thanks

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Here is an example that you can, hopefully, modify to suit your needs. Hope this helps, or at least gets you started in the right direction.
    VB Code:
    1. 'This example uses a common dialog box named CDBox
    2. Const INFINITE = &HFFFF
    3. Const STARTF_USESHOWWINDOW = &H1
    4. Private Enum enSW
    5.     SW_HIDE = 0
    6.     SW_NORMAL = 1
    7.     SW_MAXIMIZE = 3
    8.     SW_MINIMIZE = 6
    9. End Enum
    10. Private Type PROCESS_INFORMATION
    11.     hProcess As Long
    12.     hThread As Long
    13.     dwProcessId As Long
    14.     dwThreadId As Long
    15. End Type
    16. Private Type STARTUPINFO
    17.     cb As Long
    18.     lpReserved As String
    19.     lpDesktop As String
    20.     lpTitle As String
    21.     dwX As Long
    22.     dwY As Long
    23.     dwXSize As Long
    24.     dwYSize As Long
    25.     dwXCountChars As Long
    26.     dwYCountChars As Long
    27.     dwFillAttribute As Long
    28.     dwFlags As Long
    29.     wShowWindow As Integer
    30.     cbReserved2 As Integer
    31.     lpReserved2 As Byte
    32.     hStdInput As Long
    33.     hStdOutput As Long
    34.     hStdError As Long
    35. End Type
    36. Private Type SECURITY_ATTRIBUTES
    37.     nLength As Long
    38.     lpSecurityDescriptor As Long
    39.     bInheritHandle As Long
    40. End Type
    41. Private Enum enPriority_Class
    42.     NORMAL_PRIORITY_CLASS = &H20
    43.     IDLE_PRIORITY_CLASS = &H40
    44.     HIGH_PRIORITY_CLASS = &H80
    45. End Enum
    46. Private Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
    47. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    48. Private Function SuperShell(ByVal App As String, ByVal WorkDir As String, dwMilliseconds As Long, ByVal start_size As enSW, ByVal Priority_Class As enPriority_Class) As Boolean
    49.     Dim pclass As Long
    50.     Dim sinfo As STARTUPINFO
    51.     Dim pinfo As PROCESS_INFORMATION
    52.     'Not used, but needed
    53.     Dim sec1 As SECURITY_ATTRIBUTES
    54.     Dim sec2 As SECURITY_ATTRIBUTES
    55.     'Set the structure size
    56.     sec1.nLength = Len(sec1)
    57.     sec2.nLength = Len(sec2)
    58.     sinfo.cb = Len(sinfo)
    59.     'Set the flags
    60.     sinfo.dwFlags = STARTF_USESHOWWINDOW
    61.     'Set the window's startup position
    62.     sinfo.wShowWindow = start_size
    63.     'Set the priority class
    64.     pclass = Priority_Class
    65.     'Start the program
    66.     If CreateProcess(vbNullString, App, sec1, sec2, False, pclass, _
    67.     0&, WorkDir, sinfo, pinfo) Then
    68.         'Wait
    69.         WaitForSingleObject pinfo.hProcess, dwMilliseconds
    70.         SuperShell = True
    71.     Else
    72.         SuperShell = False
    73.     End If
    74. End Function
    75. Private Sub Form_Load()
    76.     'KPD-Team 1998
    77.     'URL: [url]http://www.allapi.net/[/url]
    78.     'E-Mail: [email][email protected][/email]
    79.     'Set the dialog's title
    80.     CDBox.DialogTitle = "Choose an EXEC-File ..."
    81.     'Error when canceled
    82.     CDBox.CancelError = True
    83.     'Set the dialog's filter
    84.     CDBox.Filter = "EXEC-Files (*.exe)|*.exe|All files (*.*)|*.*"
    85.     'Show the 'Open File'-dialog
    86.     CDBox.ShowOpen
    87.     'Execute the program
    88.     SuperShell CDBox.filename, Left$(CDBox.filename, Len(CDBox.filename) - Len(CDBox.FileTitle)), 0, SW_NORMAL, HIGH_PRIORITY_CLASS
    89.     End
    90. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width