Question

I am trying to control servos from C++ code on an Odroid-U2. I have the SSC-32 servo driver from LynxMotion attached to the Odroid using a Usb-Serial cord.

Simple serial commands control the servos, such as "#n Puuu" followed by carriage return, where n=pin number for the servo, and uuu=pulse width. I am able to control the servos fine when using PUTTY from the Odroid, but I have been having issues when using C++ code to control the servos. For example, in my code I do the following:

fstream f;
f.open("/dev/ttyUSB0");
f << "#0 P600\r";
f.close();

At one time, I was able to run this code and it wiggled the servo appropriately, but now the terminal gets stuck and hangs still when I try to run this code. I narrowed the problem down to the f.open("/dev/ttyUSB0") line, which prevents the code from running. I'm assuming I need to configure the USB port in some way. I know I need a baudrate of 115200, and have set it accordingly, but I'm kind of a Ubuntu n00b, so I'm not sure what other types of configurations I need.

My version of Linux is Linaro 12.11.

When I type stty, the non-default configurations set seem to be:

speed 115200 baud; line = 0;
-brkint ixoff -imaxbel
-iexten
Was it helpful?

Solution

You cannot rely on the default settings of your serial port (and you should not try to use stty to set them up outside of your program). You have to write code into your program that sets your serial port up properly. For this, you have to use functions like tcsetattr/tcgetattr, cfsetspeed, etc., see man termios.

Also, fstream might not provide access to the proper open() function parameters to use, as opening a serial port on UNIX should be done with O_NOCTTY flag.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top