Results 1 to 26 of 26

Thread: Asynchronous Open and Close of COM port

Threaded View

  1. #1

    Thread Starter
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004
    Location
    /dev/null
    Posts
    2,961

    Asynchronous Open and Close of COM port

    OK Here is an Asynchronous way to do it. I did not put the Class in a DLL or EXE. Just inside the project which may be the best option for you after all

    I wrote alot of this stuff years ago, but I just tested the project and it all works. Add the MODs, CLS, and the BAS files to your project.

    VB Code:
    1. Option Explicit
    2. '
    3. ' frm_COM_Test.frm
    4. '
    5. Private WithEvents m_COMPort As cls_COMPort
    6. '
    7.  
    8. Private Sub Form_Load()
    9.     Set m_COMPort = New cls_COMPort
    10. End Sub
    11.  
    12. Private Sub cmd_OpenPort_Click()
    13.     If (Not m_COMPort.OpenPort) Then
    14.         Me.Caption = "Open Port Unsuccessful"
    15.     Else
    16.         Me.Caption = "Open Port Successful"
    17.     End If
    18. End Sub
    19.  
    20. Private Sub cmd_ClosePort_Click()
    21.     '
    22.     Dim intReturn As Integer
    23.     intReturn = m_COMPort.ClosePort
    24.     '
    25.     If Not (intReturn = 0) Then
    26.         Me.Caption = "Can't Close Port: " & intReturn
    27.     Else
    28.         Me.Caption = "Close Port Successful"
    29.     End If
    30.     '
    31. End Sub
    32.  
    33. Private Sub cmd_OpenAsync_Click()
    34.     m_COMPort.OpenPort_Async
    35. End Sub
    36.  
    37. Private Sub cmd_CloseAsync_Click()
    38.     m_COMPort.ClosePort_Async
    39. End Sub
    40.  
    41. Private Sub m_COMPort_PortCloseDone(intSuccessful As Integer)
    42.     '
    43.     If Not (intSuccessful = 0) Then
    44.         Me.Caption = "Can't Close Port: " & intSuccessful
    45.     Else
    46.         Me.Caption = "Close Port Successful"
    47.     End If
    48.     '
    49. End Sub
    50.  
    51. Private Sub m_COMPort_PortOpenDone(blnSuccessful As Boolean)
    52.     If (Not blnSuccessful) Then
    53.         Me.Caption = "Open Port Unsuccessful"
    54.     Else
    55.         Me.Caption = "Open Port Successful"
    56.     End If
    57. End Sub

    VB Code:
    1. Option Explicit
    2. '
    3. ' VB Module: mod_ComTest.bas
    4. '
    5. Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
    6.     (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, _
    7.     ByVal dwShareMode As Long, lpSecurityAttributes As Any, _
    8.     ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, _
    9.     ByVal hTemplateFile As Long) As Long
    10. '
    11. '// This is the declaration to close the COM port
    12. Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    13. '

    VB Code:
    1. Option Explicit
    2. '
    3. ' VB Module: modTimers.bas
    4. '
    5. Private Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    6. Private Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
    7. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    8. '
    9. Private mcolItems   As Collection
    10. '
    11.  
    12. Public Sub AddTimer(ByRef pobjTimer As APITimer, ByVal plngInterval As Long)
    13.     If mcolItems Is Nothing Then
    14.         Set mcolItems = New Collection
    15.     End If
    16.     pobjTimer.ID = SetTimer(0, 0, plngInterval, AddressOf Timer_CBK)
    17.     mcolItems.Add ObjPtr(pobjTimer), pobjTimer.ID & "K"
    18. End Sub
    19.  
    20. Public Sub RemoveTimer(ByRef pobjTimer As APITimer)
    21. On Error GoTo ErrHandler
    22.     mcolItems.Remove pobjTimer.ID & "K"
    23.     KillTimer 0, pobjTimer.ID
    24.     pobjTimer.ID = 0
    25.     If mcolItems.Count = 0 Then
    26.         Set mcolItems = Nothing
    27.     End If
    28.     Exit Sub
    29. ErrHandler:
    30.    
    31. End Sub
    32.  
    33. Public Sub Timer_CBK(ByVal hWnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal SysTime As Long)
    34. Dim lngPointer  As Long
    35. Dim objTimer    As APITimer
    36. On Error GoTo ErrHandler
    37.     lngPointer = mcolItems.Item(idEvent & "K")
    38.     Set objTimer = PtrObj(lngPointer)
    39.     objTimer.RaiseTimerEvent
    40.     Set objTimer = Nothing
    41.     Exit Sub
    42. ErrHandler:
    43.  
    44. End Sub
    45.  
    46. Private Function PtrObj(ByVal Pointer As Long) As Object
    47. Dim objObject   As Object
    48.     CopyMemory objObject, Pointer, 4&
    49.     Set PtrObj = objObject
    50.     CopyMemory objObject, 0&, 4&
    51. End Function

    VB Code:
    1. Option Explicit
    2. '
    3. ' <APITimer.cls>
    4. '
    5. ' References:
    6. '
    7. '   - modTimers.bas
    8. '
    9. '
    10. Public Event Refresh()
    11. '
    12. Private mlngTimerID As Long
    13. '
    14.  
    15. Friend Property Let ID(ByVal plngValue As Long)
    16.     mlngTimerID = plngValue
    17. End Property
    18.  
    19. Friend Property Get ID() As Long
    20.     ID = mlngTimerID
    21. End Property
    22.  
    23. Public Sub StartTimer(ByVal Interval As Long)
    24.     If mlngTimerID = 0 Then
    25.         AddTimer Me, Interval
    26.     End If
    27. End Sub
    28.  
    29. Public Sub StopTimer()
    30.     If mlngTimerID > 0 Then
    31.         RemoveTimer Me
    32.     End If
    33. End Sub
    34.  
    35. Private Sub Class_Terminate()
    36.     StopTimer
    37. End Sub
    38.  
    39. Friend Sub RaiseTimerEvent()
    40.     RaiseEvent Refresh
    41. End Sub

    VB Code:
    1. Option Explicit
    2. '
    3. ' Class: cls_COMPort.cls
    4. '
    5. Public Event PortOpenDone(blnSuccessful As Boolean)
    6. Public Event PortCloseDone(intSuccessful As Integer)
    7. '
    8. Private Const GENERIC_READ = &H80000000
    9. Private Const GENERIC_WRITE = &H40000000
    10. Private Const FILE_FLAG_OVERLAPPED = &H40000000
    11. Private Const OPEN_EXISTING = 3
    12. '
    13. Private m_lngFileHandle As Long
    14. '
    15. Private WithEvents m_OpenTimer As CountDownTimer
    16. Private WithEvents m_CloseTimer As CountDownTimer
    17. '
    18.  
    19. Public Sub OpenPort_Async()
    20.     '
    21.     ' Asynchronous way to Open the COM port
    22.     '
    23.     Set m_OpenTimer = New CountDownTimer
    24.     m_OpenTimer.Interval = 100
    25.     m_OpenTimer.Increment
    26. End Sub
    27.  
    28. Public Sub ClosePort_Async()
    29.     '
    30.     ' Asynchronous way to Close the COM port
    31.     '
    32.     Set m_CloseTimer = New CountDownTimer
    33.     m_CloseTimer.Interval = 100
    34.     m_CloseTimer.Increment
    35. End Sub
    36.  
    37. Private Sub m_CloseTimer_Finsihed()
    38.     '
    39.     Dim intReturn As Integer
    40.     '
    41.     intReturn = ClosePort
    42.     RaiseEvent PortCloseDone(intReturn)
    43.     '
    44. End Sub
    45.  
    46. Private Sub m_OpenTimer_Finsihed()
    47.     '
    48.     Dim blnReturn As Boolean
    49.     '
    50.     blnReturn = OpenPort
    51.     RaiseEvent PortOpenDone(blnReturn)
    52.     '
    53. End Sub
    54.  
    55. Public Function OpenPort() As Boolean
    56.     '
    57.     ' Synchronous way to Open the COM port
    58.     '
    59.     m_lngFileHandle = CreateFile("\\.\COM1", GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0&, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0)
    60.     '
    61.     If m_lngFileHandle > 0 Then
    62.         OpenPort = True
    63.     End If
    64.     '
    65. End Function
    66.  
    67. Public Function ClosePort() As Integer
    68.     '
    69.     ' Synchronous way to Close the COM port
    70.     '
    71.     If CloseHandle(m_lngFileHandle) <> 0 Then
    72.         ClosePort = 0
    73.     Else
    74.         ClosePort = Err.LastDllError
    75.     End If
    76.     '
    77. End Function
    78.  
    79. Public Property Get FileHandle() As Long
    80.     FileHandle = m_lngFileHandle
    81. End Property

    VB Code:
    1. Option Explicit
    2. '
    3. ' <CountDownTimer.cls>
    4. '
    5. ' Author: Dave Sell
    6. '
    7. ' References:
    8. '
    9. '   - APITimer.cls
    10. '   - modTimers.bas
    11. '
    12. ' Notes:
    13. '
    14. '   - Intervals are in ms
    15. '
    16. Public Event Refresh()
    17. Public Event Finsihed()
    18. Public Event Started()
    19. '
    20. Private WithEvents m_Timer As APITimer
    21. '
    22. Private m_CountDownInterval As Long
    23. Private m_ActiveIntervals As Long
    24. Private m_blnRunning As Boolean
    25. '
    26.  
    27. Public Sub Reset()
    28.     Set m_Timer = New APITimer
    29.     m_ActiveIntervals = 0
    30.     m_blnRunning = False
    31. End Sub
    32.  
    33. Private Sub Class_Initialize()
    34.     Reset
    35.     m_CountDownInterval = 0
    36. End Sub
    37.  
    38. Public Property Let Interval(ByVal vNewValue As Variant)
    39.     m_CountDownInterval = vNewValue
    40.     If m_CountDownInterval < 0 Then
    41.         m_CountDownInterval = 0
    42.     End If
    43. End Property
    44.  
    45. Public Property Get Interval() As Variant
    46.     Interval = m_CountDownInterval
    47. End Property
    48.  
    49. Private Sub IncrementCountDown(lngIntervals As Long)
    50.     '
    51.     If Not m_blnRunning Then
    52.         m_Timer.StartTimer m_CountDownInterval
    53.         m_blnRunning = True
    54.         RaiseEvent Started
    55.     End If
    56.     '
    57.     m_ActiveIntervals = m_ActiveIntervals + lngIntervals
    58.     '
    59. End Sub
    60.  
    61. Public Sub Increment()
    62.     IncrementCountDown 1
    63. End Sub
    64.  
    65. Public Sub MultiIncrement(lngIntervals As Long)
    66.     IncrementCountDown lngIntervals
    67. End Sub
    68.  
    69. Private Sub m_Timer_Refresh()
    70.     '
    71.     If m_blnRunning = True Then
    72.         m_ActiveIntervals = m_ActiveIntervals - 1
    73.         RaiseEvent Refresh
    74.     End If
    75.     '
    76.     If m_ActiveIntervals = 0 Then
    77.         '
    78.         m_Timer.StopTimer
    79.         m_blnRunning = False
    80.         RaiseEvent Finsihed
    81.     End If
    82.     '
    83.     '
    84. End Sub
    85.  
    86. Public Property Get ActiveIntervals() As Variant
    87.     ActiveIntervals = m_ActiveIntervals
    88. End Property
    89.  
    90. Public Property Get Running() As Boolean
    91.     Running = m_blnRunning
    92. End Property
    Last edited by Dave Sell; Mar 16th, 2010 at 04:33 PM.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

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