View Full Version : Problem with sendData()
sreeram
06-10-2009, 02:40 PM
Hello
Everyone
I am trying to send the data from portal node ("\x00\x00\x01") to bridge node. I am using RS-232 serial port. After reading some documentation i have written a code and when i load this code, the nodes are loosing communication with portal. I want to display the received data on 7segment led display on bridge. Finally i should display (65, 66, 67) if my data is ("ABC")
""" code for portal node ("\x00\x00\x01"). Call this function from bridge node. I doubt this portal code """
def func1():
sendData("\x00\x18\x12", "ABC")
""" code for bridge node. Node address for bridge is "\x00\x18\x12" """
portal_address = "\x00\x00\x01"
from synapse.switchboard import*
from synapse.evalBase import*
global data_buff
def startupEvent():
initUart(1,9600)
flowControl(1,False)
crossConnect(DS_UART1, DS_PACKET_SERIAL) # without disturbing packet serial protocol, portal communicates with bridge thorugh UART1 not UART0.
crossConnect(DS_UART1, DS_STDIO) # output the received data to the terminal.
rpc(portal_address, 'func1') # get the data from portal through rpc call.
def stdinEvent(data_buff): # invokes when the data is received at UART1.
print len(data_buff) # process the data
print data_buff
idx = 0
while idx < len(data_buff):
data = ord( data_buff[idx] )
print data
display2digits(data) # display each character of data on 7segment display. You should get 65, 66, 67.
idx += 1
snappyGen.setHook(SnapConstants.HOOK_STARTUP, startupEvent) # hook startupEvent
snappyGen.setHook(SnapConstants.HOOK_STDIN, stdinEvent) # hook the stdinEvent
Qurey :
I have correctly initialized the UART. I am not getting the data in to my 7segment Led display data should be (65, 67, 68). I doubt sendData function can be only used in transparent mode. But in document it was written in a confusing manner that "it mimics transparent mode". How do i send my data from portal to brige through UART. can i have a sample code snnipet for that.
Kindly help me.
kbanks
06-10-2009, 03:35 PM
when i load this code, the nodes are loosing communication with portal.
This is because you are telling the node (via your script) to use the serial port for STDIN instead of for talking to Portal. Let's take it line by line.
initUart(1,9600)
You have just set the serial port to the wrong baud rate (Packet Serial uses 38.4 kbps.
flowControl(1,False)
This line is harmless, Packet Serial does not use flow control either.
crossConnect(DS_UART1, DS_PACKET_SERIAL) # without disturbing packet serial protocol, portal communicates with bridge thorugh UART1 not UART0.
Here you are telling the node to talk packet serial on UART1, but you have already messed up the baud rate.
crossConnect(DS_UART1, DS_STDIO) # output the received data to the terminal.
Here you undo what you just did in the previous line. As explained in the docs, crossConnect() is "exclusive". UART1 cannot be used for DS_PACKET_SERIAL and DS_STDIO at the same time.
Did you mean to use Packet Serial one one UART, and STDIN on the other UART?
(more in a bit, I am playing around with your script)
kbanks
06-10-2009, 04:35 PM
Here is a modified version of your code that will do what you want.
Be aware that the 65, 66, 67 will appear on the display in rapid succession.
It might help to thank of TRANSPARENT MODE as being sort of like a "remote UART".
If you crossConnect DS_UART0 and DS_UART1, characters will go "in one UART and out the other". In other words, "characters would go between the UARTS".
If instead, you crossConnect DS_UART0 to DS_TRANSPARENT_MODE, characters will go between the UART and "somewhere else".
Exactly where that "somewhere else" actually is will depend on the mcastSerial() or ucastSerial() functions. These functions let you control what remote node (or nodes) the serial data gets sent to.
Do a search on the forum and in the docs for dataMode.py (and variants).
Along the same lines, if you crossConnect DS_UART0 (or 1) and DS_STDIO, characters go between the local UART and your SNAPpy script.
In the following example, you will see how to do what you wanted... You want to be able to accept "remote data", and process it in your SNAPpy script.
To do this, you need to crossConnect DS_TRANSPARENT_MODE and DS_STDIO.
Notice that I do not reprogram the UART, or interfere with the Packet Serial settings. I just tell the node that TRANSPARENT MODE data should be sent to the HOOK_STDIN handler.
portal_address = "\x00\x00\x01"
from synapse.switchboard import *
from synapse.evalBase import *
global data_buff
def startupEvent():
stdinMode(0, False) # having to use Line Mode (0) due to a 2.1 bug
crossConnect(DS_TRANSPARENT, DS_STDIO)
rpc(portal_address, 'func1') # get the data from portal through rpc call.
def stdinEvent(data_buff): # invokes when the data is received at UART1.
# process the data
print len(data_buff)
print data_buff
idx = 0
while idx < len(data_buff):
# ignore optional trailing \n
if data_buff[idx] != '\n':
data = ord( data_buff[idx] )
print data
display2digits(data) # display each character of data on 7segment display. You should get 65, 66, 67.
idx += 1
snappyGen.setHook(SnapConstants.HOOK_STARTUP, startupEvent) # hook startupEvent
snappyGen.setHook(SnapConstants.HOOK_STDIN, stdinEvent) # hook the stdinEvent
Here is the compatible Portal code.
def func1():
sendData("your-node-address-goes-here", "ABC\n")
You probably noticed that where you were sending "ABC" I had to send "ABC\n".
This is due to a bug in SNAP that we were not aware of until we tried to make your script work.
It should be possible to use character mode for the STDIN settings (refer to the docs on stdinMode) in combination with the crossConnect(DS_TRANSPARENT, DS_STDIO) trick.
However, what we found today was that EXTRA garbage characters come in AFTER the "real" data. For example, "ABC!@#$%#$^" when it should have only been "ABC".
The workaround is to use the line mode for STDIN instead of character mode. This eliminates the garbage characters, but requires a trailing CR or LF ( a '\r' or '\n') at the end of the string.
We will fix this bug in STDIO before releasing version 2.2, but for now use the work-around implemented in the above scripts (send a \n after your data), and strip it off at the receiving node.
kbanks
06-10-2009, 04:54 PM
In the previous post I had extra code in the node script to ignore the trailing line feeds. It turns out this is not necessary.
In line mode, if you type "ABC" and then a Carriage Return or Line Feed, only the "ABC" gets sent to SNAPpy.
A shorter version of the stdinEvent posted previously is shown below.
def stdinEvent(data_buff): # invokes when the data is received at UART1.
# process the data
print len(data_buff)
print data_buff
idx = 0
while idx < len(data_buff):
data = ord( data_buff[idx] )
print data
display2digits(data) # display each character of data on 7segment display. You should get 65, 66, 67.
idx += 1
sreeram
06-11-2009, 07:54 AM
Hello
Everyone
I am trying to send the data from portal node ("\x00\x00\x01") to bridge node. I am using RS-232 serial port. After reading some documentation i have written a code and when i load this code, the nodes are loosing communication with portal. I want to display the received data on 7segment led display on bridge. Finally i should display (65, 66, 67) if my data is ("ABC")
""" code for portal node ("\x00\x00\x01"). Call this function from bridge node. I doubt this portal code """
def func1():
sendData("\x00\x18\x12", "ABC")
""" code for bridge node. Node address for bridge is "\x00\x18\x12" """
portal_address = "\x00\x00\x01"
from synapse.switchboard import*
from synapse.evalBase import*
global data_buff
def startupEvent():
initUart(1,9600)
flowControl(1,False)
crossConnect(DS_UART1, DS_PACKET_SERIAL) # without disturbing packet serial protocol, portal communicates with bridge thorugh UART1 not UART0.
crossConnect(DS_UART1, DS_STDIO) # output the received data to the terminal.
rpc(portal_address, 'func1') # get the data from portal through rpc call.
def stdinEvent(data_buff): # invokes when the data is received at UART1.
print len(data_buff) # process the data
print data_buff
idx = 0
while idx < len(data_buff):
data = ord( data_buff[idx] )
print data
display2digits(data) # display each character of data on 7segment display. You should get 65, 66, 67.
idx += 1
snappyGen.setHook(SnapConstants.HOOK_STARTUP, startupEvent) # hook startupEvent
snappyGen.setHook(SnapConstants.HOOK_STDIN, stdinEvent) # hook the stdinEvent
Qurey :
I have correctly initialized the UART. I am not getting the data in to my 7segment Led display data should be (65, 67, 68). I doubt sendData function can be only used in transparent mode. But in document it was written in a confusing manner that "it mimics transparent mode". How do i send my data from portal to brige through UART. can i have a sample code snnipet for that.
Kindly help me.
small correction here. I had referred to commandline.py and i set the baud rate correctly to 38,400. I forget to include that in this script.
Here one more thing is that.. i tried to experiment with two crossConnects and i came to know that they don't work. One more thing is that i forget to use DS_TRANSPARENT here because i know that sendData function sends the data through transparent mode only.
kbanks
06-11-2009, 08:32 AM
I want to make sure everyone understands that Portal (and other SNAPconnect clients) can send data to another node without having to use TRANSPARENT MODE.
The data can also be passed as a parameter of an RPC call.
For example, with your script, Portal could just call stdinEvent("ABC") directly.
Other nodes can also use RPC, it is not just for Portal.
sreeram
06-11-2009, 09:09 AM
more qureies
1) I think in my above script, we don't need macast or ucastSerial since we explicitly specified in our portal script where the data goes. And also in this case my data is coming to my snappy script through stdin which is hooked up to STDIO. I think here i am not receiving the data on GPIO 7 (rx) and GPIO 8 (Tx). If this is the case then do i have to write the data manually through series of writePin commands ? If i want to receive the characters thorugh this pin GPIO 7(rx), then in which way can i use the crossConnect.
crossConnect (DS_TRANSPARENT, DS_STDIO) # driect the recived data to the terminal
I think instead of the above command i have to use the below one if want to redirect my output to the GPIO7 (rx).
crossConnect(DS_TRANSPARENT, DS_UART1) # direct the received data to the UART1 GPIO7
2) suppose if i am not having the sendData() command, then how do i send the data from portal to bridge without using transparent mode ? i don't want to use rpc either. help me with the portal script.
in bridge_node script
def startupEvent():
init(UART1, 38,400)
flowControl(1,False)
""" i doubt this crossConnect because when i connect my DS_UART1 to DS_PACKET_SERIAL. My communication with portal will be lost because my portal will be connected to UART0 and DS_PACKET_SERIAL """
crossConnect(DS_UART1, DS_PACKET_SERIAL)
rpc(portal_node, 'func1') # go to portal script and perform func1
in portal script
def func1():
data = "ABC\n"
# send the data without using sendData function. I want to send whole data packet
kbanks
06-11-2009, 10:54 AM
1) I think in my above script, we don't need macast or ucastSerial since we explicitly specified in our portal script where the data goes.
The Portal script says where to send data FROM the Portal script, but you still need either a mcastSerial() or a ucastSerial() to say where data FROM the node should go.
And also in this case my data is coming to my snappy script through stdin which is hooked up to STDIO.
STDIN is not "hooked" to STDIO, it is part of STIO. STDIO is just shorthand for STDIN+STDOUT.
I think here i am not receiving the data on GPIO 7 (rx) and GPIO 8 (Tx).
I don't understand the question.
If this is the case then do i have to write the data manually through series of writePin commands ?
No, if you want to use the pins as a UART, use the UART functions (like initUart()).
If i want to receive the characters thorugh this pin GPIO 7(rx), then in which way can i use the crossConnect.
If you want your script to process characters received from the UART, then connect it to the UART.
crossConnect(DS_UARTx, DS_STDIO)
crossConnect(DS_TRANSPARENT, DS_STDIO)
This says you want received TRANSPARENT DATA (for example, from a Portal sendData() call) to be sent to your script (as a HOOK_STDIN event). It also says you want script output (for example, from print statements) to be sent OUT as TRANSPARENT DATA.
crossConnect(DS_TRANSPARENT, DS_UART1) # direct the received data to the UART1 GPIO7
Yes
2) suppose if i am not having the sendData() command, then how do i send the data from portal to bridge without using transparent mode ? i don't want to use rpc either.
Those are your only two choices, unless you want to learn how to use Python's pyserial library.
help me with the portal script.
Sorry, once you decide NOT to use the methods we already provide (like sendData() or rpc()), and instead create your own methods of doing things, you are either writing your code yourself, or contracting our Custom Solutions group to help you.
Why don't you want to use sendData() or rpc()?
sreeram
06-11-2009, 11:17 AM
Those are your only two choices, unless you want to learn how to use Python's pyserial library.
Sorry, once you decide NOT to use the methods we already provide (like sendData() or rpc()), and instead create your own methods of doing things, you are either writing your code yourself, or contracting our Custom Solutions group to help you.
Why don't you want to use sendData() or rpc()?
I am sorry to ask this question. Thanks a ton :) !!!
vBulletin® v3.8.0, Copyright ©2000-2012, Jelsoft Enterprises Ltd.