jahnavi
Posts: 26
Joined: Tue Jan 08, 2013 1:46 am

How to access data from two serial ports simulataneously

Tue Jan 08, 2013 2:02 am

Hi

Can you please send a sample python script to access the data from two serial ports namely ttyACM0 and ttyACM1 simultaneously.

Thank you

trouch
Posts: 310
Joined: Fri Aug 03, 2012 7:24 pm
Location: France

Re: How to access data from two serial ports simulataneously

Tue Jan 08, 2013 9:25 am

you basically have to instantiate two Serial object, each pointing to a different /dev/tty
the code then depend what you intend to do...
a serial-to-serial bridge or use two serial devices ?

Code: Select all

import serial
s0 = serial.Serial('/dev/ttyACM0', 9600)
s1 = serial.Serial('/dev/ttyACM1', 9600)

while True:
    c0 = s0.read()
    s0.write(c0)

    c1 = s1.read()
    s1.write(c1)
this will loop echoing back reveived data on both UART.

WebIOPi - Raspberry Pi REST Framework to control your Pi from the web
http://store.raspberrypi.com/projects/webiopi
http://code.google.com/p/webiopi/
http://trouch.com

jahnavi
Posts: 26
Joined: Tue Jan 08, 2013 1:46 am

Re: How to access data from two serial ports simulataneously

Wed Jan 09, 2013 4:07 am

Hi,

I am trying to take reading from two arduinos which are connected to the pi via USB hub.

Code: Select all

#!/usr/bin/python
import sys
import MySQLdb
 # connect to the MySQL server
import serial
from time import strftime
from datetime import datetime, time

ser = serial.Serial('/dev/ttyACM0',9600)

startTime = datetime.now()
try:
        conn = MySQLdb.connect (host = "localhost",
                             user = "root",
                             passwd = "qwerty",
                             db = "acc")

        while 1:
                line=ser.readline().rstrip()
                l=line.split()
                l,m,n=line.split(",")
                now = datetime.now()
                print("%s,%s,%s,%s"%(now.strftime("%Y-%m-%d %H:%M:%S"),l,m,n))
                cur = conn.cursor()
                l=12.0
                m=9.3
                n=0.7
        # Use all the SQL you like
                cur.execute("INSERT INTO senso1 (timestamp, x, y, z) VALUES(%s,%s,%s,%s)", (now,l,m,n))
                conn.commit ()


                print "Number of rows inserted: %d" % cur.rowcount

except MySQLdb.Error, e:
     print "Error %d: %s" % (e.args[0], e.args[1])
     sys.exit (1)
The above code is use to take reading from one sensor but i also want to get readings from ttyACM1 the other sensor which is connected to the pi. So,can you plese let me know how to modify the code so that i can access data from both ttyACM0 and ttyACM1 simultaneously.

Thank you

trouch
Posts: 310
Joined: Fri Aug 03, 2012 7:24 pm
Location: France

Re: How to access data from two serial ports simulataneously

Wed Jan 09, 2013 8:24 am

with the code snippet I gave, you can quickly adapt your code to use 2 serial line...

Code: Select all

import...

ser0 = serial.Serial('/dev/ttyACM0',9600)
ser1 = serial.Serial('/dev/ttyACM1',9600)

...
                line=ser0.readline().rstrip()
... insert in mysql
                line=ser1.readline().rstrip()
... insert in mysql
I suggest adding a new function so you will not have to copy past your line formating and sql insertion

WebIOPi - Raspberry Pi REST Framework to control your Pi from the web
http://store.raspberrypi.com/projects/webiopi
http://code.google.com/p/webiopi/
http://trouch.com

chris217
Posts: 10
Joined: Sat Aug 25, 2012 10:24 am
Location: Hampshire, UK

Re: How to access data from two serial ports simulataneously

Fri Jan 11, 2013 9:28 pm

One approach would be to use the Twisted framework.

http://twistedmatrix.com/trac/

This is designed for event driven applications. Be warned, however, that it has a very steep learning curve.

Return to “Python”