Topic: Error from Arduino DUE
|By: Guest||Posted on: Sep 6 2019 at 05:56:00 AM|
|I transfer about 80 kilobytes of binary packed information to Scomm in blocks of 4 bytes, the reception in Excel VBA according to your recommendation looks like this (after opening the port in inputMode = 1- Binary, InputLen = 2 mode, the settings are made in the Scomm Icon properties on the form).|
Private Sub ArduinoDue_OnComm ()
Dim BBB () As Byte
Dim i as long
Select Case ArduinoDue.CommEvent
Do While ArduinoDue.InBufferCount> 0
BBB = ArduinoDue.Input
BI = UBound (BBB) 'Len (ArduinoDue.Input)' Read)
'If (BI> = 0) Then
If (BI> 0) Then
For i = 0 To BI
If first then
Sb (0) = BBB (i) 'ArduinoDue.Input' Read '
Sbb (USbb) = Sb (0)
Sb (1) = BBB (i) 'ArduinoDue.Input' Read '
Sbb (USbb) = Sb (1)
If ((Sb (0) = 255) And (Sb (1) = 255)) Then
KB = 0
BI = ArduinoDue.InBufferCount
ArduinoDue.InBufferCount = 0
ArduinoDue.Break = False
UserForm1.CommandButton1.Enabled = False
First = Not First
USbb = USbb + 1
End If 'If (BI> 0) Then
Loop 'Do While ArduinoDue.InBufferCount> 0
Case Is> 1000
UserForm1.TextBox1.Value = "ComPort Error occurred."
Dim Sb (0 To 1) As Byte
Dim Sbb (0 To 180,000) As Variant 'Byte' Arduino 30,000 * 4 = 120,000
Dim USbb As Long
Dim BI As Long
I want to stop reception after two bytes of FF appear in the stream
as a result, in the Sbb buffer there are (after parsing) such transformed
in hexadecimal four bytes, sometimes "supplemented" by a huge amount of 0!
About 200 out of 20,000 fours
1.84016E + 11
8.33E + 102
8.22E + 101
A parsing that produces such “eight” hexadecimal looks like this:
K = 1
Prot4 (0) = 0: Prot4 (1) = 0: Prot4 (2) = 0: Prot4 (3) = 0
Co = 0
B = usbb
For j = 0 To B '
Prot1 = Sbb (j)
' assembly of 4 bytes
Co = Co Mod 4
If (Co = 0) And (j> 2) Then
S2 = ""
For i = 0 To 3 'rearranged in Arduino !!!
S1 = Hex (Prot4 (i))
If Len (S1)
|By: Guest||Posted on: Oct 9 2019 at 10:19:05 AM|
|I don't know about the huge amount of 0. But I do notice one problem in your code.|
You set .InputLen=2 which means if 2 or more bytes are in the receive buffer then .Input will read 2 bytes.
But you can not assume that you will always get both FF bytes in the same OnComm event.
You know that the arduino is sending block 4 bytes so you think there will always that many bytes including your two FF in the receive buffer. But RS232 is slow over the wire and your processor is likely responding to OnComm events very quickly on the first byte and it's possible that you respond to the OnComm event and read the serial port before that second FF has arrived.
Using InPutLen=2 is not a good way to do what you want. It would be better like this:-
InputLen=0 '(To receive ALL bytes in a single read)
In the OnComm event Read all available bytes from the port into your local BBB() array.
Step through your BBB() array shifting each byte into your global Sbb() array checking each byte for FF
You are hoping to find two consecutive FF but you cannot assume that you will always get two. If you only have one FF then you need to make a note of that fact and exit the OnComm event. You may then immediately get another OnComm event this time containing that second FF that you were waiting for. Now you can clear that local BBB() array and all your bytes are in your global Sbb() array.
Remember of course that you initialised your Sbb() array with 180000 bytes so if you did not receive that many bytes then the rest of that array will be full of invalid data (likely zero)
Reply - add a comment to this topic.
You may enter letters, numbers and standard punctuation only. HTML and other scripts/tags will be rejected.