Page 1 of 3

RS232 / Serial Scale Help

Posted: Monday 6th September 2021 8:32pm
by AndyGable
Hi everyone

I need some advice

I am trying to get a scanner scale to send the weight to my application (I can get the barcode number with no problem)

I was sent this by the Manafactor
Single Cable:
RS232 Parameters: 9600, Odd, 7, 1, (recommended) - No software control
Weight Request: STX, Address, FC, ETX, BCC
STX = ASCII S (83 dec, 53 hex)
Address = ASCII 1 (49 dec, 31 hex)
FC (function code) = ASCII 1 (49 dec, 31 hex)
ETX = ASCII CR (13 dec, 0D hex)
BCC = Optional
Weight Format: STX, 1, 1, xxxxx, ETX, BCC
Scale Cancel (optional - dependent on POS s/w): STX, 1, 2, ETX
So working from this I have come up with this code

Code: Select all

Public STX As String = Chr(&H53)
Public Address As String = Chr(&H31)
Public FunctionCode As String = Chr(&H31)
Public ETX As String = Chr(&H0D)

Public Sub btnRequestWeight_Click()

    Write #RS232Scanner, STX & Address & FunctionCode & ETX

    Sleep 0.025
    
    Try Read #RS232Scanner, Global.ScaleData, Lof(RS232Scanner)
    
    If Error Then
        Print "Error With Incoming Serial Data"
    Else
        Print "Nothing from Scale"
    Endif
    
    If Len(Global.ScaleData) > 0 Then
        Print "Raw Scale Weight : " & CStr(Global.ScaleData)
        ' CleanUpBarcodeData(CStr(Global.ScannerData))
    End If
End
but I keep getting the Error Mesage "nothing from Scale" print

am I doing something wrong? any help or gidance would be most welcomed

Re: RS232 / Serial Scale Help

Posted: Tuesday 7th September 2021 2:48pm
by thatbruce

Code: Select all

Try Read #RS232Scanner, Global.ScaleData, Lof(RS232Scanner)
    
    If Error Then
        Print "Error With Incoming Serial Data"
    Else
        Print "Nothing from Scale"
    Endif
If it is printing "Nothing from scale" then there is no error, i.e. the Read worked.

Re: RS232 / Serial Scale Help

Posted: Tuesday 7th September 2021 4:10pm
by AndyGable
So why is the scale not sending the weight?

I have even tried to send the Commands

Write #RS232Scanner, "S11\r"
Write #RS232Scanner, "S14\r"

But the scale still refuses to do anything (the scanner is still sending data to the PoS And this is on the same port)

Re: RS232 / Serial Scale Help

Posted: Wednesday 8th September 2021 6:01pm
by thatbruce
Because you haven't told us, I have to ask the obvious.

At the

Code: Select all

If Len(Global.ScaleData) > 0 Then
line have you inserted a breakpoint and inspected the value of the Global and the value of Global.ScaleData?

b

Re: RS232 / Serial Scale Help

Posted: Wednesday 8th September 2021 7:21pm
by AndyGable
I have done a print to the debug console and it is empty

Re: RS232 / Serial Scale Help

Posted: Thursday 9th September 2021 2:12am
by BruceSteers
I have no clue but..
I've seen issues using Sleep not Wait.

I've seen issues using CStr() not Str() to get values?
actually, looking at it, the Read command should read to a String variable
Global.ScaleData should be a string , so CStr() is not needed. CStr converts a non string value to a String,

Does the Write command need a terminating Linefeed?

Are you receiving ANY info from the device using Read? Ie, you are sure the com link is working.

Re: RS232 / Serial Scale Help

Posted: Thursday 9th September 2021 2:17am
by BruceSteers
I'd write it like this...

Public STX As String = Chr(&H53)
Public Address As String = Chr(&H31)
Public FunctionCode As String = Chr(&H31)
Public ETX As String = Chr(&H0D)

Public Sub btnRequestWeight_Click()

    Write #RS232Scanner, STX & Address & FunctionCode & ETX

    Wait 0.1  ' wait a little longer
     
    Try Read #RS232Scanner, Global.ScaleData, Lof(RS232Scanner)
    
    If Error Then
        Print "Error With Incoming Serial Data\n";; Error.Text
    Else
    
      If Global.ScaleData Then
        Print "Raw Scale Weight :";; Global.ScaleData
        ' CleanUpBarcodeData(CStr(Global.ScannerData))
      Else
        Print "Nothing from Scale"
      Endif ' no data if

    EndIf ' error if
End


Re: RS232 / Serial Scale Help

