Hi johnf,
Thank you for the reply.
I tried a delay of read() for up to 10 seconds but no luck — same error message.
I tried unblocking mode and the program waits for reply forever, i.e. still no luck.
In both occasions I tried signed and unsigned char.
Here is my code:
/******************************************/
#include /* Standard input/output definitions /
#include / String function definitions /
#include / UNIX standard function definitions /
#include / File control definitions /
#include / Error number definitions /
#include / POSIX terminal control definitions */
int fd;
int readport(int fd, char *result) {
int iIn = read(fd, result, 254);
result[iIn-1] = 0x00;
if (iIn < 0) {
if (errno == EAGAIN) {
printf("SERIAL EAGAIN ERROR
");
return 0;
} else {
printf("SERIAL read error %d %s
", errno, strerror(errno));
return 0;
}
}
return 1;
}
int initport(int fd) {
struct termios options;
tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &options);
return 1;
}
int main(int argc, char **argv) {
fd = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open_port: Unable to open /dev/ttyS2 - ");
return 1;
} else {
fcntl(fd, F_SETFL, 0);
}
initport(fd);
// static char API1[] = {0x7E00040852444C15};
static unsigned char API1[] = {0x7E, 0x00, 0x04, 0x08, 0x52, 0x44, 0x4C, 0x15};
printf("written:%x
", API1);
int wi = write(fd, &API1, 8);
usleep(2000000);
fcntl(fd, F_SETFL, FNDELAY); // don’t block serial read
char sResult[254];
if (!readport(fd,sResult)) {
printf("read failed
");
close(fd);
return 1;
}
printf("readport=%s
", sResult);
close(fd);
return 0;
}
/*******************************************/
DoI need to change the format of the byte expression so that the XBee module could understand and response? I tried ‘\x7E’, ‘0x7E’ but no luck so far.
Thanks.
Andrew