Getting Data from Serial

Post

Posted
Rating:
#1 (In Topic #926)
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi Everyone,

Can someone help me please I am trying to get the Result of this command https://reference.epso….php?content_id=124#gs_lr

it makes the Epson PoS Printers send the status of the printer BUT I have no idea how to acually capture and handle the data that is being sent back to me

so for example if I send Hex 1D 72 50 it should return

Code (gambas)

  1. Drawer kick-out connector status (n = 2, 50)
  2. Bit             Binary  Hex     Decimal Status
  3. 0               0       00      0       Drawer kick-out connector pin 3 is LOW.
  4.                 1       01      1       Drawer kick-out connector pin 3 is HIGH.
  5. 13 −     −     −     (Reserved)
  6. 4               0       00      0       Fixed
  7. 5, 6    −     −     −     (Reserved)
  8. 7               0       00      0       Fixed
  9.  


does anyone know how I can capture the data coming from the Serial Printer I assume i Read it in like

Code (gambas)

  1. Try Read #RS232Printer, Global.ScannerData, Lof(RS232Printer)

but i am not sure what format the Printer would send the data in (not sure if it is string or Byte)

If someone out there who can help me with this I would be most grateful as It would allow me to add Status support to my Linux Point of sale software (My Windows version has had it for years but that is because it has been using OPoS)
Online now: No Back to the top

Post

Posted
Rating:
#2
Regular
vuott is in the usergroup ‘Regular’
You must first understand if the printer sends only one Byte of data or several bytes (maybe containing other values that you may not need).

From what I understand :| , the printer returns a message consisting of 8 bits, then of 1 Byte.
I would write:

Code (gambas)

  1. Read #RS232Printer, variable_byte
(where variable_byte is a Byte data-type variable).
It will then be the task of the subsequent instructions to know the bits states of the Byte data-type value received.

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#3
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

vuott said

You must first understand if the printer sends only one Byte of data or several bytes (maybe containing other values that you may not need).

From what I understand :| , the printer returns a message consisting of 8 bits, then of 1 Byte.
I would write:

Code (gambas)

  1. Read #RS232Printer, variable_byte
(where variable_byte is a Byte data-type variable).
It will then be the task of the subsequent instructions to know the bits states of the Byte data-type value received.


When I was working on this in FeeeBASIC the program recevied the data like this 00000100 So would I still have Variable_Byte defined as byte?
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
vuott is in the usergroup ‘Regular’

AndyGable said

the program recevied the data like this 00000100
Uhmmm…this could be a string of 8 ASCII numeric characters.
Have you tried to read those data as a string?……

Code (gambas)

  1. Read #RS232Printer, variable_string, 8
Reads 8 bytes as string data- type.

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#5
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

vuott said

AndyGable said

the program recevied the data like this 00000100
Uhmmm…this could be a string of 8 ASCII numeric characters.
Have you tried to read those data as a string?……

Code (gambas)

  1. Read #RS232Printer, variable_string, 8
Reads 8 bytes as string data- type.

NOt yer I will try that when I get back home to the Printer
Online now: No Back to the top

Post

Posted
Rating:
#6
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
ok so I have this so far