Posted: Thursday 9th September 2021 2:45am
by AndyGable
BruceSteers wrote: Thursday 9th September 2021 2:12am I have no clue but..
I've seen issues using Sleep not Wait.

I've seen issues using CStr() not Str() to get values?
actually, looking at it, the Read command should read to a String variable
Global.ScaleData should be a string , so CStr() is not needed. CStr converts a non string value to a String,

Does the Write command need a terminating Linefeed?

Are you receiving ANY info from the device using Read? Ie, you are sure the com link is working.
Yea I can get the barcodes from the device (it is a combined scanner / scale unit) and it sends the Barcode number out with no problem

Re: RS232 / Serial Scale Help

Posted: Thursday 9th September 2021 2:45am
by AndyGable
BruceSteers wrote: Thursday 9th September 2021 2:17am I'd write it like this...

Public STX As String = Chr(&H53)
Public Address As String = Chr(&H31)
Public FunctionCode As String = Chr(&H31)
Public ETX As String = Chr(&H0D)

Public Sub btnRequestWeight_Click()

    Write #RS232Scanner, STX & Address & FunctionCode & ETX

    Wait 0.1  ' wait a little longer
     
    Try Read #RS232Scanner, Global.ScaleData, Lof(RS232Scanner)
    
    If Error Then
        Print "Error With Incoming Serial Data\n";; Error.Text
    Else
    
      If Global.ScaleData Then
        Print "Raw Scale Weight :";; Global.ScaleData
        ' CleanUpBarcodeData(CStr(Global.ScannerData))
      Else
        Print "Nothing from Scale"
      Endif ' no data if

    EndIf ' error if
End

Just tried that and I am sorry to say still nothing coming from the scale

Not sure if this will make a differance but this is my code i am using to open the comport (I asuume it is open to talk both ways)
RS232Scanner = New SerialPort As "RS232Scanner"
   
    'Set up the Serial Device so it should start talking
    With RS232Scanner
        .PortName = "/dev/ttyS0"
        .Speed = "9600"
        .Parity = SerialPort.Odd
        .DataBits = SerialPort.bits7
        .StopBits = SerialPort.bits1
        .FlowControl = SerialPort.None
    End With 
    
    Try RS232Scanner.Open(3)
        If Error Then 
            Global.AddToListBox("Sorry Unable to connect to Scanner\nPlease check settings and try again")    
        Else 
            If RS232Scanner.Status = Net.Active Then 
                Global.AddToListBox("Ready to scan barcode")
            End If
        End If
End

Re: RS232 / Serial Scale Help

Posted: Thursday 9th September 2021 11:48am
by BruceSteers
How about setting up a read handler?

looking at the page http://gambaswiki.org/wiki/comp/gb.net/serialport it has a number of other Events

Note: the below method is expecting the device to send text strings not binary values.
    Try RS232Scanner.Open(3)
        If Error Then 
            Global.AddToListBox("Sorry Unable to connect to Scanner\nPlease check settings and try again")    
        Else 
            If RS232Scanner.Status = Net.Active Then 
                Global.AddToListBox("Ready to scan barcode")
                
                ' you may need to enable watching...
                RS232Scanner.Watch(gb.Read, True)

                ' Attach the stream to "Scanner" in this class.
               Object.Attach(RS232Scanner, Me, "Scanner")
            End If
        End If


' Then add a read event method for Scanner  (Ps, there are other events listed)
' PS. Sorry i was wrong about using Read giving a String.  Using stream.ReadLine() gives a string Read is binary :-\

Public Sub Scanner_Read()
Dim sData As String
  While Not Eof(RS232Scanner)
    sData = RS232Scanner.ReadLine()
    debug Quote(sData) ' lets see the result with escape chars in case the console is not showing data
    Print "Recieved from Scanner:";; sData
  Wend
End


I'd say your problem is probably either read/write is not set up properly or the command being sent is not the right format.
It's very very difficult for us to guess your problem with tiny snippets of code and none of us having the device you have.

I'd say you need the following..
More study on the differences between vb code and other examples you're finding and gambas code so your conversions look better.

More study on opening/reading/writing/watching streams
a bit more research to find some example code that uses your device. any programming language code should help give insights into the correct operation of the device.

Does it need a LF?
Write #RS232Scanner, STX & Address & FunctionCode & ETX & gb.Lf

Do the commands need to be sent individually?
Write #RS232Scanner, STX
Write #RS232Scanner, Address
Write #RS232Scanner, FunctionCode
Write #RS232Scanner, ETX

You are just going to have to play around with it till you crack it :)

PS. I do not have a device to test the above code so have no idea if it will work , it's just an example.