HomeGenie Forum
Automation Program Plugins and Wizard Scripting => Help => Topic started by: haitch on September 23, 2014, 07:45:52 PM
-
I am looking into using HG to read serial data with python. Is it possible to use the python interpreter with HG? I tried importing logging into my script with the intention of being able to log/print data to a file while building a serial program with python but I can't import the logging module.
This is what I have to start with. I would like to log the console output either to stdout when launching HG from the cli or to the log file /usr/local/bin/homegenie/log/homegenie.log. Is that possible?
edit: standard python print gets logged.
Here is what I have started with.
def ser_rec (mymessage):
# this will be called every time a message is received from serial port
hg.Program.Notify("SerialPort String", mymessage);
print mymessage
return None
#setup program stuff
def setup():
portname = "/dev/ttyUSB0"
ser = hg.SerialPort.WithName( portname ).OnMessageReceived( ser_rec ).Connect()
hg.Program.Notify("Python Test", "Hello Python "+portname)
return None
hg.Program.Setup(setup)
-
Does the serial helper module read and write to the serial port via separate threads or does each operation block until completed? I am reading data from an Arduino and some comes in at sub-second intervals. The data is delimited by "
" which works fine normally but sometimes I will get data that includes one full line with a partial line on the end.
25;0;0;0;7;1.4
25;0;0;0;6;1.4
25;0;0;0;6;1.4
25;
-
Hello,
a serial send will block until the operation is completed.
You can still use
http://www.homegenie.it/docs/doxy/de/d1f/class_home_genie_1_1_automation_1_1_scripting_1_1_program_helper.html#a05df8cf777de6fbeb610e280bd96ebdd (http://www.homegenie.it/docs/doxy/de/d1f/class_home_genie_1_1_automation_1_1_scripting_1_1_program_helper.html#a05df8cf777de6fbeb610e280bd96ebdd)
to run the operation asynchronously.
Receive is asynchronous for the serial port helper itself (it won't block if your program takes along time processing a receive event).
Cheers,
g.
-
Thanks. Any thoughts on why OnStringReceived would return multiple lines? Am I not reading them fast enough which causes the to queue up in the OnStringReceived event?
-
Dunno.... perhaps also sending with pauses will cause a data break down into multiple receive events.
Have you tried seeing behavior when reading your arduino data from another program?
g.