PDA

View Full Version : Receiving Packet Array In Usart



crhomberg
- 18th April 2007, 18:01
I have a system that uses a control protocol of 8 bytes where it has a start byte which is always $A0 and an End byte which is always $AF. The bytes in between are for control and the 8th one is the checksum (I am not worried about that.
I got my USART recieving the data fine byte by byte but cannot but them in separate variables in correct order or in an array with 8 values.
I wrote this code but it just stays dead.
I need the interrupts as the program has to use the bytes to control a camera after it receives the packet and maybe another may come to cancel the previous.

My Code:
'************************************************* *******
'* PELCO PROTOCOL RECEIVER TO RECEIVE 8 BYTES WHEN INTERRUPTED *
'* PACKETS ALAYS START WITH BYTE1 = $A0 AND BYTE7 = $AF *
'************************************************* ********
@ device hs_OSC, wdt_on, pwrt_on, protect_off
define osc 20
Include "modedefs.bas"

RCSTA = %10010000 'Enable the Usart receive function
SPBRG = 64 'Set to 4800 bps
INTCON = %11000000 'Enable Global & Periferal Interrups
PIE1.5=1 'enable receive interrupt flag

trisc.6=0
TRISC.7=1 'usart input pin
TRISA.0=0


test var porta.0 'Output pin to PC to test values received
counter var word 'Used in the keep-busy loop
BAUD CON 16390



PELCO VAR BYTE[8] 'Packet of data received from the input
BYTECOUNT VAR BYTE 'Counter to put each byte in it's correct position

BYTECOUNT=0
PELCO=0

'---------------------------------jump over your interrupt routines
goto main
'-----------------------------------------------------------------

on interrupt goto myroutine


'******************* interrupt routine *********************
disable

myroutine:
if PIR1.5=1 then
PELCO[BYTECOUNT]=RCREG
IF RCREG=$AF THEN
GOSUB SENDATA
PIR1.5=0
ELSE
BYTECOUNT=BYTECOUNT+1
GOTO MYROUTINE
ENDIF
ENDIF

resume
enable
'************************************************* **

main:

'****************** Loop to keep the PIC busy *************

if counter=5000 THEN 'just to keep the pic busy to make sure the
toggle portd.0 'Toggle LED connected to this pin.
counter=0 ' interrupts are working.
else
counter=counter+1
endif

goto main

end

'********** Send data to PC to check if correct ********

SENDATA:
serout2 test,BAUD,[HEX PELCO[0],HEX PELCO[1],HEX PELCO[2],HEX PELCO[3],HEX PELCO[4],HEX PELCO[5],10,13]
RETURN

'***********************************************

skimask
- 18th April 2007, 18:12
SPBRG = 64 'Set to 4800 bps
-------------double check this...

-------------Are your A/D pins set to analog or digital?

BAUD CON 16390
-------------double check this too...

myroutine:.................
GOSUB SENDATA
...............................
resume
enable

-----------Not good programming practice to jump out of an interrupt routine and hope it comes back into it every time...might not....

if counter=5000 THEN 'just to keep the pic busy to make sure the
toggle portd.0 'Toggle LED connected to this pin.
counter=0 ' interrupts are working.
else
counter=counter+1
endif

--------------Is the LED toggling?

crhomberg
- 18th April 2007, 18:32
The value of SPBRG at 64 works fine when I tested byte by byte.

My serout2 on PORTA.0 is working fine and the loop is looping (led toggling).
The problem is that as I send data packets to the input it immediatley gets a buffer overrun error.

Regards


Chris

skimask
- 18th April 2007, 18:38
The problem is that as I send data packets to the input it immediatley gets a buffer overrun error.


Which input? The PICs input, PC's input, camera's input...

crhomberg
- 18th April 2007, 18:44
I used to receive the packets using SERIN2 and it works quite well but when the data comes one after another too fast I start loosing packets while the program is out doing other things after the serin2 times out.


'*************** Variables for Receiving Pelco-P data *********

data_1 VAR BYTE ' Header
data_2 VAR BYTE ' ID Camera
data_3 VAR BYTE ' 1st Data
data_4 VAR BYTE ' 2nd Data
data_5 VAR BYTE ' 3th DAta
data_6 VAR BYTE ' 4th DAta
data_7 VAR BYTE ' Trailer
data_8 VAR BYTE ' Checksum

'*************** CONSTANTS ***********************
baud con 16390'Baud rate for test pin output



START:


'**RECEIVE DATA FROM RS485 AND SEE IF IT IS A PELCO-P COMMAND ****

Receiving:


SERin2 DATAIN,188,1000,timeout,[data_1,data_2,data_3,data_4,data_5,data_6,data_7,d ata_8]

timeout:

if (data_1 <> $A0) then 'Check if the 1st byte is Pelco ($A0).
goto receiving
endif

etc.... etc do the decoding....

crhomberg
- 18th April 2007, 18:46
I have my PIC receiving data from a camera PTZ control console using RS485 which sends out data every time I move the joystick telling the PIC what to do. I have a RS485 to TTL converter on the input of the PIC

Regards

Chris

skimask
- 18th April 2007, 18:58
Send out bytes using serout one byte at a time instead of a whole string of bytes and in between sending out bytes, do a quick serin check to see if anything is there.

crhomberg
- 18th April 2007, 19:30
I don't have a choice, the data always comes in groups of 8.
The serout is just for debugging, not really needed finally.
I must be able to read in 8 bytes and then check if the 1st one is $A0 and the 7th $AF. When that is true I can use the data. The packets come at 4800bps and in bursts relatively far apart (That is how I got away with serin2 until now, it just lost packets from time to time)
I need to stop what the program is doing when that 1st byte arrives and then channel in the next 7 into an array or sepparate vars.

Regards

Chris

sougata
- 19th April 2007, 15:51
Hi,

I have done an application where a PIC 18F452 is connected in parallel with a dumb terminal. It parses incoming strings on the fly in an asm based INT routine and keeps required data in specified variables.For your app @4800 try this.

1. Set up a ring buffer.
2. Within the interrupt hunt for the trailer, if found set a flag.
3. Jump back to the header location to find if it was a complete command set.
4. Verify Checksum
Then you now where your data of interest are and handle them in the main routine.

skimask
- 19th April 2007, 16:08
I don't have a choice, the data always comes in groups of 8. The serout is just for debugging, not really needed finally. I must be able to read in 8 bytes and then check if the 1st one is $A0 and the 7th $AF. When that is true I can use the data. The packets come at 4800bps and in bursts relatively far apart (That is how I got away with serin2 until now, it just lost packets from time to time) I need to stop what the program is doing when that 1st byte arrives and then channel in the next 7 into an array or sepparate vars.
Regards
Chris

Put that input on portB and use the 'interrupt on change' feature...
I.E. - you're transmitting, something starts coming in on the serial line. Stop transmitting immediately and start receiving.

What you're trying to do isn't impossible. It's just getting past the PicBasicPro serial commands and using straight hardware and interrupts to handle it in the 'background'.
I suggest playing around with interrupts first, doesn't matter which one, just play with them. Then work your way up to using the serial port, both RX and TX, use some sort of buffering setup.

crhomberg
- 19th April 2007, 19:21
I don't understand how to setup a ring buffer, is this in the main program?
How does a ring buffer work?
Is this using asm interrupts or ON INT?

Darrel Taylor
- 19th April 2007, 23:54
Chris,

You already have a buffer, it doesn't need to be a "ring".
But it does need to work properly.

In your original routine...
'******************* interrupt routine *********************
disable

myroutine:
if PIR1.5=1 then
PELCO[BYTECOUNT]=RCREG
IF RCREG=$AF THEN
GOSUB SENDATA
PIR1.5=0
ELSE
BYTECOUNT=BYTECOUNT+1
GOTO MYROUTINE
ENDIF
ENDIF

resume
enable

When testing the value of RCREG, it also removes a byte from the USARTs buffer (if one's available).

And, there's currently no way to reset BYTECOUNT, it just keeps counting, which then overwrites memory areas that you don't want it to.

Try this...
'******************* interrupt routine *********************
disable

myroutine:
if PIR1.5=1 then
temp = RCREG
if temp = $A0 then BYTECOUNT = 0
if BYTECOUNT < 8 then
PELCO[BYTECOUNT]=temp
BYTECOUNT=BYTECOUNT+1
IF (BYTECOUNT = 8) and (temp=$AF) THEN GOSUB SENDATA
GOTO MYROUTINE
ENDIF
ENDIF

resume
enable


HTH,

crhomberg
- 20th April 2007, 17:51
Thanks for the example code, I modified it to my variable names and included the main loop but it just gives me vales of 0 for BUFFER, what have I done wrong. I checked if it was interrupting and it is.

Here is my code:

'************************************************* **
@ device hs_OSC, wdt_on, pwrt_on, protect_off
define osc 20
Include "modedefs.bas"
trisc.6=0
TRISC.7=1
TRISA.0=0


RCSTA = %10010000 ' ENABLE USART AND SET TO CONTINUOUS RECEIVE
SPBRG = 64 'SET TO 4800bps
INTCON = %11000000 'ENABLE GLOBAL & PERIFERAL INTERRUPTS
RCIF VAR PIR1.5 'FLAG IS THE INTERRUPT WHEN A BYTE IS IN THE USART BUFFER
RCIE VAR PIE1.5
OERR VAR RCSTA.1
CREN VAR RCSTA.4
test var porta.0
BAUD CON 16390


BUFFER VAR BYTE[8] 'BUFFER FROM USART TO COLLECT THE 8 BYTES
BYTECOUNTER VAR BYTE 'USED TO COUNTER BYTES INTO THE BUFFER
BFULL VAR BIT 'FLAGS THAT THE BUFFER HAS ALL 8 BYTES
VALUE VAR BYTE 'A TEMP SINGLE BYTE RECEIVED FROM THE USART
COUNTER VAR WORD 'USED FOR LOOP IN MAIN PROGRAM


BUFFER = 0
BYTECOUNTER = 0
BFULL = 0
VALUE = 0
RCIE=1 'ENABLE USART RECEIVE INTERRUPT FLAG

ON INTERRUPT GOTO GETDATA

GOTO MAIN:


disable


GETDATA:

if RCIF=1 then 'IF RECEIVE FLAG IS ON

VALUE=RCREG 'READ THE VALUE IN THE USART BUFFER

IF VALUE = $A0 THEN 'CHECK TO SEE IF THE BYTE IS THE START BYTE
BYTECOUNTER=0 'AND SET THE COUNTER TO FIRST BYTE
ENDIF

IF BYTECOUNTER < 8 THEN

BUFFER[BYTECOUNTER] = VALUE

BYTECOUNTER=BYTECOUNTER+1 'INCREMENT FOR NEXT BYTE

IF (BYTECOUNTER = 8) AND (VALUE=$AF) THEN
TOGGLE PORTD.0
GOTO MAIN
ENDIF

ENDIF

'CHECK IF OVERRUN HAS OCCURRED:

IF OERR=1 THEN
Serout2 test,BAUD,["OERR",10,13] 'FOR DEBUGGING SHOW ERROR
CREN=0 'STOP CONTINUOUS RECEIVE
CREN=1 'RESTART CONTINUOS RECIEVE & CLEAR THE FLAG

BYTECOUNTER=0 'RESET COUNTER
GOTO GETDATA
ENDIF

ENDIF

RESUME

ENABLE
'===============================================


MAIN:

ON INTERRUPT GOTO GETDATA

if counter= 5000 THEN 'SEND PELCO VALUE EVERY FEW LOOPS

serout2 test,BAUD,["BUFFER=",HEX BUFFER.0,HEX BUFFER.1,10,13]
'(JUST CHECK 1ST 2 VALUES)

toggle portd.1 'LED TO INDICATE TO ME THAT THE LOOP IS LOOPING

counter=0
else
counter=counter+1
endif

goto main

end

crhomberg
- 20th April 2007, 19:22
I modified my code and am getting the 8 bytes but seem to keep running into buffer overflows. Can you see an error in my interrupt routine?

'************************************************* ***************
@ device hs_OSC, wdt_on, pwrt_on, protect_off
define osc 20
Include "modedefs.bas"
trisc.6=0
TRISC.7=1
TRISA.0=0


RCSTA = %10010000 ' ENABLE USART AND SET TO CONTINUOUS RECEIVE
SPBRG = 64 'SET TO 4800bps
INTCON = %11000000 'ENABLE GLOBAL & PERIFERAL INTERRUPTS
RCIF VAR PIR1.5 'FLAG IS THE INTERRUPT WHEN A BYTE IS IN THE USART BUFFER
RCIE VAR PIE1.5
OERR VAR RCSTA.1
CREN VAR RCSTA.4
test var porta.0
BAUD CON 16390


BUFFER VAR BYTE[10] 'BUFFER FROM USART TO COLLECT THE 8 BYTES
BYTECOUNTER VAR BYTE 'USED TO COUNTER BYTES INTO THE BUFFER
BFULL VAR BIT 'FLAGS THAT THE BUFFER HAS ALL 8 BYTES
VALUE VAR BYTE 'A TEMP SINGLE BYTE RECEIVED FROM THE USART
COUNTER VAR WORD 'USED FOR LOOP IN MAIN PROGRAM


BUFFER = 0
BYTECOUNTER = 0
BFULL = 0
VALUE = 0
RCIE=1 'ENABLE USART RECEIVE INTERRUPT FLAG

ON INTERRUPT GOTO GETDATA

GOTO MAIN:


disable
'====================INTERRUPT ROUTINE ==============================

GETDATA:


IF RCIF=1 THEN

VALUE=RCREG
IF VALUE = $A0 THEN 'CHECK TO SEE IF THE BYTE IS THE START BYTE
BYTECOUNTER=0
ENDIF

RCIF=0

IF BYTECOUNTER < 8 THEN
BUFFER[BYTECOUNTER] = VALUE
BYTECOUNTER=BYTECOUNTER+1
ENDIF

'CHECK IF OVERRUN HAS OCCURRED:

IF OERR=1 THEN
Serout2 test,BAUD,["OERR",10,13] 'TO SHOW OVERFLOW
PAUSE 1000
CREN=0 'STOP CONTINUOUS RECEIVE
CREN=1 'RESTART CONTINUOS RECIEVE & CLEAR THE OVERRUN FLAG
BYTECOUNTER=0 'RESET COUNTER AS PART OF PACKET HAS BEEN LOST SO START AGAIN
BUFFER=0
GOTO GETDATA
ENDIF

IF BYTECOUNTER=7 AND BUFFER[0]=$A0 AND BUFFER[6]=$AF THEN

GOTO MAIN
ELSE
GOTO GETDATA
ENDIF
ENDIF





RESUME

ENABLE
'================================================= ====================================

MAIN:

'ON INTERRUPT GOTO GETDATA

if counter= 1000 THEN 'SEND PELCO VALUE EVERY FEW LOOPS
serout2 test,BAUD,["BUFFER=",HEX BUFFER[0],",",HEX BUFFER[1],",",HEX BUFFER[2],",",HEX BUFFER[3],",",HEX BUFFER[4],",",HEX BUFFER[5],",",HEX BUFFER[6],",",HEX BUFFER[7],10,13] 'JUST CHECK 1ST 2 VALUES
toggle portd.1 'LED TO INDICATE TO ME THAT THE LOOP IS LOOPING
counter=0
else
counter=counter+1
endif

goto main

end

'================================================= ====================================

Darrel Taylor
- 20th April 2007, 20:40
You've got data coming in at 4800 baud, and data going out a 38400.

So you can send 8 bytes in the time it takes to receive 1 byte.
The USART has a 2 byte buffer, so you can only send 16 bytes (or less) to the PC without expecting to see overflows.

But, In this statement...

serout2 test,BAUD,["BUFFER=",HEX BUFFER[0],",",HEX BUFFER[1],",",HEX BUFFER[2],",",HEX BUFFER[3],","
,HEX BUFFER[4],",",HEX BUFFER[5],",",HEX BUFFER[6],",",HEX BUFFER[7],10,13] 'JUST CHECK 1ST 2 VALUES

depending on the values of the data, it will send up to 33 bytes.

Since you are using ON INTERRUPT, the next interrupt can't happen untill the serout2 statement has finished.
<br>

crhomberg
- 20th April 2007, 22:20
Thanks Darryl,

You hit my problem on the spot, why are the small simplicities the ones that give the most grief?
I am going to look into hardware interrupts to allow me to break out of a serout2 command. Do you have any code that works well? If not, don't worry.
I feel I'm on the right track now.

Thanks All,

Chris

Darrel Taylor
- 20th April 2007, 23:25
I am going to look into hardware interrupts to allow me to break out of a serout2 command. Do you have any code that works well?
I ... uh ... er ... hem ... haw ...

Well, there's this ...

DT-INTS-14
http://www.darreltaylor.com/DT_INTS-14/intro.html
<br>

skimask
- 21st April 2007, 01:04
Thanks Darryl,
You hit my problem on the spot, why are the small simplicities the ones that give the most grief? I am going to look into hardware interrupts to allow me to break out of a serout2 command. Do you have any code that works well? If not, don't worry. I feel I'm on the right track now. Thanks All,
Chris

Or figure out a way you can send one byte at a time and check for an input in between bytes...

crhomberg
- 22nd April 2007, 12:35
Tomorrow I want to try Darryl Taylor's instant interrups, I hope that will help.
When I removed or shortened the serout2 string all worked fine so I think if an assembler interrupt can stop the Serout2 before it completes things should work well. Even If I loose a few serout2 lines it is less important then loosing input packets.

Regards

Chris

mister_e
- 22nd April 2007, 20:58
Pretty sure DarrEl's routine will help.

Nothing bad to wait between each character to send... kinda character pacing.. sort of.

crhomberg
- 23rd April 2007, 22:46
Sorry Darrel for spelling your name wrong, I have only tried your blinky program so far but I am really impressed and exited.
Now I just have to do the same but with the interrupt from the USART receiver.

Thanks

crhomberg
- 23rd April 2007, 23:47
Dear Darrel,

If you are still there, please read this adaption of my code to use your instant interrupts.
Something is still not right, it does not seem to get the values.

'************************************************* ***************

'JUST NEED TO SEND THE VALUE IN EACH BYTE ONCE THE PACKET HAS BEEN COLLECTED

'************************************************* ***************
'@ device hs_OSC, wdt_on, pwrt_on, protect_off
define osc 20
Include "modedefs.bas"
INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts

trisc.6=0
TRISC.7=1
TRISA.0=0


RCSTA = %10010000 ' ENABLE USART AND SET TO CONTINUOUS RECEIVE
SPBRG = 64 'SET TO 4800bps
INTCON = %11000000 'ENABLE GLOBAL & PERIFERAL INTERRUPTS
RCIF VAR PIR1.5 'FLAG IS THE INTERRUPT WHEN A BYTE IS IN THE USART BUFFER
RCIE VAR PIE1.5
OERR VAR RCSTA.1
CREN VAR RCSTA.4
test var porta.0
BAUD CON 16390
start var bit

BUFFER VAR BYTE[8] 'BUFFER FROM USART TO COLLECT THE 8 BYTES
PELCO VAR BYTE[8]
BYTECOUNTER VAR BYTE 'USED TO COUNTER BYTES INTO THE BUFFER
BFULL VAR BIT 'FLAGS THAT THE BUFFER HAS ALL 8 BYTES
VALUE VAR BYTE 'A TEMP SINGLE BYTE RECEIVED FROM THE USART
COUNTER VAR WORD 'USED FOR LOOP IN MAIN PROGRAM
GOT VAR BIT 'USED TO PREVENT REPEATED SENDING OF SAME PACKET VIA SEROUT2

ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag? GOTO MAIN:
INT_Handler RX_INT, _GETDATA, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor disable
ENDASM

@ INT_ENABLE RX_INT ; enable RX USART interrupts



PELCO=0
BUFFER = 0
BYTECOUNTER = 0
BFULL = 0
VALUE = 0
RCIE=1 'ENABLE USART RECEIVE INTERRUPT FLAG
GOT=0



'================================================= ====================================

MAIN:



if counter= 5000 THEN 'SEND PELCO VALUE EVERY FEW LOOPS

IF GOT=1 THEN 'IF THE PACKET IS NEW THEN SEND ELSE JUST KEEP LOOPING
serout2 test,BAUD,[HEX BUFFER[0],",",HEX BUFFER[1],",",HEX BUFFER[2],",",HEX BUFFER[3],",",HEX BUFFER[6],10,13] 'JUST CHECK 1ST 2 VALUES
GOT=0
ENDIF
toggle portd.1 'LED TO INDICATE TO ME THAT THE LOOP IS LOOPING
counter=0
else
counter=counter+1
endif

goto main

end


'====================INTERRUPT ROUTINE ==============================

GETDATA:

TOGGLE PORTD.0
IF RCIF=1 THEN

VALUE=RCREG
IF VALUE = $A0 THEN 'CHECK TO SEE IF THE BYTE IS THE START BYTE
BYTECOUNTER=0
ENDIF


IF BYTECOUNTER < 8 THEN
BUFFER[BYTECOUNTER] = VALUE 'ADD BYTE TO PACKET
BYTECOUNTER=BYTECOUNTER+1 'INCREMENT COUNTER
ENDIF

'CHECK IF OVERRUN HAS OCCURRED:

IF OERR=1 THEN
Serout2 test,BAUD,["OERR",10,13] 'TO SHOW OVERFLOW
PAUSE 1000
CREN=0 'STOP CONTINUOUS RECEIVE
CREN=1 'RESTART CONTINUOS RECIEVE & CLEAR THE OVERRUN FLAG
BYTECOUNTER=0 'RESET COUNTER AS PART OF PACKET HAS BEEN LOST SO START AGAIN
BUFFER=0
GOTO GETDATA
ENDIF

'*** PACKET MUST START WITH $A0 AND HAVE $AF AS IT'S 7TH BYTE. ******
IF BYTECOUNTER=7 AND BUFFER[0]=$A0 AND BUFFER[6]=$AF THEN
GOT=1 'INDICATE THAT THIS IS A NEW PACKET
GOTO MAIN 'RETURN TO MAIN PROGRAM
ELSE
GOTO GETDATA 'GO BACK AND WAIT FOR NEXT PACKET
ENDIF
ENDIF


@ INT_RETURN
'================================================= ====================================

skimask
- 24th April 2007, 00:01
Which PIC are you using?
(unless I missed something, I don't think that's ever been established...)

crhomberg
- 24th April 2007, 00:41
I am using the PIC16F977A

Archangel
- 24th April 2007, 00:41
http://www.picbasic.co.uk/forum/showthread.php?t=3251

Unless I misread your code as follows:


@ device hs_OSC, wdt_on, pwrt_on, protect_off

You are using PM as the assembler, and according to this link, DT interrupts require MPASM.
HTH
JS

mister_e
- 24th April 2007, 01:01
i know i miss something here, but i feel lazy to read that carefully... Anyways is the SEROUT2 baudrate should be really 38400 baud, Driven, Inverted, No parity?

skimask
- 24th April 2007, 04:02
I am using the PIC16F977A

I missed those datasheets :)

Darrel Taylor
- 24th April 2007, 04:13
Oh my, look what happened while I was out trying to make a buck, (a.k.a. at work).

That's the most "unless I missed something"'s I've ever seen in a row. :eek:

Chris,

You can't jump out of the Interrupt handler back to the main loop.
Instead, you should just use your GOT flag to indicate reception, then let the main loop handle it at it's leisure.
IF BYTECOUNTER=7 AND BUFFER[0]=$A0 AND BUFFER[6]=$AF THEN
GOT=1 'INDICATE THAT THIS IS A NEW PACKET
GOTO MAIN 'RETURN TO MAIN PROGRAM
ELSE
GOTO GETDATA 'GO BACK AND WAIT FOR NEXT PACKET
ENDIF
ENDIF

@ INT_RETURN


I think this might work better for the Main loop ...

MAIN:
IF GOT=1 THEN 'IF THE PACKET IS NEW THEN SEND ELSE JUST KEEP LOOPING
GOT=0
GOSUB ShowBuffer
ENDIF

if counter= 5000 THEN 'SEND PELCO VALUE EVERY FEW LOOPS
counter=0
GOSUB ShowBuffer
toggle portd.1 'LED TO INDICATE TO ME THAT THE LOOP IS LOOPING
ENDIF

counter=counter+1
goto main


'================================================= =========================
ShowBuffer:
serout2 test,BAUD,[HEX BUFFER[0],",",HEX BUFFER[1],",",HEX BUFFER[2], _
",",HEX BUFFER[3],",",HEX BUFFER[6],10,13] 'JUST CHECK 1ST 2 VALUES
return


But, you'll still need to fix the Interrupt handler.

HTH,

sougata
- 24th April 2007, 14:47
Hi,

In your code you are assuming that the first byte at buffer[0] is the pelco-p header.


IF BYTECOUNTER=7 AND BUFFER[0]=$A0 AND BUFFER[6]=$AF THEN
GOT=1 'INDICATE THAT THIS IS A NEW PACKET
GOTO MAIN 'RETURN TO MAIN PROGRAM
ELSE
GOTO GETDATA 'GO BACK AND WAIT FOR NEXT PACKET
ENDIF
ENDIF


This might not be the case as you may have a dataset misaligned say for one missed byte or corrupted data. So you actually do not know that if your buffer is perfectly aligned. I think you should consider a better algo for checking the PELCO strings.

I assume are using serout2 for your debug purposes. Why not use the TX part of the USART? Yes limitation are that you need to use the same speed as the RX. DT's INT routines takes a finite time to take action in your PBP routine as it saves all the variables for you and checks all the flags and execution orders (aka all the hardwork that we were supposed to do) but please note that it responds to the interrupt instantly. This may screw up the soft serial out routines of PBP as it will introduce timing jitters in the output serial streams. Hope Darrel can enlighten more.

I wish that I actually find some spare time this weekend to get something useful for you. Hope you succeed before that.

crhomberg
- 24th April 2007, 20:41
It's finally running just as I wanted it,

Thanks very much to skimask, Suagata ans especially Darrel Taylor for your help. I could never have got it right. I now can use this code as a "plug in" for all my 8byte protocol coms projects.

I hope I will be able to help you someday as you helped me.

Best Regards

Chris Rhomberg