Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get Count as output from weighing machine using com port

My Port Connect Event Code is Below:

VB
Private Sub cmdConnect_Click()
If cmdConnect.Caption = "Connect  Machine" Then
    Dim str As String
    str = Combo1.Text
    Dim i As Integer, j As String
    i = Mid(str, 4, 1)
    MSComm1.CommPort = i

        MSComm1.PortOpen = False
        MSComm1.PortOpen = True
        MsgBox "Communication Created Successfully"
        MSComm1.Settings = "9600,N,8,1"
        MSComm1.Output = "0" + Chr(13)
        MSComm1.DTREnable = True
        MSComm1.Handshaking = 1
        MSComm1.NullDiscard = True
        MSComm1.RTSEnable = True
        MSComm1.EOFEnable = True
        MSComm1.RThreshold = 12
        MSComm1.SThreshold = 1
        MSComm1.InputMode = comInputModeText 
        cmdConnect.BackColor = &HC0&
        cmdConnect.Caption = "Disconnect Machine"


My OnComm Event Code is Here:

VB
     <pre>Dim str As String, res As Variant, com As String, txt As String, i As Integer
'MSComm1.Settings = "9600,N,8,1"
'MSComm1.Output = "0" + Chr(13)
MSComm1.DTREnable = True
MSComm1.Handshaking = 1
MSComm1.NullDiscard = True
MSComm1.RTSEnable = True
MSComm1.EOFEnable = True
MSComm1.RThreshold = 15
MSComm1.SThreshold = 1
'MSComm1.InputLen = 4
MSComm1.InputMode = comInputModeText
Select Case MSComm1.CommEvent
  Case comEvReceive

          txtDisplay.Text = ""
      Do While MSComm1.InBufferCount > 0
             'txtDisplay.Text = txtDisplay.Text + Chr(13) + MSComm1.Input
              txtDisplay.Text = txtDisplay.Text + vbNewLine + MSComm1.Input
      Loop
      If OptAuto.Value = True Then
            ReportPrint
      ElseIf OptManu.Value = True Then
            cmdPrint.Visible = True
      End If
  Case Is > 1000
      MsgBox "Unknown Weight"
End Select



My Output Format is
Same Weight Come in two Different Format
First Output Like
0001.600013660S
here count is 1366
Second time Output like 0.0001.60001366 here count is 1366
some other weight give output like this
2.1852.185 here count is 185


Please tell me it's the correct coding
or tell me correct coding to receive com port data in uniform format
i want to split count from this output here count come in last digit of every single output
Posted
Comments
CPallini 12-Aug-14 4:57am    
You have to check out the scale documentation in order to correctly handle incoming data.

1 solution

:sigh:

Com ports - particularly serial ports - don't work like that. It takes time to receive the data - in your case a maximum of about 960 characters per second because you have the baud rate set to 9600 - and it doesn't try to "frame" or "package" the data at all because it doesn't know what data it will get.

All it is is a stream of characters, and it passes them to you as individual characters. So sometimes, you get 4 characters and sometimes you get the ones you didn't look at last time plus a bunch of new ones. Just reading until no more are available doesn't help - because it takes real time to receive each character!

Look at exactly what the machine is supposed to give you: it will almost certainly be the data plus a terminator such as newline. So you read the data, and save it until a terminator arrives. Then you can look at the whole number and convert it into a valid value, not before.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900