Hi David!
I read the topic you mentioned and now the serial read() is working in blocking mode :), but additionally I had to set another option to get it work in really blocking mode: options.c_cc[VMIN] = 2;
which blocks the read function until 2 characters arrives.
Now I am happy with this, but I still can reproduce the original problem, so may not be the blocking was the problem:
if I set a channel to 1 <= x <= 65535 with SET TARGET, the GET POSITION works well, but if I set to 0 or 65536 the Maestro returns 0x00 0x00. I am getting this result until I set a valid value for example 6000.
Is this normal operation?
Here is a sample code:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <termios.h>
int init(char *portname)
{
int port;
struct termios options;
port = open(portname,O_RDWR|O_NOCTTY);
if (port == -1)
{
printf("Unable to open port %s\n",portname);
return -1;
}
printf("Port opened: %s, handler: %d\n", portname, port);
tcgetattr(port, &options);
options.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
options.c_cc[VMIN] = 2;
tcsetattr(port, TCSANOW, &options);
return port;
}
int get_error(int port)
{
char command;
char response[2] = "";
unsigned int retval = 0;
int iores = 0;
command = 0xA1;
iores = write(port, &command, sizeof(command));
iores = read(port, response, 2);
printf("read iores: %d\n", iores);
if (iores != 2)
printf("error reading: %d bytes read", iores);
printf("Error: 0x%02x 0x%02x\n",response[0],response[1]);
retval = *((int *)(response));
return retval;
}
int set_pos(int port, unsigned short servo, unsigned short pos)
{
char command[4];
int i, iores;
pos *= 4;
command[0] = 0x84; //command type
command[1] = servo; //servo index
command[2] = pos & 0x7F; //pos low bits
command[3] = (pos >> 7) & 0x7F; //pos high bits
iores = write(port, &command, sizeof(command));
if(iores > 0)
{
printf("Position set to %d\n", pos);
return pos;
} else
{
printf("I/O Error!\n");
return 0;
}
}
int get_pos(int port, int servo)
{
char command[2];
char response[2] = "";
int iores = 0;
unsigned int retval = 0;
command[0] = 0x90; //get position command
command[1] = servo; //servo index
iores = write(port, &command, sizeof(command));
iores = read(port, response, 2);
printf("read iores: %d\n", iores);
if (iores != 2)
printf("error reading: %d bytes read", iores);
printf("Servo %d position: 0x%02x 0x%02x\n", servo,response[0],response[1]);
retval = *((int*)(response));
return retval;
}
int main(int arg, char** argv)
{
char *portname = "/dev/ttyACM0";
int port, i;
int servo = atoi(argv[1]);
port = init(portname);
printf("Get Position: %d\n",get_pos(port, servo));
if(argv[2])
printf("%d\n", set_pos(port, servo, atoi(argv[2])));
printf("Get Position: %d\n",get_pos(port, servo));
printf("Error: %d\n", get_error(port));
close(port);
}
biga.