Code (gambas)

  1. ' Gambas class file
  2.  
  3. Public RS232Printer As SerialPort
  4.  
  5.         'Hex    1D    72    n
  6.          Public RequestPaperStatus As String =Chr$(&H1D) & Chr$(&H72) & Chr$(49) ' Paper Status
  7.     Public RequestCashDrawerStatus As String = "u001D" & "r" & Chr(50) ' 50 for the Cash Drawer
  8.    
  9. Public Sub Form_Open()
  10. '
  11.     RS232Printer = New SerialPort As "RS232Printer"
  12.    
  13.     'Set up the Serial Device so it should start talking
  14.     With RS232Printer
  15.         .PortName = "/dev/ttyS0"
  16.         .Speed = "9600"
  17.         .Parity = SerialPort.None
  18.         .DataBits = SerialPort.bits8
  19.         .StopBits = SerialPort.bits1
  20.         .FlowControl = SerialPort.None
  21.    End With
  22.        
  23.     Try RS232Printer.Open
  24.         If Error Then
  25.             AddToListBox("Sorry Unable to connect to Printer\nPlease check settings and try again")    
  26.         Else
  27.             If RS232Printer.Status = Net.Active Then
  28.                 RS232Printer.Watch(gb.Write, 1)
  29.                 AddToListBox("Ready.. Watching For incoming data from Printer")
  30.             Endif
  31.         End If
  32.  
  33.  
  34.  
  35. Public Sub RS232Printer_Read()
  36.     Dim variable_string As String = Null
  37.  
  38.     Sleep 0.025
  39.    
  40.     Read #RS232Printer, variable_string, 8
  41.    
  42.     AddToListBox(variable_string)
  43.    
  44.     If Error Then
  45.         AddToListBox("Error With Incoming Serial Data\n" & Error.Text)
  46.     Endif
  47.  
  48.  
  49.  
  50. Public Sub Button1_Click()
  51.  
  52.     AddToListBox("Sending Paper Status Request")
  53.    
  54.     'Write #RS232Printer, RequestPaperStatus
  55.    
  56.     AddToListBox("Sending Cash Drawer Status Request")  
  57.    
  58.     Write #RS232Printer, RequestCashDrawerStatus
  59.    
  60.    ' RS232Scanner_Read
  61.  
  62.  
  63. Public Sub AddToListBox(textToShow As String)
  64.  
  65.     Dim gw As GridView
  66.     Dim b As Byte
  67.  
  68.     ListBox1.Add(Trim(textToShow))
  69.     ListBox1.Index = ListBox1.Count - 1
  70.     gw = ListBox1.Children[0]
  71.  
  72.     b = ListBox1.List.Max
  73.  
  74.     gw.Rows[b].Height = -1
  75.    
  76.     ListBox1.Refresh
  77.     FMain.Refresh
  78.     'Wait 0.1
  79.  
  80.  

While the program is is running i can click the button to request the status and then I send

Code (gambas)

  1. Write #RS232Printer, RequestPaperStatus
  2.  

to the printer but i am not sure if everything is ok with printer or it is ignoring the commands I am sending

am I sending the code corerectly is whaty I am thinking

this is what I need to send as Hex    1D    72    n = 49

this i what I am using at the moment

Chr$(&H1D) & Chr$(&H72) & Chr$(49)

Is this correct or have i messed it up some where?
Online now: No Back to the top

Post

Posted
Rating:
#7
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
 After some work I have the Printer SORT of talking to my software

When I open the COver the software displays a <
when I press the Feed Key I get a \

I am not sure if this is correct or not BUT at least something is happening lol

But I was thinking I should get a 8 bits of data
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Enthusiast
PJBlack is in the usergroup ‘Enthusiast’

Code (gambas)

  1. Ascii   Hex   Dec    Bin
  2. <       3C    60     00111100
  3. \       5C    92     01011100
  4.  
Online now: No Back to the top

Post

Posted
Rating:
#9
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

PJBlack said

Code (gambas)

  1. Ascii   Hex   Dec    Bin
  2. <       3C    60     00111100
  3. \       5C    92     01011100
  4.  

Cool so how do I show the Bin on screen? as that is how the Printer sends the status

Code (gambas)

  1. Bit             Binary  Hex     Decimal Status
  2. 0, 1    00              00      0               Roll paper near-end sensor: paper adequate.
  3.                 11              03      3               Roll paper near-end sensor: paper not present.
  4. 2, 3    00              00      0               Roll paper end sensor: paper present.
  5.                 11              0C      12              Roll paper end sensor: paper not present.
  6. 4               0               00      0               Fixed
  7. 5,6             −             −     −             (Reserved)
  8. 7               0               00      0               Fixed
  9.  


This is the Data I would get if I sent the following command Chr$(&H1D) & Chr$(&H72) & 50

Code (gambas)

  1. Bit             Binary  Hex     Decimal Status
  2. 0               0               00      0               Drawer kick-out connector pin 3 is LOW.
  3.                 1               01      1               Drawer kick-out connector pin 3 is HIGH.
  4. 13 −             −     −             (Reserved)
  5. 4               0               00      0               Fixed
  6. 5, 6    −             −     −             (Reserved)
  7. 7               0               00      0               Fixed
  8.  
