Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unable to Read the Serial Port

Status
Not open for further replies.

muraliambt

Programmer
Jan 22, 2002
75
0
0
US
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
 
You don't appear to have configured the serial port itself (baud rate, bits, parity, handshaking, stop bits etc.) and from memory most scales are a bit unusual in this respect so the default values are very unlikely to work.

Get hold of one of the many free serial port ActiveX components and set the port properties to match the documentation that came with the scale. Most of these components will raise an event when something arrives so the programming becomes very simple.



Bob Boffin
 
You can use the MSComm control. You will have to add the component to your project. It would be the Microsoft MSComm Control 6.0.

Then you can use MSComm1.Output and MSComm1.Input to read and write from your file.

HyperEngineer
If it ain't broke, it probably needs improvement.
 
Debugging these things is a lot easier if you connect a null modem between two ports and use a program like HyperTerminal to do the other side of the conversion.
 
Also what Bob said about setting your parameters. Here's a snippet I used to set up a comm port to an electronic device.
Code:
   ' set the comm port
   With MSComm1
      .CommPort = 3
      .DTREnable = True
      .Handshaking = comNone
      .InBufferSize = 256
      .InputLen = 1
      .OutBufferSize = 128
      .RThreshold = 0
      .Settings = "57600,N,8,1"
      .SThreshold = 0
      .PortOpen = True
   End With



HyperEngineer
If it ain't broke, it probably needs improvement.
 
Thanks guys, by inserting the comm port setting it worked fine and i also tried putting the mscomm control in the form, it worked fine.

I have choosen to use MSCOMM control method, it is very easy to use but i have one small problem in that.

when i put the weight in the Scale it takes some seconds to display the values, it is not displaying immediately.

If i reduce the MSComm1.RThreshold to 1 then i am getting only 0.00 in the Text Box.


Following is the code i used.
MSComm1.CommPort = 1
MSComm1.Settings = "9600,n,8,1"
MSComm1.PortOpen = True
MSComm1.RThreshold = 300


Thanks,
Muraliambt.

 
Hi guys,

It is working fine now, even the reading from scale is fast.

Thanks
Muraliambt.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top