Ale_resource Member Licensed User Apr 9, 2021 #1 Hi I have a connection to a device in tcp to which I send a command and then I get a response. I send the command like this: B4X: AStreams1.Write2(b,0,b.Length) and then I wait for the answer like this: B4X: Wait For AStreams1_NewData(BufferProg() As Byte) Dim Text As String = BytesToString(BufferProg, 0, BufferProg.Length, "UTF8").Trim the problem is that I never get the full answer and I can't understand why. This is the answer I get from the code above : V000000000000000000000000000000000000000000000 V001000000000000000000000000000000000000000000 V002040000000000000000000000000000000000000000 Click to expand... While in reality it should be this: V000000000000000000000000000000000000000000000 V001000000000000000000000000000000000000000000 V002040000000000000000000000000000000000000000 V003000000000000000000000000000000000000000000 V004000000000000000000000000000000000000000000 V005000000000000000000000000000000000000000000 V006000000000000000000000000000000000000000000 V007000000000000000000000000000000000000000000 V008000110000000000000000000000000000000000000 V009000210000000000000000000000000000000000000 V010000310000000000000000000000000000000000000 V011000510000000000000000000000000000000000000 V012000610000000000000000000000000000000000000 V013000000000000000000000000000000000000000000 V014000000000000000000000000000000000000000000 V015000000000000000000000000000000000000000000 V016000000000000000000000000000000000000000000 V017000000000000000000000000000000000000000000 V018000000000000000000000000000000000000000000 V019000000000000000000000000000000000000000000 V020000000000000000000000000000000000000000000 V021000000000000000000000000000000000000000000 V022000000000000000000000000000000000000000000 V023000000000000000000000000000000000000000000 V024000000000000000000000000000000000000000000 V025000000000000000000000000000000000000000000 V026000000000000000000000000000000000000000000 V027000000000000000000000000000000000000000000 V028000000000000000000000000000000000000000000 V029000000000000000000000000000000000000000000 V030000000000000000000000000000000000000000000 V031000000000000000000000000000000000000000000 V032000000000000000000000000000000000000000000 V033000000000000000000000000000000000000000000 V034000000000000000000000000000000000000000000 V035000000000000000000000000000000000000000000 V036000000000000000000000000000000000000000000 V037000000000000000000000000000000000000000000 V038000000000000000000000000000000000000000000 V039000000000000000000000000000000000000000000 Click to expand... It seems that reception is interrupted or I don't know what else , thanks
Hi I have a connection to a device in tcp to which I send a command and then I get a response. I send the command like this: B4X: AStreams1.Write2(b,0,b.Length) and then I wait for the answer like this: B4X: Wait For AStreams1_NewData(BufferProg() As Byte) Dim Text As String = BytesToString(BufferProg, 0, BufferProg.Length, "UTF8").Trim the problem is that I never get the full answer and I can't understand why. This is the answer I get from the code above : V000000000000000000000000000000000000000000000 V001000000000000000000000000000000000000000000 V002040000000000000000000000000000000000000000 Click to expand... While in reality it should be this: V000000000000000000000000000000000000000000000 V001000000000000000000000000000000000000000000 V002040000000000000000000000000000000000000000 V003000000000000000000000000000000000000000000 V004000000000000000000000000000000000000000000 V005000000000000000000000000000000000000000000 V006000000000000000000000000000000000000000000 V007000000000000000000000000000000000000000000 V008000110000000000000000000000000000000000000 V009000210000000000000000000000000000000000000 V010000310000000000000000000000000000000000000 V011000510000000000000000000000000000000000000 V012000610000000000000000000000000000000000000 V013000000000000000000000000000000000000000000 V014000000000000000000000000000000000000000000 V015000000000000000000000000000000000000000000 V016000000000000000000000000000000000000000000 V017000000000000000000000000000000000000000000 V018000000000000000000000000000000000000000000 V019000000000000000000000000000000000000000000 V020000000000000000000000000000000000000000000 V021000000000000000000000000000000000000000000 V022000000000000000000000000000000000000000000 V023000000000000000000000000000000000000000000 V024000000000000000000000000000000000000000000 V025000000000000000000000000000000000000000000 V026000000000000000000000000000000000000000000 V027000000000000000000000000000000000000000000 V028000000000000000000000000000000000000000000 V029000000000000000000000000000000000000000000 V030000000000000000000000000000000000000000000 V031000000000000000000000000000000000000000000 V032000000000000000000000000000000000000000000 V033000000000000000000000000000000000000000000 V034000000000000000000000000000000000000000000 V035000000000000000000000000000000000000000000 V036000000000000000000000000000000000000000000 V037000000000000000000000000000000000000000000 V038000000000000000000000000000000000000000000 V039000000000000000000000000000000000000000000 Click to expand... It seems that reception is interrupted or I don't know what else , thanks
KMatle Expert Licensed User Longtime User Apr 9, 2021 #2 Did you initialize with prefix mode? Upvote 0
Ale_resource Member Licensed User Apr 9, 2021 #3 KMatle said: Did you initialize with prefix mode? Click to expand... no because the other device does not support prefix mode Upvote 0
KMatle said: Did you initialize with prefix mode? Click to expand... no because the other device does not support prefix mode
DonManfred Expert Licensed User Longtime User Apr 10, 2021 #4 Note that you should expect the packets to be splittet. Not all data can come in one event... Upvote 0
Ale_resource Member Licensed User Apr 10, 2021 #5 DonManfred said: Note that you should expect the packets to be splittet. Not all data can come in one event... Click to expand... I put the log write in the newdata event and it is written only once Upvote 0
DonManfred said: Note that you should expect the packets to be splittet. Not all data can come in one event... Click to expand... I put the log write in the newdata event and it is written only once
Erel B4X founder Staff member Licensed User Longtime User Apr 11, 2021 #6 Your code will only handle the NewData event once. Don't use Wait For here. Implement it in a different sub. Upvote 0
Your code will only handle the NewData event once. Don't use Wait For here. Implement it in a different sub.
Ale_resource Member Licensed User Apr 12, 2021 #7 Erel said: Your code will only handle the NewData event once. Don't use Wait For here. Implement it in a different sub. Click to expand... I tried to put the following code twice: B4X: Wait For AStreams1_NewData(BufferProg() As Byte) Wait For AStreams1_NewData(BufferProg() As Byte) but doing so does not execute subsequent lines of code. So I figured the package is sent all at once. Isn't there some time or byte limitation? Upvote 0
Erel said: Your code will only handle the NewData event once. Don't use Wait For here. Implement it in a different sub. Click to expand... I tried to put the following code twice: B4X: Wait For AStreams1_NewData(BufferProg() As Byte) Wait For AStreams1_NewData(BufferProg() As Byte) but doing so does not execute subsequent lines of code. So I figured the package is sent all at once. Isn't there some time or byte limitation?
Erel B4X founder Staff member Licensed User Longtime User Apr 12, 2021 #8 This code is plainly wrong. You cannot assume the number of times that it will be called in non-prefix mode. First step is to implement the event in its own sub. Upvote 0
This code is plainly wrong. You cannot assume the number of times that it will be called in non-prefix mode. First step is to implement the event in its own sub.
Ale_resource Member Licensed User Apr 12, 2021 #9 Erel said: This code is plainly wrong. You cannot assume the number of times that it will be called in non-prefix mode. First step is to implement the event in its own sub. Click to expand... How should I do it then? Making a loop from 1 to 100 for example and putting inside the call to the new_data? Can you give me an example? thank you Upvote 0
Erel said: This code is plainly wrong. You cannot assume the number of times that it will be called in non-prefix mode. First step is to implement the event in its own sub. Click to expand... How should I do it then? Making a loop from 1 to 100 for example and putting inside the call to the new_data? Can you give me an example? thank you
Erel B4X founder Staff member Licensed User Longtime User Apr 12, 2021 #10 Code: Sub AStreams1_NewData(BufferProg() As Byte) Log(BufferProg.Length) End Sub Delete the Wait For AStream1... Upvote 0
Code: Sub AStreams1_NewData(BufferProg() As Byte) Log(BufferProg.Length) End Sub Delete the Wait For AStream1...
Ale_resource Member Licensed User Apr 12, 2021 #11 Erel said: Code: Sub AStreams1_NewData(BufferProg() As Byte) Log(BufferProg.Length) End Sub Delete the Wait For AStream1... Click to expand... I did so B4X: .... AStreams1.Write2(b,0,b.Length) .... Sub AStreams1_NewData (Buffer() As Byte) Log(Buffer.Length) End Sub but on the log I don't see anything Upvote 0
Erel said: Code: Sub AStreams1_NewData(BufferProg() As Byte) Log(BufferProg.Length) End Sub Delete the Wait For AStream1... Click to expand... I did so B4X: .... AStreams1.Write2(b,0,b.Length) .... Sub AStreams1_NewData (Buffer() As Byte) Log(Buffer.Length) End Sub but on the log I don't see anything
Erel B4X founder Staff member Licensed User Longtime User Apr 12, 2021 #12 There nothing more that I can say based on the information you provided. Upvote 0
Ale_resource Member Licensed User Apr 12, 2021 #13 Erel said: There nothing more that I can say based on the information you provided. Click to expand... I upload a small sample project of the code I use, maybe something wrong? Attachments test.zip test.zip 11.5 KB · Views: 425 Upvote 0
Erel said: There nothing more that I can say based on the information you provided. Click to expand... I upload a small sample project of the code I use, maybe something wrong?
Erel B4X founder Staff member Licensed User Longtime User Apr 12, 2021 #14 1. Switch to B4XPages. This is especially important when dealing with communication. 2. Remove HttpUtils2 and add OkHttpUtils2. 3. Please read my short answers more carefully: Upvote 0
1. Switch to B4XPages. This is especially important when dealing with communication. 2. Remove HttpUtils2 and add OkHttpUtils2. 3. Please read my short answers more carefully:
Ale_resource Member Licensed User Apr 12, 2021 #15 Erel said: 1. Switch to B4XPages. This is especially important when dealing with communication. 2. Remove HttpUtils2 and add OkHttpUtils2. 3. Please read my short answers more carefully: View attachment 111510 Click to expand... Yes, the changes you indicated to me I made in the original project but not in the demo that I sent you sorry ... all done except the transition to B4XPages but unfortunately the result does not change Upvote 0
Erel said: 1. Switch to B4XPages. This is especially important when dealing with communication. 2. Remove HttpUtils2 and add OkHttpUtils2. 3. Please read my short answers more carefully: View attachment 111510 Click to expand... Yes, the changes you indicated to me I made in the original project but not in the demo that I sent you sorry ... all done except the transition to B4XPages but unfortunately the result does not change
C Chris2 Well-Known Member Licensed User Longtime User Apr 12, 2021 #16 As well as @Erel's points, I think you may have missed @DonManfred 's... Note that you should expect the packets to be splittet. Not all data can come in one event... Click to expand... The reply you're expecting will come in bits, so you might need to let it build up... B4X: Private sb As StringBuilder <--- in Globals Sub AStreams1_NewData (Buffer() As Byte) sb.Append(BytesToString(BufferProg, 0, BufferProg.Length, "UTF8") Dim s as string = sb.ToString Log(s) .... .... End Sub You can then check if string s has the complete message using regex, or its length, or whatever. You will need to know something about the reply you're expecting though in order to know when it is complete. AsyncStreamsText might be useful also.... [class] AsyncStreamsText - Useful when working with streams of text When a message (set of bytes) is sent over a network channel, of any type, there is no guarantee that the whole message will arrive as a single set of bytes. The message might be split or merged together with other messages. One way to overcome this issue is to initialize AsyncStreams in... www.b4x.com Upvote 0
As well as @Erel's points, I think you may have missed @DonManfred 's... Note that you should expect the packets to be splittet. Not all data can come in one event... Click to expand... The reply you're expecting will come in bits, so you might need to let it build up... B4X: Private sb As StringBuilder <--- in Globals Sub AStreams1_NewData (Buffer() As Byte) sb.Append(BytesToString(BufferProg, 0, BufferProg.Length, "UTF8") Dim s as string = sb.ToString Log(s) .... .... End Sub You can then check if string s has the complete message using regex, or its length, or whatever. You will need to know something about the reply you're expecting though in order to know when it is complete. AsyncStreamsText might be useful also.... [class] AsyncStreamsText - Useful when working with streams of text When a message (set of bytes) is sent over a network channel, of any type, there is no guarantee that the whole message will arrive as a single set of bytes. The message might be split or merged together with other messages. One way to overcome this issue is to initialize AsyncStreams in... www.b4x.com