Does any one have any idea how I can capture the above data
Online now: No Back to the top

Post

Posted
Rating:
#10
Regular
vuott is in the usergroup ‘Regular’

AndyGable said

how do I show the Bin on screen?
In general, to convert a numeric value to a binary representation of type string, you can use the native Bin() function:
https://gambaswiki.org/wiki/lang/bin

You will take care to preliminarily convert the ASCII character to a numeric value using the Asc () function.
Exemplum:

Code (gambas)

  1. Print Bin (Asc ("<"))

____________________________________________________

AndyGable said

Code (gambas)

  1. Bit             Binary  Hex     Decimal Status
  2. 0               0               00      0               Drawer kick-out connector pin 3 is LOW.
  3.                 1               01      1               Drawer kick-out connector pin 3 is HIGH.
  4. 13 −             −     −             (Reserved)
  5. 4               0               00      0               Fixed
  6. 5, 6    −             −     −             (Reserved)
  7. 7               0               00      0               Fixed
  8.  
Does any one have any idea how I can capture the above data
IF the printer sends 8-bits data, these correspond to 1 Byte.
Therefore it would seem that you just need simply to read 1 Byte:

Code (gambas)

  1. Dim variable_Byte As Byte
  2.  
  3. Read #RS232Printer, variable_Byte
  4.  
  5. Print Bin(variable_Byte)

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#11
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

vuott said

AndyGable said

how do I show the Bin on screen?
In general, to convert a numeric value to a binary string, you can use the native Bin() function:
https://gambaswiki.org/wiki/lang/bin

You will take care to preliminarily convert the ASCII character to a numeric value using the Asc () function.
Exemplum:

Code (gambas)

  1. Print Bin (Asc ("<"))

____________________________________________________

AndyGable said

Code (gambas)

  1. Bit             Binary  Hex     Decimal Status
  2. 0               0               00      0               Drawer kick-out connector pin 3 is LOW.
  3.                 1               01      1               Drawer kick-out connector pin 3 is HIGH.
  4. 13 −             −     −             (Reserved)
  5. 4               0               00      0               Fixed
  6. 5, 6    −             −     −             (Reserved)
  7. 7               0               00      0               Fixed
  8.  
Does any one have any idea how I can capture the above data
IF the printer sends 8-bits data, these correspond to 1 Byte.
Therefore it would seem that you just need simply to read 1 byte:

Code (gambas)

  1. Dim variable_Byte As Byte
  2.  
  3. Read #RS232Printer, variable_Byte
  4.  
  5. Print Bin(variable_Byte)


Thanks for that I am now getting on screen


Code

Ready....
Watching For incoming data from Printer
10100
0
0
1111

I assume that is the printer saying all is well but it is is not in 8 bits
Online now: No Back to the top

Post

Posted
Rating:
#12
Regular
vuott is in the usergroup ‘Regular’

AndyGable said

Code

Ready....
Watching For incoming data from Printer
10100
0
0
1111

I assume that is the printer saying all is well but it is is not in 8 bits
If you delete the Bin () function:

Code (gambas)

  1. ......
  2. Print variable_Byte
what numeric data do you get?

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#13
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

vuott said

AndyGable said

Code

Ready....
Watching For incoming data from Printer
10100
0
0
1111

I assume that is the printer saying all is well but it is is not in 8 bits
If you delete the Bin () function:

Code (gambas)

  1. ......
  2. Print variable_Byte
what numeric data do you get?


Code

20
0
0
15

on screen I am now getting
11000110101001111
200015

because I added

    While Not Eof(RS232Printer)
        Read #RS232Printer, variable_Byte
        DataRec &= variable_Byte
    Wend
Online now: No Back to the top

Post

Posted
Rating:
#14
Regular
vuott is in the usergroup ‘Regular’
From the on-line documentation, indicated by you:
https://reference.epso…/index.php?content_id=124
it appears that each status is communicated by the printer through 1 Byte.
Infact it says: « Each status is <COLOR color="#800000">1 byte</COLOR>. »

