serial comm with vba in ms-access

V

Thread Starter

vbdummy

part of the code in an access file. try to talk to a scale via rs232 on my computer. use one command button to enable the serial port, another one to disable, and the third one is used to send a command and get the feedback from the scale.

i can use this code to read the value from the port,but have to click the button twice to get the current value. actually, the first touch on the command button gets the previous value, then the current value can pop out on the second click.

i use NETCommOCX to replace the mscomm.ocx, since i do have any vb installed on my PC. other commands can be executed just fine, but this one.

anyone finds out any bugs in my program, please point me out to the right direction. thanks so much!


Private Sub Command75_Click()
'Open Serial Port and configure the settings

If NETComm6.PortOpen Then
MsgBox "Serial Port is already Open", vbOKOnly, "Port Status"
Else
NETComm6.CommPort = 2 'set the port number
NETComm6.PortOpen = True 'open port
NETComm6.RThreshold = 1
NETComm6.InBufferCount = 0 'Flush the Buffer
NETComm6.InputLen = 0 ' Tell the control to read entire buffer
End If
End Sub

Private Sub Command76_Click()
'Close Serial Port

If NETComm6.PortOpen Then
NETComm6.CommPort = 2
NETComm6.InBufferCount = 0
NETComm6.PortOpen = False
Else
MsgBox "Serial Port is already closed", vbOKOnly, "Port Status"
End If
End Sub

Private Sub Command77_Click()
'send the Print command and read the weight value.
NETComm6.Output = "p" & Chr(13) 'send PRINT command to the scale.

Dim Buffer As String 'define Buffer
Buffer$ = Buffer$ & NETComm6.InputData 'wait for the data come into the serail port
WtInfo = Buffer
End Sub


*WtInfo is a textbox in the form.
 
T

Todd Hernandez

i don't think there is a problem with your code. one thing that i typically do for serial comms is to define a dummy or holder variable and set it equal to the input. then i get the input again and put it in the object that i actually want to have the value e.g.

dim holder as string
dim buffer as string

holder = NETComm6.InputData
buffer = buffer & NETComm6.InputData
 
Top