View Full Version : stdin event hook
mwhitley
07-01-2008, 08:19 PM
I have a script with a function that is reading in data from the uart in character mode. It seems like the number of bytes I get each time the hook is called varies. I have a command word of a certain length that I'm looking for. If I was in C# I would do something like:
while(serialport.bytestoread < 4 );
mydata = serialport.read() //Don't remember the exact syntax, but you know what I meanCan you help me understand a little more about this hook? I guess a few things I'm wondering about are:
If the hook is called and I'm doing something with the buffer while new data comes it, is the existing data overwritten?
If the VM waits until I exit the hook before it's called again with new data, how big is the input buffer? I believe it's 40 characters - what happens if this number is exceeded?
Can I stream in data at 57600 baud and not be in danger of missing bytes? (I guess what I'm really looking for here is a time for each instruction in the python VM).
mgenti
07-02-2008, 06:16 AM
If the hook is called and I'm doing something with the buffer while new data comes it, is the existing data overwritten?
The hook is not an interrupt in the sense that you can be interrupted again while you are processing the data and it will not be overwritten.
If the VM waits until I exit the hook before it's called again with new data, how big is the input buffer? I believe it's 40 characters - what happens if this number is exceeded?
I don't have those numbers in front of me right now, I'll have to post them later.
(I guess what I'm really looking for here is a time for each instruction in the python VM).
This thread (http://forums.synapse-wireless.com/showthread.php?t=4&) may answer your question as to the speed of the virtual machine. These measurements were taken while SNAP 2.0 was in beta so they should be taken as an approximation.
kbanks
07-02-2008, 08:55 AM
I have a script with a function that is reading in data from the uart in character mode. It seems like the number of bytes I get each time the hook is called varies.
Yes, the number of characters varies, because it is driven by the number of characters actually received by that point in time.
So, the length of the string passed to you in the STDIN hook will vary.
I have a command word of a certain length that I'm looking for. What should I do? (original question paraphrased, see previous post)
1) If your data happens to be delimited by a carriage return or a line feed, consider using LINE MODE instead of CHARACTER MODE. The core code will buffer up the characters until it sees a CR or LF, and give you a nice clump of characters all at once.
Be aware that the maximum string that can be buffered like this is 40 characters. If more than 40 characters are received, an error emssage will be printed, and data will be lost.
Also be aware that if your data is delimited by CR and LF, you might get two strings (one of length 0).
2) If your character stream has a different delimiter (or no delimiter), you can do your own buffering until you have enough characters.
global strBuf
def init():
global strBuf
strBuf = ""
def stdinEvent(incomingChars):
strBuf += incomingChars
if len(strBuf) == 4: # finally got enough characters
processCmd(strBuf)
strBuf = ""
Here you are limited by the 64 byte restriction for concatenated strings (of which you can only have one at a time).
If the hook is called and I'm doing something with the buffer while new data comes it, is the existing data overwritten?
SNAPpy hooks are not re-entrant/recursive. While you are in a HOOK_STDIN handler, no other hooks will be invoked (including a second HOOK_STDIN).
If you take too long to process your incoming characters, data will be dropped, but this will happen over in the UART receive code.
If the VM waits until I exit the hook before it's called again with new data, how big is the input buffer? I believe it's 40 characters - what happens if this number is exceeded?
An extra buffer, used only for LINE INPUT MODE, can hold strings up to 40 characters long.
Incoming serial data is initially buffered in reusable blocks of approx. 100 bytes.
If either buffer gets overrun, data will be dropped.
Can I stream in data at 57600 baud and not be in danger of missing bytes?
No, you can not stream data without missing characters.
You should use the lowest baudrate that your application can stand.
You should also use hardware flow control, if your data stream is ongoing.
More information about SNAPpy flow control can be found in this thread (http://forums.synapse-wireless.com/showthread.php?t=82)
To get the highest throughput possible, consider using TRANSPARENT MODE AKA DATA MODE. This is an option if you don't actually need to process the data at the node, you just want to send it somewhere else.
Also be aware that the performance of DATA MODE will be greatly enhanced in the next release.
mgenti
07-02-2008, 08:59 AM
Also be aware that the performance of DATA MODE will be greatly enhanced in the next release.
Additionally the SNAP Gateway will support data mode as well if you want to do the processing in your own application.
mwhitley
07-02-2008, 10:20 AM
Thanks for all the information, it's very helpful.
Additionally the SNAP Gateway will support data mode as well if you want to do the processing in your own application. How do I do this? I'm familiar with using transparent mode to get data to the UART of another node (aka wireless serial port) using
crossConnect(DS_UART1, DS_TRANSPARENT)
ucastSerial(otherNodeAddr)but how do I get that into gateway?
Or are you talking about issuing an RPC into gateway from the stdin event hook?
mgenti
07-02-2008, 10:23 AM
How do I do this?
You would actually do this the same way you would send it to a SNAP node, instead use the address of the SNAP Gateway.
However, this functionality is only available in the upcoming 2.1 release.
mwhitley
07-02-2008, 11:11 AM
However, this functionality is only available in the upcoming 2.1 release.
any chance I can get an early beta?
vBulletin® v3.7.5, Copyright ©2000-2010, Jelsoft Enterprises Ltd.