mbelew
Sep 8th, 2001, 06:43 PM
Im using the CreateFile and WritFile API call to send data to a serial device. The Code for my function containing the Create file call is:
Private Function OpenPort1(ByVal strPort As String, ByVal lngBaudRate As String, ByVal lngDataBit As Long) As Long
Dim pDCB As DCB
Dim lpPort As String
'// Create Comm Name Buffer
lpPort = "COM2" + strPort + vbNullChar
'// Close the current opened Comm Port (If any)
If hComm1 > 0 Then CloseHandle (hComm1)
'// Open selected comm port
hComm1 = CreateFile(lpPort, _
GENERIC_READ Or GENERIC_WRITE, _
0, _
ByVal 0, _
OPEN_EXISTING, _
FILE_FLAG_NO_BUFFERING, _
0)
If hComm1 <> INVALID_HANDLE_VALUE Then
pDCB.DCBlength = Len(pDCB)
'// Retrieve default Comm port settings
GetCommState hComm1, pDCB
'// Configure new Comm port settings
With pDCB
.BaudRate = lngBaudRate
.Parity = NOPARITY
.ByteSize = lngDataBit
.StopBits = 2
.EofChar = 0
.ErrorChar = 0
.EvtChar = 0
.fBitFields = 20625
.XoffChar = 0
.XoffLim = 0
.XonChar = 0
.XonLim = 0
End With
'// Set new configure Comm port settings
If SetCommState(hComm1, pDCB) = 0 Then
CloseHandle (hComm1)
OpenPort1 = 0
MsgBox "Fail to configure serial port COM11!", vbExclamation + vbOKOnly, "Error"
Else
OpenPort1 = hComm1
End If
Else
CloseHandle (hComm1)
OpenPort1 = 0
End If
End Function
and the function containing the WriteFile call is :
Private Sub Write1Port(ByVal strData As String)
Dim dwByteWrite As Long
Dim Sz As Long, Idx As Long
Dim Bytes() As Byte
'// Create & Convert str into array of Byte
Sz = Len(strData)
ReDim Bytes(Sz) As Byte
For Idx = 1 To Sz
Bytes(Idx - 1) = Asc(Mid$(strData, Idx, 1))
Next
'// Write data into Open Comm Port
If hComm1 <> INVALID_HANDLE_VALUE Then
WriteFile hComm1, _
Bytes(0), _
UBound(Bytes), _
dwByteWrite, _
ByVal 0&
Else
MsgBox "Cannot communicate through COM11", vbExclamation + vbOKOnly, "Error"
End If
Erase Bytes
End Sub
I only want to send one byte at a time to the device such as:
Write1Port &HC7
What changes can I make to send only the single byte to the serial device?
Private Function OpenPort1(ByVal strPort As String, ByVal lngBaudRate As String, ByVal lngDataBit As Long) As Long
Dim pDCB As DCB
Dim lpPort As String
'// Create Comm Name Buffer
lpPort = "COM2" + strPort + vbNullChar
'// Close the current opened Comm Port (If any)
If hComm1 > 0 Then CloseHandle (hComm1)
'// Open selected comm port
hComm1 = CreateFile(lpPort, _
GENERIC_READ Or GENERIC_WRITE, _
0, _
ByVal 0, _
OPEN_EXISTING, _
FILE_FLAG_NO_BUFFERING, _
0)
If hComm1 <> INVALID_HANDLE_VALUE Then
pDCB.DCBlength = Len(pDCB)
'// Retrieve default Comm port settings
GetCommState hComm1, pDCB
'// Configure new Comm port settings
With pDCB
.BaudRate = lngBaudRate
.Parity = NOPARITY
.ByteSize = lngDataBit
.StopBits = 2
.EofChar = 0
.ErrorChar = 0
.EvtChar = 0
.fBitFields = 20625
.XoffChar = 0
.XoffLim = 0
.XonChar = 0
.XonLim = 0
End With
'// Set new configure Comm port settings
If SetCommState(hComm1, pDCB) = 0 Then
CloseHandle (hComm1)
OpenPort1 = 0
MsgBox "Fail to configure serial port COM11!", vbExclamation + vbOKOnly, "Error"
Else
OpenPort1 = hComm1
End If
Else
CloseHandle (hComm1)
OpenPort1 = 0
End If
End Function
and the function containing the WriteFile call is :
Private Sub Write1Port(ByVal strData As String)
Dim dwByteWrite As Long
Dim Sz As Long, Idx As Long
Dim Bytes() As Byte
'// Create & Convert str into array of Byte
Sz = Len(strData)
ReDim Bytes(Sz) As Byte
For Idx = 1 To Sz
Bytes(Idx - 1) = Asc(Mid$(strData, Idx, 1))
Next
'// Write data into Open Comm Port
If hComm1 <> INVALID_HANDLE_VALUE Then
WriteFile hComm1, _
Bytes(0), _
UBound(Bytes), _
dwByteWrite, _
ByVal 0&
Else
MsgBox "Cannot communicate through COM11", vbExclamation + vbOKOnly, "Error"
End If
Erase Bytes
End Sub
I only want to send one byte at a time to the device such as:
Write1Port &HC7
What changes can I make to send only the single byte to the serial device?