Android Question Asynchnous to synchronous mode

Victor Pavlov

Member
Licensed User
Hi,
I am writing driver for fiscal device. Usually you send binary command and receive answer from the device. In the last versions of B4A it became extremely difficult to manage it and I need some help. In order to make the question simpler, I will draw some block diagram:

1. The Android Device sends command to the Fiscal Printer via Socket;
2. Give time of 50 miliseconds to the Fiscal Printer to receive the command and send you ACK signal;
3. The Fiscal Printer should respond in 60 - 300 miliseconds. It can send you answer or command "Wait" for another 60 - 300 miliseconds;
4. Step 3 can be repeated several times, it depends on the load of the printer and current status;
5. The Fiscal Device send you the answer of the command in Step 1;
6. The Android Device should wait until Step 5 is completed to continue again with Step 1.

Generally this is something like:
PrinterTX.Write(WriteBuffer)
Multi Wait for PrinterTX_NewData (Buffer() As Byte) ' Here you should get the complete answer, because the device sends it part by part

If I use Wait For, it just gets the first received data and the parent sub continue.

Shortly: Single send, multiple sequential answers, the parent procedure should continue only when the complete multipart answer was received.

In V4A (previous versons) I do it with my own Sleep procedure, which just sleeps and do not allow parent sub to continue.

Any ideas?
 

Victor Pavlov

Member
Licensed User
No ideas?
This is sample code for the task.

B4X:
Private Sub FiscalPrint()
    Dim Ret As Int
    
    SendCommand(CreateCommand(cmdGetStatus, ""))
    Ret = GetAnswer()
    
    Select Case Ret
        Case 0:    'Do nothing

        Case 1:    'Transaction started, waiting payment
            SendCommand(CreateCommand(cmdPayment, "100"))
            Ret = GetAnswer()
            SendCommand(CreateCommand(cmdFinish, ""))
            Ret = GetAnswer()

        Case 2:    'Transaction started, payment complete, print receipt
            SendCommand(CreateCommand(cmdFinish, ""))
            Ret = GetAnswer()
    End Select
        
    SendCommand(CreateCommand(cmdStart, ""))
    Ret = GetAnswer()
    
    SendCommand(CreateCommand(cmdItemSale, "Beer;10;2.50"))
    Ret = GetAnswer()

    SendCommand(CreateCommand(cmdItemSale, "Chips;5;1.50"))
    Ret = GetAnswer()
    
    SendCommand(CreateCommand(cmdPayment, "100"))
    Ret = GetAnswer()

    SendCommand(CreateCommand(cmdFinish, ""))
    Ret = GetAnswer()

End Sub
 

Erel

Administrator
Staff member
Licensed User
In the last versions of B4A it became extremely difficult to manage it and I need some help. In order to make the question simpler, I will draw some block diagram:
Nothing has changed in the last versions of B4A that makes anything more difficult. You can keep using your old and not recommended sleep method that is based on a loop. The only change needed is to call it something else instead of Sleep.
 

Victor Pavlov

Member
Licensed User
What is the better way to do it? On my second post I wrote some sample sequence. How to do it better? Give us advice.
 

Erel

Administrator
Staff member
Licensed User
You made a statement in the first post:
"In the last versions of B4A it became extremely difficult to manage it and I need some help"

As I explained, this statement is completely incorrect. Nothing has changed in the last versions of B4A that makes it more complicate to implement such tasks. On the contrary the new resumable subs feature can be used to implement it in a correct way that doesn't hold the main thread.
However you are not forced to use the new feature. Your old solution will work exactly as before.

I cannot give you any other advice as the main point in your post is incorrect.

If you like you can start a new thread and ask how can Wait For and Sleep be used to implement such communication.
 
Last edited:
Top