Hi @LangueRodriguez it works with DecodeHex. Can I ask you the last question? When I go to read the serial, I don't see all data, Sometimes I have white spaces, how can I solve them?
@Mario C I add in DataAvaible event Serial.Poll. But not change the my problem
you can search the forum, for this and the documentation.
The DataAvailable event can send it all but it's not always the case. you should use Serial.LookAhead to see if all your data is there then read it using Serial.ReadAll or Serial.Read(NUMBYTES).
Quote from this docs page:
When the serial device sends data back to the Serial control that is open, the Serial control’s DataAvailable event fires. The data is transferred into a memory buffer. In the DataAvailable event handler, you use the Read or ReadAll methods to get some or all of the data from the buffer. Choose the Read method when you want to get a specific number of bytes (characters) from the buffer. Choose the ReadAll method to get all of the data in the buffer. In both cases, the data returned from the buffer is removed from the buffer to make room for more incoming data. If you need to examine the data in the buffer without removing it from the buffer, you can call the LookAhead method.
If Instr(Me.LookAhead(Encodings.ASCII), Chr(13) + Chr(10)) > 0 Then TextArea1.Text = TextArea1.Text + Me.ReadAll(Encodings.ASCII) End If
You are reading the entire buffer Using "me.lookahead" and then reading all content.
When the DataAvailable event is raised, you could get a string like "my string is" and in another datavailble event you can get "complete" you may want to get "my string is complete" instead.
So check the buffer using me.lookahead (keeps the data in the buffer). If all expected data is there, then you read the part that is completed. (it's chunked).
your in the right way. the only thing is you use "Me.ReadAll" and you don't check if the end is actually "Chr(13) + Chr(10)"
You may wanna try Me.Read(Count,[encoding]) .
Instr returns an integer, you can store it, compare it and then if it's > 0 Read the line part until you got "Chr(13) + Chr(10).
Dim EOLStart As Integer = Instr(Me.LookAhead(Encodings.ASCII), Chr(13) + Chr(10)) // Same as EndOfLine.Windows Dim Line As String Do // Loop at least once to check the data EOLStart = Instr(Me.LookAhead(Encodings.ASCII), Chr(13) + Chr(10)) // Get the EOL position Line = Me.Read(EOLStart+2, Encodings.ASCII) // You should check the output of this in the debugger // EOLStart should read Chr(13) + Chr(10) too. TextArea1.Append Line // Adds the line to the textarea. Loop Until EOLStart < 1
Untested code, but it should give you a clue.
@LangueRodriguez Or, what I think works better in Xojo, put your Serial1.read code on a timer that "polls" the port every 100ms or so.
Why not create a timer that fires every 100ms with the following code:
TextArea1.Text = TextArea1.Text + Serial1.ReadAll(Encodings.ASCII)
Hi @Tim H , @LangueRodriguez , @Derk J thanks for your answer. if I don't know if you understood my problem well. Right now, when the device wants to write something about my program, it does, but in the wrong way. Sometimes some characters don't come out and sometimes it doesn't break automatically. I don't see this problem on Hercules. Please help me. I'm running out of ideas
@Mario C Hi @Tim H , @LangueRodriguez , @Derk J thanks for your answer. if I don't know if you understood my problem well. Right now, when the device wants to write something about my program, it does, but in the wrong way. Sometimes some characters don't come out and sometimes it doesn't break automatically. I don't see this problem on Hercules. Please help me. I'm running out of ideas
That is because probably you don’t understand the dataavailable event.
It can send all data bit is NOT guaranteed to do so.
Sometimes you get only a part and sometimes you get more than you want. You have to filter your data.
What do you mean by does not Break properly?
You might wanna set the correct baud rate. And other properties before the .Open call
Using lookahead keeps it in the buffer (so it remains available in the next time dataavailble is fired)
Using .Read or .ReadAll removes it from the buffer
@Derk J look at this picture. On the right is what I want to see on the left screen. On the right I send to my device a command to get values, on the left if I click view config by the same command. If I press the command several times, it gives me everything wrong "see monitor". With Hercules, no problem
I'm not sure if this will help or not. Here is the DataAvailable code I have for EVERY serial device I work with:
Dim s As String While me.BytesAvailable > 0 s = s + me.ReadAll me.Poll Wend SendDataToListeners(s)
I work with multiple barcode readers, bill acceptors, lightning and emf detectors, and card dispensers with no issues serial-wise. The biggest issue I have is that cheap Prolific USB to serial converters don't always work and the last byte doesn't like to be consumed but that's a driver issue.
This code doesn't filter out any unwanted characters (noise) and will not guarantee a very large string is completely received as it's not checking for a terminator.