View Full Version : One master,multiple slavers Uart communication
Mark paul
11-08-2009, 08:24 PM
This script is our most simple and stable script to realize this kind of need:
when transmit Uart data,the master can talk to any slaver,and the slaver can only talk to the master.
on the master side,when using,you should add 3 byte Address of slaver to the beginning of your data,and this script will set this as an RPC destination,when receive data from slaver ,it will print the address of slaver,so you can know which slaver send this data,on the slaver side,you just send and receive as transparent mode.
There are some protection in case some extreme situation,like .receive packet with the same packet sequence(ID) ;print function being interrupt;packet data incomplete;
maybe synapse engineer can point out what is unnecessary.
Thanks for reply.
ArthurBR
11-17-2009, 08:24 AM
I could not use the scrip.
There was no communication with the serial port.
Mark paul
11-17-2009, 07:28 PM
I could not use the scrip.
There was no communication with the serial port.
what‘s that mean?if the slaver's address is ce 00 01,and the master’s address is ce0003(you can modify it in your script)you just send "ce0001xxxxxxxxxxxxxx"to the master's uart,the script of the master will deal with it ,and call a rpc,finish the transmit,and the slaver side,send“xxxxxxx”.
Don't forget that your Com parameters match the format of" 57600,8,n,1"
ArthurBR
11-19-2009, 08:51 AM
I tried to use the following script to the master:
from synapse.switchboard import *
serialnum=0x00
def startupEvent():
"""detectEvalBoards()"""
setPinDir(0, True) #Pin0 config as Send Data Led
writePin(0, False)
setPinDir(1, True) #Pin1 config as recv Data Led
writePin(1, False)
crossConnect(DS_STDIO, DS_UART1)
initUart(1,57600)
stdinMode(1, False) # Line Mode, Echo On
saveNvParam(13,60000) #un-use this pamar
saveNvParam(15,32)
serialnum=0
def stdinEvent(recdata):#recv Data from COM
global serialnum
if recdata[0]=='Z':
if recdata[1]=='1':
print localAddr(), #local_address
else:
lenth=len(recdata)
if serialnum>255:
serialnum=0
serialnum=serialnum+1
slaveraddr=recdata[0:3]
if lenth>5:
CRC_check=recdata[5]
if (recdata[2]=='\xff') and (recdata[1]=='\xff')and(recdata[0]=='\xff'):
mcastRpc(1,64,'remote_rpc',recdata[3:lenth],CRC_check,lenth,serialnum)
else:
rpc(slaveraddr,'remote_rpc',recdata[3:lenth],CRC_check,lenth,serialnum) # UnicastFrame
pulsePin(0,5, True)#send led
recdata=''
idsave=0
def remote_rpc(recdata,CRC_checksum,datalenth,idnum):# Print Data to COM
global idsave
global printEN
if idsave!=idnum:
if CRC_checksum==recdata[2]:
if len(recdata)==datalenth:
Slaveraddr=rpcSourceAddr()
print Slaveraddr,recdata,
pulsePin(1,5,True)
recdata=''
idsave=idnum
snappyGen.setHook(SnapConstants.HOOK_STARTUP, startupEvent)
snappyGen.setHook(SnapConstants.HOOK_STDIN, stdinEvent)
And this for the other nodes:
from synapse.switchboard import *
serialnum=0x00
def startupEvent():
"""detectEvalBoards()"""
setPinDir(0, True) #Pin0 config as Send Data Led
writePin(0, False)
setPinDir(1, True) #Pin1 config as recv Data Led
writePin(1, False)
crossConnect(DS_STDIO, DS_UART1)
initUart(1,9600)
stdinMode(1, False) # Line Mode, Echo On
saveNvParam(13,60000)
saveNvParam(15,32)
serialnum=0
def stdinEvent(recdata):#recv Data from COM
global serialnum
if recdata[0]=='Z':
if recdata[1]=='1':
print localAddr(), #local_address
else:
lenth=len(recdata)
if lenth>5:
if serialnum>255:
serialnum=0
serialnum=serialnum+1
CRC_check=recdata[2]
rpc("\x00\x36\x01",'remote_rpc',recdata,CRC_check,lenth,serialnum) # UnicastFrame
pulsePin(0,5, True)#send led
recdata=''
idsave=0
def remote_rpc(recdata,CRC_checksum,datalenth,idnum):# Print Data to COM
global idsave
global printEN
if idsave!=idnum:
if CRC_checksum==recdata[2]:
if len(recdata)+3==datalenth:
print recdata,
pulsePin(1,3,True)
recdata=''
iflag=0
idsave=idnum
snappyGen.setHook(SnapConstants.HOOK_STARTUP, startupEvent)
snappyGen.setHook(SnapConstants.HOOK_STDIN, stdinEvent)
But there was no communication between them.
what is wrong?
kbanks
11-19-2009, 10:59 AM
In the second script you have a hardcoded SNAP Address
rpc("\x00\x36\x01",'remote_rpc' ...
Is 00.36.01 really the correct address of the other node?
ArthurBR
11-19-2009, 12:51 PM
yes, the mac address of the master is 00 36 01. All are prothoboards.
Changed the speed of the slave to 57600, being equal to the master.
Now only the master receives data. I tried to send data from slave to master, but the data did not arrive until the master.
Mark paul
11-19-2009, 07:46 PM
I guess maybe you sending in string mode.
ArthurBR
11-20-2009, 09:52 AM
I think I got confused, the data are not arriving in slaves.
Slave to the master arrive ok
ArthurBR
11-23-2009, 12:55 PM
QUOTE=Mark paul;2593]I guess maybe you sending in string mode.[/QUOTE]
I tried to send the string mode, so it did not work
The first three bytes of data from slave to the master indicating the mac address of the slave.
Just need to know the correct format of the string to send the master to the slave indicating the mac adresss slave.
kbanks
11-23-2009, 01:05 PM
You use the "\x" prefix to specify hexadecimal values in Python.
For example, if the sticker on your node ends in 12.34.56 (and/or Portal is showing you that it's SNAP Address is 12.34.56) then you will want a string like:
addr = "\x12\x34\x56"
Although this may look like a 12 character string, it is actually a 3 character string.
Alternativly, you can use the chr() function to create these sorts of strings on the fly.
addr = chr(0x12) + chr(0x34) + chr(0x56).
This can be handy when you know the numeric values you want.
vivekgang
08-18-2010, 09:43 AM
buff=AA0BC0
t1="\xbuff[0:2]"
t2="\xbuff[2:4]"
t3="\xbuff[4:6]"
Dest1=t1+t2+t3
B1= chr(0xAA)
B2=chr(0x0B)
B3=chr(0xC0)
Dest2=B1+B2+B3
Is Dest1 OR Dest2 Correct to pass in RPC Function.
kbanks
08-18-2010, 05:18 PM
Dest2 is correct in your example.
The "address" string has to be exactly 3 binary bytes long.
In your example, Dest1 is 6 characters, not 3.
vBulletin® v3.8.0, Copyright ©2000-2012, Jelsoft Enterprises Ltd.