more G-Labs products

Author Topic: Python and Serial interface  (Read 3762 times)

September 23, 2014, 07:45:52 PM
Read 3762 times

haitch

  • Guest
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.

Code: [Select]
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)
« Last Edit: September 26, 2014, 05:38:45 PM by haitch »

September 26, 2014, 05:48:26 PM
Reply #1

haitch

  • Guest
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.

Code: [Select]
25;0;0;0;7;1.4
25;0;0;0;6;1.4
25;0;0;0;6;1.4
25;

September 27, 2014, 01:20:01 AM
Reply #2

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
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

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.

September 27, 2014, 01:29:26 AM
Reply #3

haitch

  • Guest
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?

September 27, 2014, 09:53:13 AM
Reply #4

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
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.