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
Have a sub that sends your message and starts a response-timeout timer. You'll then either receive a response back (via AStreams1_NewData) or the timeout will fire (via Timer1_Tick).

.NewData can disable the timer, process the response, and then possibly call send message sub to start the next round of the conversation.

.Tick can disable the timer, then call the send message sub to resend the last request.

If the response takes *precisely* the same amount of time as the timeout, then you might occasionally get *both* events happen for the one request, so perhaps number your requests and responses so that you know which apply to which.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The exact details depend on your requirements and protocol.

You can do something like this, assuming that the response expected length is known:
B4X:
AStream.Write(your message)
Dim Response As B4XBytesBuilder
Response.Initialize
Do While Response.Length < 100
 Wait For Astream_NewData(Data() As Byte)
  Response.Append(Data)
Loop
 
Upvote 0

Ale_resource

Member
Licensed User
B4X:
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")
        
        Dim response As B4XBytesBuilder
        response.Initialize
        Do While response.Length < 100
            Wait For Astream1_NewData(Data() As Byte)
                response.Append(Data)
        Loop
        Log("a")       
        
    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

Sub AStreams1_NewData (Buffer() As Byte)
    'Log("INIZIO Ricezione Dati")
    'Log(BytesToString(Buffer, 0, Buffer.Length, "ASCII"))
    'risposta_Custom = BytesToString(Buffer, 0, Buffer.Length, "ASCII")
    Log("FINE Ricezione Dati")
    'Return BytesToString(Buffer, 0, Buffer.Length, "ASCII")
End Sub

using this code I have this log:

This is not correct , I don't have the answers to every single command
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
You have not specified what type of device receives and responds to commands.
and what would be your answers

Is it an anduino module, mobile phone, CCTV, etc.?
What is it
 
Upvote 0

Ale_resource

Member
Licensed User
sorry:
I translated this message.


Is it a printer?

what make and model?
is a cash register connected via tcp, the answer is formed by the command in the case of a positive answer or in the case of a negative answer I have a value of the type "ERR03"
 
Upvote 0

emexes

Expert
Licensed User
Yes , model CUSTOM
I think where @oparra was headed was:

if you/we know the make and model number of the printer, then you/we might be able to find a manual for it that tells you/us how to talk to it.

Can you post a photo of the printer, and another photo of the label that should be on the bottom or back of the printer?
 
Upvote 0

Ale_resource

Member
Licensed User
this is a part of the italian manual
 

Attachments

  • scan_compressed.pdf
    497 KB · Views: 145
Upvote 0

emexes

Expert
Licensed User
Righto, I got as far as ... can't download the English manual without being a logged-in customer.
On a positive note: do you recognize any of the model numbers, eg VK80, VKP80,VKP80II, etc. Is there a label on the printer somewhere that has anything like that on it?
 
Last edited:
Upvote 0

Ale_resource

Member
Licensed User
But not all is lost. ? Do you recognize any of the model numbers, eg VK80, VKP80,VKP80II, etc. Is there a label on the printer somewhere that has anything like that on it?
no , the model is this.... I also sent a piece of manual in Italian, in English I don't have it
 

Attachments

  • 1594809150202.png
    88.7 KB · Views: 132
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…