muraliambt
Programmer
Hi All,
Can anybody tell me what is the problem with the following code. When i run, it gives me BytesRead :0
I Have electronic scale which is connected to My computer and scale is configured to send the output to computer also.
SerialPort handle is OK ( hSerialPort is not -1 ).
Thanks.
Muraliambt.
Option Explicit
Private Const GENERIC_READ As Long = &H80000000
Private Const GENERIC_WRITE As Long = &H40000000
Private Const OPEN_EXISTING As Long = 3
Private Const FILE_ATTRIBUTE_NORMAL As Long = &H80
Private Const NOPARITY As Long = 0
Private Const ONESTOPBIT As Long = 0
''
''Private Type SECURITY_ATTRIBUTES
'' nLength As Long
'' lpSecurityDescriptor As Long
'' bInheritHandle As Long
''End Type
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long _
) As Long
Private Declare Function WriteFile Lib "kernel32" ( _
ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToWrite As Long, _
ByRef lpNumberOfBytesWritten As Long, _
ByVal lpOverlapped As Long _
) As Boolean
Private Declare Function ReadFile Lib "kernel32" ( _
ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToRead As Long, _
ByRef lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Long _
) As Boolean
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Boolean
Private Sub Command1_Click()
Dim hSerialPort As Long
''Dim sa As SECURITY_ATTRIBUTES
Dim success As Boolean
Dim Buffer As Byte
Dim BytesWritten As Long
Dim BytesRead As Long
Dim I As Integer
'Create NULL Security Attributes
'' With sa
'' .bInheritHandle = False
'' .lpSecurityDescriptor = 0
'' .nLength = Len(sa)
'' End With
' Obtain a handle to the COM1 serial port.
''hSerialPort = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0&, sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
hSerialPort = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
' Verify that the obtained handle is valid.
If hSerialPort = -1 Then
MsgBox ("PROBLEM")
Else
MsgBox ("Reading OK")
End If
'' Write data to COM1.
''Success = WriteFile(hSerialPort, Buffer, UBound(Buffer) - 1, BytesWritten, 0)
''If Success = False Then
'' 'PROBLEM
'' Debug.Print "PROBLEM"
''End If
' Read data from COM1.
'buffer(LBound(buffer)) = 0
success = ReadFile(hSerialPort, Buffer, BytesWritten, BytesRead, 0)
If success Then
'Form1.Text = ("BytesRead: " & BytesRead)
GrossWeight.Text = ("BytesRead: " & BytesRead)
For I = LBound(Buffer) To LBound(Buffer) + BytesRead - 1
GrossWeight.Text = (I & " | " & Buffer(I)) '& " | " & chr(Buffer(i)))
Next
Else
MsgBox ("PROBLEM")
End If
success = CloseHandle(hSerialPort)
If success Then
MsgBox ("Successully Released the Handle")
Else
MsgBox ("Unable to Release the Handle")
End If
End Sub
Can anybody tell me what is the problem with the following code. When i run, it gives me BytesRead :0
I Have electronic scale which is connected to My computer and scale is configured to send the output to computer also.
SerialPort handle is OK ( hSerialPort is not -1 ).
Thanks.
Muraliambt.
Option Explicit
Private Const GENERIC_READ As Long = &H80000000
Private Const GENERIC_WRITE As Long = &H40000000
Private Const OPEN_EXISTING As Long = 3
Private Const FILE_ATTRIBUTE_NORMAL As Long = &H80
Private Const NOPARITY As Long = 0
Private Const ONESTOPBIT As Long = 0
''
''Private Type SECURITY_ATTRIBUTES
'' nLength As Long
'' lpSecurityDescriptor As Long
'' bInheritHandle As Long
''End Type
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" _
(ByVal lpFileName As String, _
ByVal dwDesiredAccess As Long, _
ByVal dwShareMode As Long, _
ByVal lpSecurityAttributes As Long, _
ByVal dwCreationDisposition As Long, _
ByVal dwFlagsAndAttributes As Long, _
ByVal hTemplateFile As Long _
) As Long
Private Declare Function WriteFile Lib "kernel32" ( _
ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToWrite As Long, _
ByRef lpNumberOfBytesWritten As Long, _
ByVal lpOverlapped As Long _
) As Boolean
Private Declare Function ReadFile Lib "kernel32" ( _
ByVal hFile As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfBytesToRead As Long, _
ByRef lpNumberOfBytesRead As Long, _
ByVal lpOverlapped As Long _
) As Boolean
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Boolean
Private Sub Command1_Click()
Dim hSerialPort As Long
''Dim sa As SECURITY_ATTRIBUTES
Dim success As Boolean
Dim Buffer As Byte
Dim BytesWritten As Long
Dim BytesRead As Long
Dim I As Integer
'Create NULL Security Attributes
'' With sa
'' .bInheritHandle = False
'' .lpSecurityDescriptor = 0
'' .nLength = Len(sa)
'' End With
' Obtain a handle to the COM1 serial port.
''hSerialPort = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0&, sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
hSerialPort = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
' Verify that the obtained handle is valid.
If hSerialPort = -1 Then
MsgBox ("PROBLEM")
Else
MsgBox ("Reading OK")
End If
'' Write data to COM1.
''Success = WriteFile(hSerialPort, Buffer, UBound(Buffer) - 1, BytesWritten, 0)
''If Success = False Then
'' 'PROBLEM
'' Debug.Print "PROBLEM"
''End If
' Read data from COM1.
'buffer(LBound(buffer)) = 0
success = ReadFile(hSerialPort, Buffer, BytesWritten, BytesRead, 0)
If success Then
'Form1.Text = ("BytesRead: " & BytesRead)
GrossWeight.Text = ("BytesRead: " & BytesRead)
For I = LBound(Buffer) To LBound(Buffer) + BytesRead - 1
GrossWeight.Text = (I & " | " & Buffer(I)) '& " | " & chr(Buffer(i)))
Next
Else
MsgBox ("PROBLEM")
End If
success = CloseHandle(hSerialPort)
If success Then
MsgBox ("Successully Released the Handle")
Else
MsgBox ("Unable to Release the Handle")
End If
End Sub