Parsing Serial data stream
Hi All,
Wonder if anyone would take a look at this and tell me where I'm missing it.
This started as http://www.picbasic.co.uk/forum/showthread.php?p=64209 and I have made a bit of progress but I could still use some fresh eyes (and more brain cells) I must be searching for the wrong terms as I can not seem to come up with how to handle the data once I get it.
This seems like it would give me the different bytes (sync, address, and command) but it stumbles when it tries to get the command byte. I get an echo for the sync and the address, but it won't grab the command. Seems to lock up too. Any suggestions? At least a link or two where I can learn more. BTW, I have been devouring Jan Axelson's Serial Port Complete book. Some help, but not on the parsing.
Code:
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
'Based on Hserin with Darryl Taylor's Instant Interrupts
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
' Program to echo incoming serial data.
' RX SYNC byte, if good: ADDRESS byte, if good: COMMAND
' MPASM, LabX-1 , PBP2.47, Using PIC 16F877A @ 4mHz, 9600 Baud
' Using Hardware USART and MAX232 to MCS Serial Communicator on PC
DEFINE OSC 4
DEFINE HSER_RCSTA 90h ' enable serial port,
define HSER_TXSTA 24h ' enable transmit,
define HSER_SPBRG 25 ' set baudrate to 9600
DEFINE HSER_CLOERR 1 ' automatic clear overrun error
TRISD = $00000000 'D to outputs for the LEDs
TRISC = %10000000 ' PORTC.7 is the RX input, PORTC.6 is the TX output
ADCON1 = %00001111 'Set up ADCON1 register no matter what yr doing!!!
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
INCLUDE "MODEDEFS.BAS" ' Include Shiftin/out modes
INCLUDE "DT_INTS-14.bas" ' Base Interrupt System
INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
' Variable definition
'::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
RCIF VAR PIR1.5 ' Receive interrupt flag (1=full , 0=empty)
TXIF VAR PIR1.4 ' Transmit interrupt flag (1=empty, 0=full)
led0 var PORTD.0
led1 var PORTD.1
led2 var PORTD.2
led7 var PORTD.7
led0count var byte
led2count var byte
state var byte 'storage for condition bits
holdoff var word
CmdBuf var byte 'command for the pwm or acknowledge
SerialData var byte '
ID con "A" 'simple address, later 256
led2count = %00000001
state = 0
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler RX_INT, _Getbytes, PBP, no
endm
INT_CREATE ; Creates the interrupt processor
ENDASM
@ INT_ENABLE RX_INT ; enable RX_INT interrupts
':::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
hserout ["start"] 'splash screen to verify comm
HSEROUT [10] ' CR to start new line on terminal program
Mainloop:
for holdoff = 1 to 500
pause 1
next holdoff
led0count = led0count + 1
led0 = led0count.0 'toggle led every loop for heartbeat
goto Mainloop
'*********** 'ISR for RX_int interrupt ***************************************
Getbytes:
While RCIF = 1 ' clear the buffer
HSERIN 100,error,[Serialdata] ' Get serial
led2count = led2count + 1 ' Incr heartbeat
Wend
led2 = led2count.0 'led to confirm program went to RX ISR
if SerialData = "U" then GoodSync
if serialdata = ID then GoodAdr
if state = %00000011 then GoodCmd 'good sync and address(ID)
state.1=0 'If not Sync & ID, retry
@ INT_RETURN
GoodSync:
hserout ["Sync: ",SerialData]
HSEROUT [10] 'Carriage Return
state.0 = 1 'set sync good flag
@ INT_RETURN
GoodAdr:
hserout ["Adr: ",serialdata]
hserout [10]
state.1 = 1 'got a good address
@ INT_RETURN
GoodCmd:
CmdBuf = serialdata 'store the command
hserout ["Cmd: ",CmdBuf] 'send Command and CR
HSEROUT [10]
state.1=0 'reset the address verification
@ INT_RETURN
error:
Toggle led7
@ INT_RETURN
end
Thanks for any direction.... At least a sound a horn so that I can get out of the fog :-)
Thanks
Mark