I have 6 PICs chattering amongst themselves in a gas mixer. The comms is cable based, not wireless, but there are similarities. I found I had to send a number of preamble characters to get the receivers reliably synchronised. The number of preamble characters and the value of the character need to be chosen with some care in an attempt to get a 'balanced' signal on the line. Sending four $AA or $55 characters then "$" as a 'start of message' works a treat for me.
/code:
Snip from RxPacket:
GetSoM:
serin2 msg, 84, 250, rxpacketdone, [wait ("$")]
GetPktInfo:
for C_A = 0 to 1
serin2 msg, 84, [rxchar[C_A]]
chksum = chksum + rxchar[C_A]
next C_A
pkttype = rxchar[0] : msglen = rxchar[1]
GetPayLoad:
for C_A = 2 to (msglen + 1)
serin2 msg, 84, [rxchar[C_A]]
chksum = chksum + rxchar[C_A]
next C_A
GetEoMandChkSum:
for C_A = 2 to 3
serin2 msg, 84, [rxchar[msglen + C_A]]
next C_A
BadMsgCheck:
if rxchar[msglen + 2] <> "*" then getmessage 'should be EoM.
if rxchar[msglen + 3] <> chksum then getmessage 'try again
Snip from TxPacket:
'Send PreAmble
chksum = 0
for C_A = 1 to precnt
serout2 msg, 84,[preambl]
next C_A
'Send Start of Message
serout2 msg, 84,["$"]
'Send message and accumulate ChkSum
for C_A = 0 to (msglen + 1)
serout2 msg, 84, [txchar[C_A]]
chksum = chksum + txchar[C_A]
next C_A
'Send EoM and ChkSum
txchar[msglen+2] = "*" : txchar[msglen+3] = chksum
serout2 msg, 84, [txchar[msglen+2], txchar[msglen+3]]
pause 5 'time enough for recipient to let go of Ack line
if ack = 0 then sendmessage 'recipient is requesting a repeat
TxPacketDone:
'Message sent or timed out so release CommsBus
TRISB = %11111111
/endcode
HTH
BrianT
Bookmarks