Communicating over serial to a stepper motor

asked 2016-07-12 02:21:16 -0500

Kjemu gravatar image


I want to make a simple node to subscribe to a topic and publish/write these to a serial port. I have managed to send what i want and get the response i need but i can't implement this in a ROS node script. All the rosserial guides i have read is about arduino. However i only want to read and write to this port. Not edit whats on the other side of the ports software.

So basically i have a python script that send commands which gives me the wanted response. (Positioning of a stepper motor of type Nanotec PD2-n) But how can i implement this into a node?

Appreciate help in python and ROS (new to both)

import serial, fcntl, struc, time

#establish connection with serial port

ser = serial.Serial(

# commands as following: "absolute mode" , "move to position 'angle' " , "execute"

commands = ['#1p2\r',
'#1s' + str(angle) + '\r',

if ser.isOpen():
    for data in commands
        response = ser.readline()
        print("read data: " + response)
    print "cannot open serial port"
edit retag flag offensive close merge delete


Not an answer, but some clarification: rosserial is not meant as a generic interface to a (set of) serial port(s), but rather as a convenience library that provides a minimal implementation of the ROS C++ client library that works on Arduinos (and other embedded systems).

gvdhoorn gravatar image gvdhoorn  ( 2016-07-12 02:47:24 -0500 )edit

It's called rosserial because most of the times, such systems will be connected to a more powerful 'host' via some sort of serial interface (RS232 or similar). To allow communication, it comes with a comms protocol that supports concepts similar to TCPROS, allowing pub/sub etc from an Arduino.

gvdhoorn gravatar image gvdhoorn  ( 2016-07-12 02:49:27 -0500 )edit

Yes, so rosserial will not be used in my case. Thanks for the clarification. I found this example: serial-example which i assume is something i want but in python.

Kjemu gravatar image Kjemu  ( 2016-07-12 03:24:17 -0500 )edit

You want a python serial port library? something like ?

If your code is already running the serial part you are missing the ROS communication part. Have you seen the ROS Python tutorials? ?

fsteinhardt gravatar image fsteinhardt  ( 2016-07-12 09:25:22 -0500 )edit