Can anyone tell me how to open a serial port using the Create File API please?
Thanks
Printable View
Can anyone tell me how to open a serial port using the Create File API please?
Thanks
Hi,
There's an example here: http://msdn.microsoft.com/library/de...s_resource.asp
which I've attempted to convert to VB:
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.VB Code:
Option Explicit Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _ (ByVal lpFileName As String, _ ByVal dwDesiredAccess As Long, _ ByVal dwShareMode As Long, _ lpSecurityAttributes As Long, _ ByVal dwCreationDisposition As Long, _ ByVal dwFlagsAndAttributes As Long, _ ByVal hTemplateFile As Long) As Long Private Declare Function GetCommState Lib "kernel32" _ (ByVal nCid As Long, _ lpDCB As DCB) As Long Private Declare Function SetCommState Lib "kernel32" _ (ByVal hCommDev As Long, _ lpDCB As DCB) As Long ' ' Create File Constants ' Private Const GENERIC_READ = &H80000000 Private Const GENERIC_WRITE = &H40000000 Private Const OPEN_EXISTING = 3 Private Const INVALID_HANDLE_VALUE = -1 ' ' Baud Rate Constants ' Private Const CBR_110 = 110 Private Const CBR_115200 = 115200 Private Const CBR_1200 = 1200 Private Const CBR_128000 = 128000 Private Const CBR_14400 = 14400 Private Const CBR_19200 = 19200 Private Const CBR_2400 = 2400 Private Const CBR_256000 = 256000 Private Const CBR_300 = 300 Private Const CBR_38400 = 38400 Private Const CBR_4800 = 4800 Private Const CBR_56000 = 56000 Private Const CBR_57600 = 57600 Private Const CBR_600 = 600 Private Const CBR_9600 = 9600 ' ' Parity and stop bit(s) Constants ' Private Const NOPARITY = 0 Private Const ONESTOPBIT = 0 Private Type DCB DCBlength As Long BaudRate As Long fBitFields As Long 'See Comments in Win32API.Txt wReserved As Integer XonLim As Integer XoffLim As Integer ByteSize As Byte Parity As Byte StopBits As Byte XonChar As Byte XoffChar As Byte ErrorChar As Byte EofChar As Byte EvtChar As Byte wReserved1 As Integer 'Reserved; Do Not Use End Type Private Sub Open_Comm_Click() Dim lngHCom As Long Dim udtDCB As DCB Dim lngReturn As Long Dim strComPort As String strComPort = "COM1" ' ' Create a handle to the Commport ' lngHCom = CreateFile(strComPort, _ GENERIC_READ Or GENERIC_WRITE, _ 0, _ 0, _ OPEN_EXISTING, _ 0, _ 0) If lngHCom = INVALID_HANDLE_VALUE Then MsgBox "Unable to Open Communications Port " & strComPort & vbCrLf _ & Err.LastDllError Else ' ' Obtain the current settings for the comm port ' lngReturn = GetCommState(lngHCom, udtDCB) If lngReturn = 0 Then MsgBox "Unable to obtain current CommState of " & strComPort & vbCrLf _ & Err.LastDllError Else ' ' Set up the new settings ' udtDCB.BaudRate = CBR_9600 udtDCB.ByteSize = 8 udtDCB.Parity = NOPARITY udtDCB.StopBits = ONESTOPBIT lngReturn = SetCommState(lngHCom, udtDCB) If lngReturn = 0 Then MsgBox "Unable to set CommState of " & strComPort & vbCrLf _ & Err.LastDllError Else MsgBox "Successfully Opened and Configured " & strComPort End If End If End If End Sub
Regards
Doug
Yes, thats a great starter for 10.
Thanks for all your trouble. :wave: