You can calculate the time it should take to execute the comamnd, by multiplying the number of steps by the step-time. So for 10,000 steps at 20mSec. per step should take 20 seconds to execute. If you send the first command, wait exactly 20 seconds then send the second command the PIC may miss the start of the second command and therefore ignore it.
The code below is something I wrote to illustrate this.
/* interface to Stepper Motor Controller with serial interface */ #include#include #include #include #include #include #include #include #define STEPPER "/dev/stepper" int fd; /* file descriptor for comm port */ FILE *fp; /* same as a FILE * */ /* format of input command is */ /* A+xxxxx:nnn */ /* A = chip ident */ /* + or - is step direction */ /* xxxxx is 5 digits of step count */ /* : delimits step count from step rate */ /* nnn is step rate in milliseconds */ /* the string is terminated with a newline char */ /* define a set of "difficult" commands */ struct step { char dir; int count; int dly; }; struct step s[] = { '+',10000,2, '-',11000,2, '+',12000,2, '-',13000,2, '+',14000,2, '-',15000,2, '+',16000,2, '-',17000,2, '+',18000,2, '-',19000,2, '+',20000,2, '-',21000,2, '+',22000,2, '-',23000,2, '+',24000,2, '-',25000,2, '+',26000,2, '-',27000,2, '+',28000,2, '-',29000,2, '+',30000,2 }; main() { char a[20]; int i; int j; int d; int w; int n = 1; open_comm(STEPPER); for(i = 0; i < sizeof(s)/sizeof(struct step); i++) { a[0] = 'A'; a[1] = s[i].dir; sprintf(&a[2], "%05d", s[i].count); sprintf(&a[7], ":%03d", s[i].dly); a[11] = '\n'; a[12] = '\0'; printf("%05d ^Gsending: ", i); fflush(stdout); write(fd, a, 12); a[11] = '\0'; printf("%s (%d mSec)\n", a, s[i].count * s[i].dly); usleep((s[i].count * s[i].dly * 1010)); } } int open_comm(char *dev) { struct termios t; unsigned char ret_code; if((fd = open(dev, O_RDWR | O_NONBLOCK)) < 0) { printf("Can't open port %s, %s\n", dev, sys_errlist[errno]); exit(1); } fp = fdopen(fd, "w"); cfsetispeed(&t, B38400); cfsetospeed(&t, B38400); tcgetattr(fd, &t); cfmakeraw(&t); t.c_iflag = (ISTRIP|IXON|IXOFF); tcsetattr(fd, TCSANOW, &t); return; }
The line
usleep((s[i].count * s[i].dly * 1010));
shows the formula I used to calculate the amount of time to wait
between sending command to the PIC. If I had used a value less than
1010 (theoretically it should be 1000) I found that the PIC was
missing some commands.