Well, if we take the "Paper sensor status", for example we have:
«<COLOR color="#800000">Roll paper near-end sensor: paper adequate</COLOR>» = decimal value 0 (id est: &h00 in Gambas…or in C language 0x00… OK?);
and
«<COLOR color="#800000">Roll paper end sensor: paper not present</COLOR>» = decimal value 12, which corresponds to binary 00001100 (in fact in the table the third and fourth bits - index 2 and 3 - are indicated as set to 1, counting from the right).

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#15
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

vuott said

From the on-line documentation, indicated by you:
https://reference.epso…/index.php?content_id=124
it appears that each status is communicated by the printer through 1 Byte.
Infact it says: « Each status is <COLOR color="#800000">1 byte</COLOR>. »

Well, if we take the "Paper sensor status", for example we have:
«<COLOR color="#800000">Roll paper near-end sensor: paper adequate</COLOR>» = decimal value 0 (id est: &h00 in Gambas…or in C language 0x00… OK?);
and
«<COLOR color="#800000">Roll paper end sensor: paper not present</COLOR>» = decimal value 12, which corresponds to binary 00001100 (in fact in the table the third and fourth bits - index 2 and 3 - are indicated as set to 1, counting from the right).


Yea That is right so how Do I get my App to show messages to say Paper Fine or Paper Low etc


<LIST>
  • <LI>I want to support the following status
    Paper Low
    Cover Open
    Cash Drawer Open
    Cash Drawer Closed</LI>
</LIST>
Online now: No Back to the top

Post

Posted
Rating:
#16
Regular
vuott is in the usergroup ‘Regular’
It would seem that you have to read with the instruction "Read" every single Byte that the printer sends you.

Code (gambas)

  1. Read #printer-file-device, variable_BYTE_data-type

Often external devices send String values (ASCII characters), but the documentation shows that your printer sends numeric values of 1 Byte data-type.
…or so at least  :? I understand.

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#17
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

vuott said

It would seem that you have to read with the instruction "Read" every single Byte that the printer sends you.

Often external devices send String values (ASCII characters), but the documentation shows that your printer sends numeric values of 1 Byte data-type.

And how would i do that?
Online now: No Back to the top

Post

Posted
Rating:
#18
Regular
vuott is in the usergroup ‘Regular’
In general, in Gambas to read the value of 1 Byte data-type from a stream (file), you do what I have already indicated, and which I repeat here as a simple practical example:

Code (gambas)

  1. Dim variable_byte As Byte
  2.  
  3. Read #variable_file, variable_byte

…then you compare the numerical value read with those present in the table of printer documentation.

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#19
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’

Code (gambas)

  1. Select case variable_byte
  2.  
  3.   Case 20
  4.     textbox.text = "draw  Open"   'or what ever it indicates
  5.  
  6.   Case 15
  7.     textbox2.text = "paper out"
  8.  
  9.  
  10.  
  11.  
  12.  
Online now: No Back to the top

Post

Posted
Rating:
#20
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’
Hi Everyone

Thank you so much for your input on this at the moment

I have been doing some more work and I found some old code I use to use in FreeBASIC

Code

    Dim status As String

    Write #Global.PrinterPort, Chr(10) & Chr(4) & 1
    Global.PrinterPort.Send  

    While loc(Global.PortName) = 0
        Sleep 1
    Wend

    status = Input(Loc(Global.PrinterPort), 1)

    Print Bin(Val(status), 8)

    If Bit(Val(status), 3) = -1 Then
        Global.AddToScreenList("Kickout connector high")
    Else
      Global.AddToScreenList("Kickout connector low")
   Endif
   
   If Bit(Val(status), 4) = -1 Then
      Global.AddToScreenList("Printer is Offline")
   Else
      Global.AddToScreenList("Printer is Online")
   Endif
   
   If Bit(Val(status), 6) = -1 Then
      Global.AddToScreenList("Waiting for Online Recovery")
   Else
      Global.AddToScreenList("Not Waiting for Online Recovery")
   Endif
   
   If Bit(Val(status), 7) = -1 Then
      Global.AddToScreenList("Paper fed by button")
   Else
      Global.AddToScreenList("Paper not fed by button")
   Endif


I Tried this (hoping i would not have to do any conversion) but I get a Error message on Bit saying "Unknown Identifier"

So does anyone now the Gambas option for Bit?
Online now: No Back to the top

Post

