configuration of my serial port is:
timer_t tid = 0;
struct itimerspec it;
fd = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
perror("open_port: Unable to open /dev/ttyS0\n");
exit(1);
}
satimer.sa_handler = signal_handler_TIMER;
satimer.sa_flags = 0;
satimer.sa_restorer = NULL;
sigaction(SIGALRM, &satimer, NULL);
it.it_value.tv_sec = 0;
it.it_value.tv_nsec = 10000000;
it.it_interval.tv_sec = 0;
it.it_interval.tv_nsec = 10000000;
if (timer_create(CLOCK_REALTIME, NULL, &tid) == -1)
fprintf(stderr, "error in timer_create \n");
// printf("timer ID is 0x%lx\n", (long) tid);
if (timer_settime(tid, 0, &it, NULL) == -1)
fprintf(stderr, "error in settime \n");
fcntl(fd, F_SETFL, 0);
fcntl(fd, F_SETOWN, getpid());
fcntl(fd, F_SETFL, O_SYNC); /**<<<<<<------This line made it work.**/
tcgetattr(fd, &termAttr);
//baudRate = B115200; /* Not needed */
cfsetispeed(&termAttr, B9600);
cfsetospeed(&termAttr, B9600);
termAttr.c_cflag |= PARENB;
termAttr.c_cflag &= ~PARODD;
termAttr.c_cflag &= ~CSTOPB;
termAttr.c_cflag &= ~CSIZE;
termAttr.c_cflag |= CS8;
termAttr.c_cflag |= (CLOCAL | CREAD);
termAttr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
termAttr.c_iflag &= ~(IXON | IXOFF | IXANY);
termAttr.c_oflag &= ~OPOST;
termAttr.c_cc[VMIN] = 5;
termAttr.c_cc[VTIME] = 5;
tcsetattr(fd, TCSANOW, &termAttr);
...................
void PowerUp(void){
unsigned char *p_commands,ima,komanda = STATUS_REQUEST;
p_commands = &comandi[0];
unsigned char *p_tx_buffer_;
for (;;) {
if ((milisekundi == 10) || (milisekundi == 30) || (milisekundi == 50)
|| (milisekundi == 70) || (milisekundi == 90)) {
makeTXpaket(0x00);
makeTXpaket(komanda);
p_tx_buffer_ = &tx_buffer[1];
nbytes = write(fd, tx_buffer, *p_tx_buffer_);
printf("%d"errno);
if (nbytes != sizeof(tx_buffer)) {
}
sleep(0.2);
bytes = read(fd, rx_buffer, sizeof(rx_buffer));
printf("%d"errno);
if (bytes != sizeof(rx_buffer)) {
}
printf("%X\n", rx_buffer);
}
.....................
n回の書き込みとシリアル通信の停止からの読み取りの後、errno = 4これを解決する方法?なぜシリアル通信が停止するのですか?私のタイマーはwrite()関数を中断しましたが、これが理由です?助けてくれてありがとう!