I would like to capture the weight through the signal send out from the weighing machine. The weighing machine is connecting to the PC by using com port.
Can I have a sample code for capturing the signal by using MSComm or maybe you have better suggestion on this matter. Thanks for your help.
The following sample reads from the com port looking for temperature data and displaying it on a label control.
For your application you would have to change the MScomm1 event code to look for whatever your machine sends, then parse it and format accordingly.
The Static Buffer within that procedure is required since the data might not arrive complete, hence you have to save it until you can determine it's safe to process.
option Explicit
Dim Temperature as Single
private Sub MScomm1_OnComm()
static Buffer as string
Dim i as Integer, ts as string
on error resume next
Select Case MScomm1.CommEvent
' Handle each event or error by placing ' code below each case statement
' Errors
Case comEventBreak ' A Break was received.
Case comEventFrame ' Framing error
Case comEventOverrun ' Data Lost.
Case comEventRxOver ' Receive buffer overflow.
Case comEventRxParity ' Parity error.
Case comEventTxFull ' Transmit buffer full.
Case comEventDCB ' Unexpected error retrieving DCB]
' Events
Case comEvCD ' Change in the CD line.
Case comEvCTS ' Change in the CTS line.
Case comEvDSR ' Change in the DSR line.
Case comEvRing ' Change in the Ring Indicator.
Case comEvReceive ' Received RThreshold # of chars
com.InputLen = 0
'retrieve all chars
Buffer = Buffer & MScomm1.input
'search for CH01 data (look for what the particular device sends)
i = InStr(Buffer, "CH01:")
'if string found and the temp data is there then grab it
Do While i > 0 And len(Buffer) > (i + 6)
'grab the numeric part of the data
ts = mid(Buffer, i + 5, 6)
'convert to number
Temperature = Val(Trim(ts))
'display Temp and/or do whatever
lblTemp.Caption = Format(Temperature, "##0.0 C")
'discard used part of string
Buffer = mid(Buffer, i + 11)
'see if more data is available for CH01
i = InStr(Buffer, "CH01:")
Loop
Case comEvSend ' There are SThreshold number of chars in the TX buffer
Case comEvEOF ' An EOF character was found in the input stream
End Select
End Sub
private Sub Form_Load()
on error resume next
MScomm1.CommPort = 2
MScomm1.Settings = "9600,n,8,1"
'no handshaking
MScomm1.Handshaking = comNone
'ample buffer size
MScomm1.InBufferSize = 2048
'fire the RECEIVE event when 1 or more chars waiting
MScomm1.RThreshold = 1
'don't fire the SEND event
MScomm1.SThreshold = 0
'open port
MScomm1.PortOpen = true
If Not MScomm1.PortOpen then
MsgBox "Unable to open COM" & MScomm1.CommPort, vbCritical, "error"
End
End If
End Sub
private Sub Form_Unload(Cancel as Integer)
on error resume next
'don't leave port open, release it
MScomm1.PortOpen = false
End Sub
Hii markus,
Can u please send a sample code,that will b useful for me too for some reference.
regards
Naseem
Hello Naseem!
Just so you know, this thread is quite OLD - in future it would be best to start your own thread, based on your problem.
Here's a small pice of code that might be useful to you
Code:
Private Sub Form_Load ()
' Buffer to hold input string
Dim Instring As String
' Use COM1.
MSComm1.CommPort = 1
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read entire buffer when Input
' is used.
MSComm1.InputLen = 0
' Open the port.
MSComm1.PortOpen = True
' Send the attention command to the modem.
MSComm1.Output = "ATV1Q0" & Chr$(13) ' Ensure that
' the modem responds with "OK".
' Wait for data to come back to the serial port.
Do
DoEvents
Buffer$ = Buffer$ & MSComm1.Input
Loop Until InStr(Buffer$, "OK" & vbCRLF)
' Read the "OK" response data in the serial port.
' Close the serial port.
MSComm1.PortOpen = False
End Sub
I've included a sample as well - it's one of MSDN's examples
Enjoy!
Last edited by HanneSThEGreaT; December 19th, 2008 at 03:00 AM.
?? Did nor send you any sample ... first had no time and
then forgot ... sorry...
I appended the sample project here. It's quick and dirty, but you
should be able to see how to use the MSComm control. You can
use a terminal program to test or a Null-modem (send line of the
port connected to the receive line). If you use a Null-modem,
you should see in the second text box what you have entered
in the first one if you press the send button. The binary option
is for integer numbers only.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.