Android Question Astrem

Ale_resource

Member
Licensed User
hi, I am connecting to a tcp device via clientsocket and sending a message via AsyncStreams, if I create a sub to send the message can I receive the reply on the same sub or only through AStreams1_NewData?
In one cycle, I would need to send data and receive an answer immediately before continuing to send the next message.

Sen Message:
Sub InviaComandoCustom (comando As String )
    If clientSocket.Connected = True Then
        Log("Inizio Invio comando")
        'AStreams1.InitializePrefix(clientSocket.InputStream,False,clientSocket.OutputStream,"clientSocket")
        Dim stringa As String = NumberFormat(Main.contatore,2,0) & "0" & comando
        Dim ck As Int = CalcolaCK(stringa)
        stringa =  stringa & NumberFormat(ck,2,0)
        If Main.contatore = 99 Then
            Main.contatore = 0
        End If
        Dim b(stringa.Length + 2) As Byte
        b(0) = 2
        b(stringa.Length + 1) = 3
        Dim BC As ByteConverter
        Log("send : " & stringa)
        BC.ArrayCopy(stringa.GetBytes("ASCII"),0,b,1,stringa.Length)
        If clientSocket.IsInitialized = True And clientSocket.Connected = True Then
            AStreams1.Write2(b,0,b.Length)
        End If
        Main.contatore=Main.contatore+1   
        Log("FINE INVIO COMANDO")
    Else
        Msgbox2Async("Nessuna Stampante connessa , riprovare il collegamento ? ","ERRORE","SI","NO","",Null,False)
        Wait For Msgbox_Result (Result As String)
        If Result = DialogResponse.POSITIVE Then
            AvviaConnessione
        End If
    End If
End Sub

when I call this sub I do not receive the answer and therefore I go on sending messages without verifying the result and I cannot in this way give the opportunity to try again to resend the message that was not successful
 

emexes

Expert
Licensed User
Are these the printer manuals?
It looks like it's just standard ESC/P with some additions for cutting paper, printing barcodes, etc. So now I'm trying to find the bit where it talks about every command apparently returning a response. Perhaps that's only over the TCP/IP interface. Or maybe it is only certain commands, like having to wait for the paper cut to be finished.
 
Upvote 0

Ale_resource

Member
Licensed User
the answer is given on every single command .. for example, between one command and another, the card may run out and therefore an answer is returned to me every time
 
Upvote 0

emexes

Expert
Licensed User
Why are you wrapping the strings that you send to the printer, with STX (ASCII 002) before the string and ETX (ASCII 03) after the string?
 
Upvote 0

emexes

Expert
Licensed User
because it is a protocol specification that the commands must start with 02 and end with 03
Can you show us the commands manual page that talks about that? There is an Intermec Printer Language that does that, but I can't find anything about it in the manuals related to this printer.
 
Upvote 0

emexes

Expert
Licensed User
This will sound like a dumb question, but... have you tried just printing text to the printer, eg:
B4X:
Dim b() As Byte = Array As Byte(13, 10, 65, 66, 67, 49, 50, 51, 13, 10)
AStreams1.Write(b)
It is possible to ask the printer to tell you if an error occurs or the paper runs out (or gets near the end). The extra <STX><ETX> layer might be a massive overkill, as well as lock you in to using a limited choice of printers rather than ESC/P printers generally.

edit: or is limiting printer choice by design?
 
Last edited:
Upvote 0

Ale_resource

Member
Licensed User
yes, the message arrives, the problem I have in waiting for the answer in the same sub before proceeding to send another command .. yes the choice to limit the printer belongs to the company
 
Upvote 0

emexes

Expert
Licensed User
the problem I have in waiting for the answer in the same sub before proceeding to send another command
The nature of event-driven programming is that your program doesn't grind to a halt waiting for something to happen. Using Wait For works great if you are waiting for ONE thing to happen, but gets complicate if you're waiting for MULTIPLE things.

What I would do here is:

Send message
Set timeout timer
[job done]

Now one of the following two events will happen first:

NewData event:
if response is positive then
Send next message
Set timeout timer
else
Resend last message
Set timeout timer
end if

Timeout Tick event:
Resend last message
Set timeout timer

Then we need to consider what to do if errors keep occurring without a break. Perhaps display a notification every five minutes when attempts are being made to print, but still allow other aspects of the program to continue functioning. Perhaps unsuccessful printing could be saved to a file, and then all printed when the fault is rectified.
 
Upvote 0

raphaelcno

Active Member
Licensed User
Longtime User
using this code I have this log:
View attachment 97154
This is not correct , I don't have the answers to every single command


In following block you should probably replace the number "100" by the number of bytes you expect to receive:
B4X:
        Do While response.Length < 100  ' <<<<<<< Replace 100 by expected number of bytes
            Wait For Astream1_NewData(Data() As Byte)
                response.Append(Data)
        Loop

otherwise you don't get out of the loop!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…