I have a Arduino Mega 2560 sending data out, my environment is Ubuntu 14.04, it's a python ROS node.

Problem is that it works well when I use Serial Monitor or Plotter in Arduino IDE to see the data coming, but when I run my python scripts, it went wrong, this is my python scripts:

import serial
import roslib; roslib.load_manifest('robot_skin')
import rospy
from robot_skin.msg import FloatArray

def setup_serial(dev_name, baudrate):
    serial_dev = serial.Serial(dev_name, timeout=1)
    if(serial_dev == None):
        raise RuntimeError("robotis_servo: Serial port not found!\n")


    return serial_dev

except (serial.serialutil.SerialException), e:
    raise RuntimeError("robotis_servo: Serial port not found!\n")

def get_adc_data(serial_dev, num_adc_inputs):
    ln = serial_dev.readline()
    l = map(int, ln.split(','))
    if len(l) != num_adc_inputs:
        rospy.logwarn('Something fishy with the serial port.')
        l = get_adc_data(serial_dev, num_adc_inputs)
    return l

if __name__ == '__main__':

dev_name = '/dev/ttyACM0'

baudrate = 115200

serial_dev = setup_serial(dev_name, baudrate)

pub = rospy.Publisher('/arduino/ADC', FloatArray)

for i in range(10):
    ln = serial_dev.readline()

rospy.loginfo('Started publishing ADC data')

while not rospy.is_shutdown():
    fa = FloatArray()
    fa.header.stamp = rospy.Time.now()
    fa.data = get_adc_data(serial_dev, 6)


when I run this, it goes to serial_dev == NONE, have a error.

I have googled a long time to so solve this, I made two changes but this still not working:

  1. I used and then Serial Monitor and Plotter in my Arduino IDE works after this: sudo chmod a+rw /dev/ttyACM0

  2. I added myself to the dialout group usesudo adduser $USER dialout

But still have the same error......

Thank you.

  • 2
    Did you log out/in after the sudo adduser $USER dialout? – waltinator Mar 30 '18 at 14:01
  • The sudo adduser $USER dialout is enough , you don't need to run chmod. As waltinator said log out/in after run adduser. – Ali Razmdideh Mar 30 '18 at 14:16
  • Thanks, Yes I did, I logged out once when I added to dialout, and today I rebooted my computer once. I just logged out and logged in again, I didn't run chmod, my Arduino IDE Serial Monitor and Plotter works well, but when I run my python code, same problem..... – Shawn Mar 30 '18 at 15:23
  • it's interesting that my arduino board blinks everytime I hit enter to run my python code, just like it would also blink if I run the Serial Monitor and Plotter in Arduino IDE – Shawn Mar 30 '18 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.