PDA

View Full Version : Capture and compare serial data



Dennis
- 4th April 2010, 22:23
Hi Guys

Haven't been around in a while and I hope everyone is well :-)

I am trying to do a kind of wireless capture and compare but I have a problem on the receiving end that I lookings for some suggestions on.

Heres the pseudo code:
1.send/transmit 4 bytes 4 times (done and it's working!)

2.receive the 4 bytes 4 times (the receiver receives the 4 bytes 4 times successfully -using serial port tool)

3.Store the 4 instances/lines of 4 bytes (Here's where I have the problem)

4.Compare lines 3 and 4 (or any of the lines with one of the others.This is the other problem I have)

Heres my code:


'Variables begin here
SYNK VAR BYTE
SYNK = $7E '126 DECIMAL, BINARY 11111110
TRAIN VAR BYTE
TRAIN = $B3
WNUM VAR BYTE
XNUM VAR BYTE
YNUM VAR BYTE
ZNUM VAR BYTE
WNUM = 31
XNUM = 1
YNUM = 2
znum = 2
'end of variables

main:
low led 'set led low
If GPIO.2 = 1 then main 'check button

send:

for counter = 0 to 3

DEBUG train,train,train,train,train,synk,WNUM,XNUM,YNUM, ZNUM,$d,$a

next counter
high led 'set led high

pause 200
goto main
end


So as you can see I use DEBUG to send the same this to the serial port 4 times which renders this output in the serial tool:


³³³³³~
³³³³³~
³³³³³~
³³³³³~

which converted to DECIMAL looks like this:


17917917917917912631122
17917917917917912631122
17917917917917912631122
17917917917917912631122

PERFECT but .....

How can I receive and store each instance of WNUM,XNUM,YNUM,ZNUM.

In other words I would like to have four instances of WNUM,XNUM,YNUM,ZNUM, so I could compare each instance of WNUM,XNUM,YNUM and ZNUM.

What is the best way to do it ?

Would I use 4 different arrays?
For example array1 would hold the first line conatining the values for WNUM,XNUM,YNUM,ZNUM.
Array2 would hold the values for the second line and array3 for line 3 and array4 for line 4.

Some help would really be appreciated.

Kind regards

Dennis

Dennis
- 5th April 2010, 01:06
In my frustration @ myself and my rush to post I really messed up the english for this line

I am trying to do a kind of wireless capture and compare but I have a problem on the receiving end that I lookings for some suggestions on.

What it should read is :
I am trying to do a kind of wireless capture and compare but I have a problem on the receiving end that I am looking for some suggestions on.

Looking forward to your replies

Thanks so much

Dennis

mackrackit
- 5th April 2010, 10:42
Hey Dennis,

An array(s) would be the way to do it. Could compare separate bits or the whole array sorta like doing a checksum. Lots of possibilities.

This may give you some ideas on how to set it up
http://www.picbasic.co.uk/forum/showthread.php?t=544

Dennis
- 5th April 2010, 20:53
Dave :-)

Thanks a million for the tip ...very helpful indeed.
Still bashing around with the options.

Now this is what prompted me to post this thread, if I use an array , should I use a byte or a word array ?
Which would be the better one?
Why choose one over the other?

No matter what type of array I choose, I think the comparison of bytes would become rather cumbersome not so ?

For example if I choose a word array then the first line received would receive the first 4 bytes(0 to 4) WNUM,XNUM,YNUM,ZNUM.
The second line would be held in byte 5 to 7 not so ?

Then to compare each line is where things start to get cumbersome I think and by that I mean lots of IF,THEN statements.
So for example to compare lines 1 (WNUM,XNUM,YNUM and ZNUM and line 2 (WNUM,XNUM,YNUM and ZNUM I might have to do something like :
MyWordArray var WORD [7]
If (MyWordArray.0 and MyWordArray.1 and MyWordArray.2 and MyWordArray.3 ) = (MyWordArray.4 and MyWordArray.5 and MyWordArray.6 and MyWordArray.7) THEN DoSomething

This doesn't look quite right :-(

That's the problem I have ... I am really battling to somehow compare each line.
Is there a way of doing this by sending and receiving everything as a STRING and comparing the entire string ?

Any more suggestions would really be appreciated.

Kind regards

Dennis

Charles Linquis
- 5th April 2010, 22:22
Sometimes, when the strings you have to compare are very long, or when there are a lot of them, rather than compare them byte-for-byte, it is easier to use a good CRC algorithm on each one, then compare the CRCs.

Dennis
- 6th April 2010, 00:31
@Charles and Dave

I definitely agree with the checksum idea , in fact, it is the checksum concept that has led to the confusion

As a reference let's use this thread (thanks Art for his input!)
http://www.picbasic.co.uk/forum/showthread.php?t=12492

SO from that thread POST #2 one possible method of checksumming is described, since it's fairly straight forward we could use that as a reference.

So the bytes I would like to transmit are declared as
WNUM,XNUM,YNUM and ZNUM respectively.

***NOTE that this fairly trivial when comparing each byte of a single transmission BUT I am transmitting the same thing 4 times and that's whats confusing me.
The reason I want to do it 4 (or more) times is because from what I have seen in my tests the first one and/or second received line is usually damaged/corrupted so I figured it is probably best if I compare the third and 4th transmitted lines. Does that make sense?

Here's an example of the transmit code:



WNUM as byte
XNUM as byte
YNUM as byte
ZNUM as byte
CHECKSUM as byte
CHECKSUM = $3F

'now do the XOR checksumming
Checksum = Checksum ^ WNUM
Checksum = Checksum ^ XNUM
Checksum = Checksum ^ YNUM
Checksum = Checksum ^ ZNUM

'and now TX the data 4 times
main:
low led 'set led low
If GPIO.2 = 1 then main 'check button

send:

for counter = 0 to 3

DEBUG train,train,train,train,train,synk,WNUM,XNUM,YNUM, ZNUM,CHECKSUM,$d,$a

next counter
high led 'set led high

pause 200
goto main
end





On the receiver side the code (as per the thread referenced earlier) would look like this:




'Variables begin here

SYNK VAR BYTE
SYNK = $7E
WNUM VAR BYTE
XNUM VAR BYTE
YNUM VAR BYTE
ZNUM VAR BYTE
CHECKSUM var byte 'this holds the received checksum
CHECKSUM_CHECK var byte 'hold the XOR'd value
Checksum_Check = $3F 'this is the checksum value to XOR with

'Variables end here

DEBUGIN [WAIT(SYNK),WNUM,XNUM,YNUM,ZNUM,CHECKSUM]
' wait for SYNK, when received store next 4 bytes in variables WNUM,XNUM,YNUM,ZNUM
'then send these to hyperterminal or serial program or wherever

'checksum the received bytes using XOR with 3F
Checksum_Check = Checksum_Check ^ WNUM
debug "xNUM checksum_check is ",HEX checksum_check
Checksum_Check = Checksum_Check ^ XNUM
debug "wNUM checksum_check is ",HEX checksum_check
Checksum_Check = Checksum_Check ^ YNUM
debug "yNUM checksum_check is ",HEX checksum_check
Checksum_Check = Checksum_Check ^ ZNUM
debug "zNUM checksum_check is ",HEX checksum_check

IF Checksum_Check = Checksum THEN
DEBUG "WNUM is ",DEC WNUM,"XNUM is ",DEC XNUM,"YNUM is ",DEC YNUM,"ZNUM is ",DEC ZNUM,$d,$a
ENDIF


BUT as you can see the receive code only does its thing for each received line. I want to store all 4 lines of data then do a compare of the received lines 3 and 4.

See the problem is if only one transmission is made (one line) and there is a corruption of a byte (checksum mismatch anywhere in the line) then the TX fails. That is why I thought to send and receive 4 lines (or more) of the same thing and then do a compare.

I am totally confused (again)

PLEASE HELP..........!

Kind regards

Dennis

mackrackit
- 6th April 2010, 00:40
Not sure exactly what you are trying to do?

Send the same thing four times and if any of them fail the whole thing fails? Even if one is good?

Even doing a compare will not tell you which one is good. Only the checksum (CRC) thing will do that.

Dennis
- 6th April 2010, 00:44
Hi Dave

Send same thing 4 times
so
1
2
3
4
and compare instance 3 and 4 'cos usually correction occurs on 1 and 2
If 3 and 4 match then TX was good.

Hope that helps

Kind regards
Dennis

mackrackit
- 6th April 2010, 00:51
OK, how about this.

Receive the first two and do nothing with that data because it is most likely bad.

Do the checksum on data #3.
if good then ... STOP??
OR go on and run the check on data #4

If #4 check then GO ON.

No compare any where...

MAYBE...

Dennis
- 6th April 2010, 01:06
@ Dave

OK cool

So I Will
1. Capture all 4 lines into and array each one is 4 bytes and a checksum byte so array needs to receive 20 bytes total.
2. Check 4 for checksum match
Continue on ...

It certainly seems doable and will give it a bash right away!

Still just wondering if there is a way to capture and compare all 4 lines

Kind regards
Dennis

mackrackit
- 6th April 2010, 01:27
If you really want to do each line then I guess the way would be ...

IF array1 bit1 = array2 bit1 then
IF array1 bit2 = array2 bit2 then
IF array1 bit3 = array2 bit3 then
......on and on and on....

But something like Charles said
IF check1 = check2 then
IF check2 = check3 then
IF check3 = check4 then
Hey. it is done... :)

Charles Linquis
- 6th April 2010, 01:59
Depending on what your data looks like, you can also do some neat things with:

ARRAYREAD (name_of_first_string),(nocompare_location),(numbe r_of_chars_to_compare),[WAITSTR name_of_second_string]

Dennis
- 6th April 2010, 02:00
AHA...

That's where I'm stuck ....at step 1 I mentioned earlier ...


1. Capture all 4 lines into and array each one is 4 bytes and a checksum byte so array needs to receive 20 bytes total.

I this is the line I am using to receive the data


DEBUGIN [WAIT(SYNK),WNUM,XNUM,YNUM,ZNUM,CHECKSUM]
' wait for SYNK, when received store next 4 bytes in variables WNUM,XNUM,YNUM,ZNUM
'then send these to hyperterminal or serial program or wherever


What must be changed in order for me receive it 4 times and build the array?

I was playing around with this as a start..but it's here where I am stuck!


For counter=0 To 19
DEBUGIN [WAIT(SYNK),WNUM,XNUM,YNUM,ZNUM,CHECKSUM]
' wait for SYNK, when received store next 4 bytes in variables WNUM,XNUM,YNUM,ZNUM
'then send these to hyperterminal or serial program or wherever
counter=counter+1
Next


As always any help would be appreciated

Kind regards

Dennis

aratti
- 6th April 2010, 02:07
Hi Denis, I think you could take some advantage from this thread :http://www.picbasic.co.uk/forum/showthread.php?t=12870
In post # 4 DT is giving an interesting snippet for having 2d array in PBP.

I don't know if you have already used bi-dimensional arrays, if not let me say that they could easy your compare problem.


You cannot corrupt the variable within a FOR/NEXT cycle:

For Counter =0 to 19
.
.
.
counter=counter+1
Next Counter

You should expect some problem from this code!

Al.

Dennis
- 6th April 2010, 03:54
@ Charles ..thanks a million for the tips will check them out as soon as I have the array(s) built up.

@ Al .. Thanks for the tip there , has me wondering how I can put the 2D array to use for my a solution.

I am guessing as a start I should replace the DEBUG sommand with something like serin2 and build the array with one command ?
Something like :

SerIn2 GPIO.4,T2400,500, main,[WAIT(SYNK),STR DATAIN\4]
This would indeed create an array and store the 4 bytes and the checksum byte.
But again I want to to do this 4 times and have either one array with the contents of each time OR I could try have 4 different arrays (one created for each time) and then check the checksum values for each array.

Now I am wondering if I am doing the right thing , perhaps I should rather have more SYNK/qualifier bytes and send and receive one instance of the data and checksum that.
Problem is that sometimes some corruption occurs at the receiver side so to solve this I thought it best to send more instances of the same data.

Any more suggestions are welcome

Kind regards

Dennis

mackrackit
- 6th April 2010, 04:01
Here is something else if you want to go the route of sending "good" data the first time.
You might be able to get an idea or two... or more confused :)
http://www.picbasic.co.uk/forum/showthread.php?t=12554

Ioannis
- 6th April 2010, 10:08
Another one idea is to have the 3rd string stored in an array and then on the 4th reception compare in the serial input that string using the wait modifier
like this:

debugin [str my_array\4]

and then

debugin [waitstr my_array\4]

I'll try to put up an example soon. I got to go now... sorry

Ioannis