H. serial example code, H.1. cross-platform c, H. serial example code 5.h.1. cross-platform c – Pololu Maestro User Manual

Page 47

Advertising
background image

detected the baud rate, Pololu devices that expect a leading command byte of 0x80 will ignore command packets that
start with 0xAA, and the Maestro will ignore command packets that start with 0x80.

5.h. Serial Example Code
5.h.1. Cross-platform C

The example C code below works on Windows, Linux, and Mac OS X 10.7 or later. It demonstrates how to set the
target of a Maestro channel by sending a Set Target command to its Command Port, and how to read the position of
a channel by sending the Get Position command. The Maestro’s serial mode needs to be set to “USB Dual Port” for
this code to work. You will also need to modify the line that specifies the name of the COM port device.

This code will work in Windows if compiled with MinGW, but it does not work with the Microsoft C
compiler. For Windows-specific example code that works with either compiler, see

Section 5.h.2

.

// Uses POSIX functions to send and receive data from a Maestro.

// NOTE: The Maestro's serial mode must be set to "USB Dual Port".

// NOTE: You must change the 'const char * device' line below.

#include <fcntl.h>

#include <stdio.h>

#include <unistd.h>

#ifdef _WIN32

#define O_NOCTTY 0

#else

#include <termios.h>

#endif

// Gets the position of a Maestro channel.

// See the "Serial Servo Commands" section of the user's guide.

int maestroGetPosition(int fd, unsigned char channel)

{

unsigned char command[] = {0x90, channel};

if(write(fd, command, sizeof(command)) == -1)

{

perror("error writing");

return -1;

}

unsigned char response[2];

if(read(fd,response,2) != 2)

{

perror("error reading");

return -1;

}

return response[0] + 256*response[1];

}

// Sets the target of a Maestro channel.

// See the "Serial Servo Commands" section of the user's guide.

// The units of 'target' are quarter-microseconds.

int maestroSetTarget(int fd, unsigned char channel, unsigned short target)

{

unsigned char command[] = {0x84, channel, target & 0x7F, target >> 7 & 0x7F};

if (write(fd, command, sizeof(command)) == -1)

{

perror("error writing");

return -1;

}

return 0;

}

int main()

{

// Open the Maestro's virtual COM port.

const char * device = "\\\\.\\USBSER000"; // Windows, "\\\\.\\COM6" also works

//const char * device = "/dev/ttyACM0"; // Linux

Pololu Maestro Servo Controller User's Guide

© 2001–2014 Pololu Corporation

5. Serial Interface

Page 47 of 73

Advertising