RS232 / Serial Scale Help

Post your Gambas programming questions here.
AndyGable
Posts: 140
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

RS232 / Serial Scale Help

Post by AndyGable » Monday 06th September 2021 8:32pm

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

User avatar
thatbruce
Posts: 19
Joined: Saturday 04th September 2021 11:29pm

Re: RS232 / Serial Scale Help

Post by thatbruce » Tuesday 07th September 2021 2:48pm

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.

AndyGable
Posts: 140
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

Re: RS232 / Serial Scale Help

Post by AndyGable » Tuesday 07th September 2021 4:10pm

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)

User avatar
thatbruce
Posts: 19
Joined: Saturday 04th September 2021 11:29pm

Re: RS232 / Serial Scale Help

Post by thatbruce » Wednesday 08th September 2021 6:01pm

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

AndyGable
Posts: 140
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

Re: RS232 / Serial Scale Help

Post by AndyGable » Wednesday 08th September 2021 7:21pm

I have done a print to the debug console and it is empty

User avatar
BruceSteers
Posts: 657
Joined: Thursday 23rd July 2020 5:20pm

Re: RS232 / Serial Scale Help

Post by BruceSteers » Thursday 09th 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.
Last edited by BruceSteers on Thursday 09th September 2021 2:22am, edited 1 time in total.
Wishing well
Bruce

If at first you don't succeed , try it differently.

User avatar
BruceSteers
Posts: 657
Joined: Thursday 23rd July 2020 5:20pm

Re: RS232 / Serial Scale Help

Post by BruceSteers » Thursday 09th 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

Wishing well
Bruce

If at first you don't succeed , try it differently.

AndyGable
Posts: 140
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

Re: RS232 / Serial Scale Help

Post by AndyGable » Thursday 09th September 2021 2:45am

BruceSteers wrote:
Thursday 09th 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

AndyGable
Posts: 140
Joined: Wednesday 02nd December 2020 12:11am
Location: England
Contact:

Re: RS232 / Serial Scale Help

Post by AndyGable » Thursday 09th September 2021 2:45am

BruceSteers wrote:
Thursday 09th 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

User avatar
BruceSteers
Posts: 657
Joined: Thursday 23rd July 2020 5:20pm

Re: RS232 / Serial Scale Help

Post by BruceSteers » Thursday 09th September 2021 11:48am

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.
Wishing well
Bruce

If at first you don't succeed , try it differently.

Post Reply