Results 1 to 3 of 3

Thread: Opening a serial Connection

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    54

    Opening a serial Connection

    Can anyone tell me how to open a serial port using the Create File API please?

    Thanks

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Opening a serial Connection

    Hi,

    There's an example here: http://msdn.microsoft.com/library/de...s_resource.asp
    which I've attempted to convert to VB:
    VB Code:
    1. Option Explicit
    2. Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
    3.                 (ByVal lpFileName As String, _
    4.                  ByVal dwDesiredAccess As Long, _
    5.                  ByVal dwShareMode As Long, _
    6.                  lpSecurityAttributes As Long, _
    7.                  ByVal dwCreationDisposition As Long, _
    8.                  ByVal dwFlagsAndAttributes As Long, _
    9.                  ByVal hTemplateFile As Long) As Long
    10.                  
    11. Private Declare Function GetCommState Lib "kernel32" _
    12.                 (ByVal nCid As Long, _
    13.                  lpDCB As DCB) As Long
    14.                  
    15. Private Declare Function SetCommState Lib "kernel32" _
    16.                 (ByVal hCommDev As Long, _
    17.                  lpDCB As DCB) As Long
    18. '
    19. ' Create File Constants
    20. '
    21. Private Const GENERIC_READ = &H80000000
    22. Private Const GENERIC_WRITE = &H40000000
    23. Private Const OPEN_EXISTING = 3
    24. Private Const INVALID_HANDLE_VALUE = -1
    25. '
    26. ' Baud Rate Constants
    27. '
    28. Private Const CBR_110 = 110
    29. Private Const CBR_115200 = 115200
    30. Private Const CBR_1200 = 1200
    31. Private Const CBR_128000 = 128000
    32. Private Const CBR_14400 = 14400
    33. Private Const CBR_19200 = 19200
    34. Private Const CBR_2400 = 2400
    35. Private Const CBR_256000 = 256000
    36. Private Const CBR_300 = 300
    37. Private Const CBR_38400 = 38400
    38. Private Const CBR_4800 = 4800
    39. Private Const CBR_56000 = 56000
    40. Private Const CBR_57600 = 57600
    41. Private Const CBR_600 = 600
    42. Private Const CBR_9600 = 9600
    43. '
    44. ' Parity and stop bit(s) Constants
    45. '
    46. Private Const NOPARITY = 0
    47. Private Const ONESTOPBIT = 0
    48.  
    49. Private Type DCB
    50.         DCBlength As Long
    51.         BaudRate As Long
    52.         fBitFields As Long 'See Comments in Win32API.Txt
    53.         wReserved As Integer
    54.         XonLim As Integer
    55.         XoffLim As Integer
    56.         ByteSize As Byte
    57.         Parity As Byte
    58.         StopBits As Byte
    59.         XonChar As Byte
    60.         XoffChar As Byte
    61.         ErrorChar As Byte
    62.         EofChar As Byte
    63.         EvtChar As Byte
    64.         wReserved1 As Integer 'Reserved; Do Not Use
    65. End Type
    66.  
    67. Private Sub Open_Comm_Click()
    68. Dim lngHCom As Long
    69. Dim udtDCB As DCB
    70. Dim lngReturn As Long
    71. Dim strComPort As String
    72. strComPort = "COM1"
    73. '
    74. ' Create a handle to the Commport
    75. '
    76. lngHCom = CreateFile(strComPort, _
    77.             GENERIC_READ Or GENERIC_WRITE, _
    78.             0, _
    79.             0, _
    80.             OPEN_EXISTING, _
    81.             0, _
    82.             0)
    83. If lngHCom = INVALID_HANDLE_VALUE Then
    84.     MsgBox "Unable to Open Communications Port " & strComPort & vbCrLf _
    85.             & Err.LastDllError
    86. Else
    87.     '
    88.     ' Obtain the current settings for the comm port
    89.     '
    90.     lngReturn = GetCommState(lngHCom, udtDCB)
    91.     If lngReturn = 0 Then
    92.         MsgBox "Unable to obtain current CommState of " & strComPort & vbCrLf _
    93.             & Err.LastDllError
    94.     Else
    95.         '
    96.         ' Set up the new settings
    97.         '
    98.         udtDCB.BaudRate = CBR_9600
    99.         udtDCB.ByteSize = 8
    100.         udtDCB.Parity = NOPARITY
    101.         udtDCB.StopBits = ONESTOPBIT
    102.         lngReturn = SetCommState(lngHCom, udtDCB)
    103.         If lngReturn = 0 Then
    104.             MsgBox "Unable to set CommState of " & strComPort & vbCrLf _
    105.                 & Err.LastDllError
    106.         Else
    107.             MsgBox "Successfully Opened and Configured " & strComPort
    108.         End If
    109.     End If
    110. End If
    111. End Sub
    I haven't got a Comm port on this PC so I can't test it. Hopefully it's a good starter for 10 for you.
    Regards
    Doug

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    54

    Re: Opening a serial Connection

    Yes, thats a great starter for 10.

    Thanks for all your trouble.

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