Posted
Rating:
#21
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
(Quicky)
I think the minimal thing we can deal with is a Byte. You can Or or And bytes with some mask (usually a Const or an Enum) to see if one or more bits are set.
e.g.

Code

Const elephant as Byte=3
...
Function IsItAnElephant(this as Byte) as Boolean
   If And(this, elephant) then Return True
   Return False
End

Mneh. It looks right to me but I haven't actually tried it.

hth
thatbruce

Online now: No Back to the top

Post

Posted
Rating:
#22
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
here are all the gambas bit functions  :D

k9:k9.9:start [GAMBAS BOOK 3.19.5]

since you are working with serial port … this might be of intrest

k24:k24.1:k24.1.5:start [GAMBAS BOOK 3.19.5]
Online now: No Back to the top

Post

Posted
Rating:
#23
Regular
vuott is in the usergroup ‘Regular’

AndyGable said

… I get a Error message on Bit saying "Unknown Identifier"
Of course, the Bit () function in Gambas does not exist, nor is it a function or class that you have created.


AndyGable said

So does anyone now the Gambas option for Bit?
What should Bit () do?  :|

Europaeus sum !

<COLOR color="#FF8000">Amare memorentes atque deflentes ad mortem silenter labimur.</COLOR>
Online now: No Back to the top

Post

Posted
Rating:
#24
Enthusiast
AndyGable is in the usergroup ‘Enthusiast’

grayghost4 said

here are all the gambas bit functions  :D

k9:k9.9:start [GAMBAS BOOK 3.19.5]

since you are working with serial port … this might be of intrest

k24:k24.1:k24.1.5:start [GAMBAS BOOK 3.19.5]

that last one does seem to help

I just can not get a reliable data from the prnter either i get 20015 (all good) or it comes like 200152001520015 so is my app not clearing the data some where?
Online now: No Back to the top

Post

Posted
Rating:
#25
Avatar
Enthusiast
GrayGhost is in the usergroup ‘Enthusiast’
AndyGable wrote: ↑Saturday 12th November 2022 12:51pm
    So does anyone now the Gambas option for Bit?

What should Bit () do? :|
see my post above


  Print BTst(0, 0)  …….. prints   false   least significant bit is 0
  Print BTst(1, 0)  …….. returns  True   least significant bit is 1
  Print BTst(2, 1)    ……….returns True   bit two is a 1

Function   Description
BClr (Number, Bit)         Returns number with deleted bit' Bit'.
BSet (Number, Bit)         Returns number with bit' Bit' set.
BTst (Number, Bit)          Returns True if bit' Bit' is set, otherwise False.
BChg (Number, Bit)   Returns Number whose bit' Bit' was inverted.
Lsl (Number, Bit)   Each bit in the bit sequence of Number is shifted to the left by' Bit' bits. For the left-hand bits that are omitted, zero bits are attached to the right-hand side. The sign is ignored. (Lsl = logical shift left)
Lsr (Number, Bit)   Each bit in the bit sequence of Number is shifted to the right by' Bit' bits. Zero bits are inserted on the left for the bits that are omitted on the right. (Lsr = logical shift right)
Shl (Number, Bit)   Each bit in the bit sequence of Number is shifted to the left by' Bit' bits. For the left-hand bits that are omitted, zero bits are attached to the right-hand side.
Asl (Count, Bit)   Synonym for Shl (count, bit). The sign is not ignored.
Shr (Number, Bit)   Each bit in the bit sequence of Number is shifted to the right by' Bit' bits. The sign bit is inserted on the left for the bits that are omitted on the right.
Asr (Number, Bit)   Synonym for Shr (Number, Bit)
Rol (Number, Bit)   During the operation Rol, the bits rotate by the number of' bits' as if MSB (most significant bit - highest value bit position) and LSB (less significant bit - lowest value bit position) were connected to each other. The bit that is shifted out of the bit sequence to the left has the same value as the bit that is shifted in from the right. (Rol = Rotate left)
Ror (Number, Bit)   During operation Ror(), the bits rotate by the number of 'bits' as if MSB and LSB were connected to each other. The bit that is shifted out of the bit sequence to the right has the same value as the bit that is shifted in from the left.
Online now: No Back to the top
1 guest and 0 members have just viewed this.