pythonubuntuarduinoserial-portioerror

IO Error [Errno 5] with long term Serial Connection in Python


I am running a fairly simple python code on Ubuntu that is to communicate with an Arduino. The code works as expected for about 15 minutes and then throws an I/O error [Errno 5]. I have searched through many threads about this error message but I can't seem to find a solution that fits my needs.

The python code sends commands to the Arduino through serial port and the arduino does whatever that command is. When this error is thrown, the Arduino is forced off of its original port number. It goes from '/dev/ttyACM1' to '/dev/ttyACM2'

At first I thought it had to do with the Arduino's autoreset function, which I bypassed with a 120 ohm resistor between the Reset and +5V pins. This did not solve the problem.

Next, I thought it was because the Ubuntu machine suspends itself due to inactivity, so I disabled that power saving feature, but no improvement.

Also, I leave the terminal session open where the python code is ran originally.

I'm sure the problem has to do with the serial port number issue since the error is triggered by a failed ser.write command

The subroutine of the python code which generates this error is as follows:

def sc_loop(ptime,maxiter, file):

iter = 1              ## Initialize cycle count
totalvol = 0
cyclevol = 0
while iter < maxiter:
    ts = time.time()
    st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    print >>file, st, " - pump cycle #", iter
    ## Tell the arduino to begin pumping
    ser.write("1".encode())
    pump = ser.readline()
    if pump == 'P':
        ts = time.time()
        st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
        print >>file, st, " - Sample chamber pump turned ON"
    ts = time.time()
    st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
    print >>file, st, " - Sample chamber pump turned ON"
    time.sleep(ptime)
    ser.write("2".encode())
    ts2 = time.time()
    st2 = datetime.datetime.fromtimestamp(ts2).strftime('%Y-%m-%d %H:%M:%S')
    print >>file, st2, " - Sample Chamber Pump turned OFF"
    print >>file, st2, " - Pump was on for",ts2-ts, "seconds"
    cyclevol = 0.0667*(ts2-ts)
    print >>file, st2, " - ", cyclevol," mL of sample were pumped this cycle"
    totalvol = totalvol+cyclevol
    print >>file, st2, " - ", totalvol, " mL of sample have been pumped so far"
    time.sleep(3)
    runcamera(file) ## Run camera
    ## imaged = raw_input("Sample is ready for imaging [Press any key once imaged] ")
    iter = iter+1
return

This code works flawlessly, albeit temporarily. My question is, what causes this error after 10-15 minutes of the python code running?

Thanks in advance!


Solution

  • Take a look at:

    http://ubuntuforums.org/showthread.php?t=2057988

    The issue is due to udev giving dynamic names to devices. After a while, the arduino is re-registered by the machine and is assigned a new port number. It is assigned a new port number because the driver burps while it 'removes' and 'adds' the arduino almost simultaneously.

    Creating a new rule for udev to assign the device a static port will help.