General area when it fits no where else

Moderator: Mmiscool

User avatar
By Ecoli-557
#52478 Thanks for all the assists.....
An update: I know how I need to work with the reader, I just don't know how to code it in Basic.
Mike has been a source of great help but I still am struggling to learn the subtleties of this language.
A brief text version of how it is supposed to work; The reader sends 5 bytes when it 'sees' a tag. The bytes are in hex. Bytes 1-4 are the tag's unique number and the 5th byte is the checksum. Checksum is generated by taking the 4 bytes of payload and successively XORing them such as byte1 XOR byte2 XOR byte3 XOR byte4 = checksum
So, I can provide some pseudo-code for what I need to do:
1. receive serial packet of 5 bytes into an array length of 5 bytes called 'DATA'
2. Variable CHKSUM=5th byte of DATA
3. Variable TEMPCHK=DATA(1) XOR DATA(2) XOR DATA(3) XOR DATA(4) - number in () is byte in array
4. If TEMPCHK <> CHKSUM then print "Bad Packet, else print "Good Packet"
5. Variable TAG = hextoint (mid(data,1,4)
An example using real numbers from a serial eavesdrop on the serial line-
00 82 6A 25 CD
00 XOR 82 XOR 6A XOR 25 = CD
00 82 6A 25 hex to dec is 0008546853 which is written on the tag which confirms what I need to accomplish.

Things I haven't been able to find out-
1. I don't see how to create an array with a specified length, eg DATA(5) meaning array DATA has 5 elements.
2. How to equate a variable as a specific element in the array such as variable CHKSUM=DATA(5)
3. Don't know if basic assumes the serial string is decimal or hex, need to specify hex

I think the serial branch is working, but I cannot get it to print correctly.
I am still fuzzy on variables as it relates to this version of basic and my experience in variables, arrays, bits, bytes, and words and how to get to any one of these does not help with this basic so apologies if this is written somewhere, just point me to it. I am still learning.........

For example in Picbasic Pro ( my native language):
to define a variable 'DATA' as an array of 5 byes is:
Code: Select allDATA   var   byte(5)

to define a variable 'CHKSUM' as element 5 in data array 'DATA' is:
Code: Select allCHKSUM   var   DATA(5)

Just looking for clarification and thanks again to all who are trying to help - especially Mike.

Regards to All,
Steve
User avatar
By bugs
#52490 Not dealing with the serial input method just now (olympics) -perhaps will test tomorrow evening.
This rough code should give you a start on the rest of the stuff. You kept confusing me by saying there were 5 bytes in serial string - this is not true - there are 14 ASCII characters (assuming no c/r l/f) representing the coded 5 bytes.

Code: Select all'assume you have obtained the serial string below

'start with a test data string of 14 characters
ip$="00 82 6a 25 cd"

'dimension string and numeric arrays
dim a$(4)
dim b(4)

'split the input string into pairs of ASCII characters
i = 0
for ptr = 1 to 13 step 3
 a$(i) = mid(ip$,ptr,2)

'store the numeric values of the hex
 b(i) = hextoint(a$(i))
serialprintln b(i)
 i = i+1
next i

'calculate the crc
crc = b(0) xor b(1) xor b(2) xor b(3)

if crc <> b(4) then
 print "bad data"
else
 print "ok"

'reconstruct the hex value (without the spaces)
 tag$ = a$(0) & a$(1) & a$(2) & a$(3)
 print hextoint(tag$)
endif

end

User avatar
By Ecoli-557
#52493 Thanks!
Not sure I understand your meaning of the 14 ASCII chars in the 5 bytes. The 1st 4 are the decimal number which is stored in HEX. The last byte is the checksum. I do understand the array info and it looks interesting.
I am currently working something that Mike put me on so I will need to finish that before I get back to this.
Your code is informative.
Regards,
Steve
User avatar
By bugs
#52496 Ok - the seedstudio device sends 00space82space6Aspace25spaceCD.
If you type that normally you will count 14 keystrokes.
That is what the serial port sees.
Printable ASCII characters are used to prevent problems decoding pure binary.
The program assembles these 14 characters to give the hex representation.

The serial reception turns out to be easy just as Mike said - using his branch at the start of the previous code I can make my laptop teminal program send the characters and they are printed on the browser screen BUT you have to manually REFRESH the browser as otherwise there is no indication of anything being printed.
Modified code below.

Code: Select allserialbranch [serialin]
wait

[serialin]
delay 1000   ' give it some time to catch all of the string.
serialinput ip$
print "received:" & ip$

'dimension string and numeric arrays
dim a$(4)
dim b(4)

'split the input string into pairs of ASCII characters
i = 0
for ptr = 1 to 13 step 3
 a$(i) = mid(ip$,ptr,2)

'store the numeric values of the hex
 b(i) = hextoint(a$(i))
 i = i+1
next i

'calculate the crc
crc = b(0) xor b(1) xor b(2) xor b(3)

if crc <> b(4) then
 print "bad data"
else
 print "ok"

'reconstruct the hex value (without the spaces)
 tag$ = a$(0) & a$(1) & a$(2) & a$(3)
 print hextoint(tag$)
endif

end



Now back to the diving...