код взят из примеров производителя шлюза rs232<>1-wire DS2480.
//--------------------------------------------------------------------------
// Write an array of bytes to the COM port, verify that it was sent out. Assume that baud rate has been set.
//
// 'outlen' - number of bytes to write to COM port
// 'outbuf' - pointer ot an array of bytes to write
//
// Returns: TRUE(1) - success
// FALSE(0) - failure
//
int WriteCOM(int fd, int outlen, unsigned char *outbuf){
unsigned i;
int size_written;
// timer timeout vars
struct sigevent ev;
// timeout
_Uint64t ui_timeout;
// calculate a timeout
ui_timeout = (80)*mSEC_NSEC;
ev.sigev_notify = SIGEV_UNBLOCK;
// write the byte
for ( i=0;i<outlen;i++) {
TimerTimeout( CLOCK_REALTIME, _NTO_TIMEOUT_SEND | _NTO_TIMEOUT_REPLY, &ev, &ui_timeout, NULL );
size_written=write(fd , &outbuf[i] , 1);
tcdrain(fd);
if (size_written !=1 ){
printf("transmit timeout \n");
return 0;
}
msDelay(2);
}
if (dodebug) { //debug
for ( i = 0; i < outlen; i++) printf(">%02X",outbuf[i]);
printf("\n");
}
// check results of write
return 1;
}
Re: посоветуйте как оптимальней портировать qnx posix non-compatible код на линукс
//--------------------------------------------------------------------------
// Read an array of bytes to the COM port, verify that it was
// sent out. Assume that baud rate has been set.
//
// 'inlen' - number of bytes to read from COM port
// 'inbuf' - pointer to a buffer to hold the incomming bytes
//
// Returns: number of characters read
//
int ReadCOM(int fd, int inlen, unsigned char *inbuf){
int size_read;
int i;
struct sigevent ev; // timer timeout vars
_Uint64t ui_timeout; // timeout
ev.sigev_notify=SIGEV_UNBLOCK;
ui_timeout = (20 * inlen + 60)*mSEC_NSEC; // calculate a timeout
// printf ("start read...");
TimerTimeout( CLOCK_REALTIME, _NTO_TIMEOUT_SEND | _NTO_TIMEOUT_REPLY, &ev, &ui_timeout, NULL );
// read
size_read = read( fd, inbuf, inlen );
// printf ("...stop read\n");
// check results
if (size_read>0){
if (dodebug) { // debug
// printf ("number read bytes = %d\n",size_read);
for (i = 0; i < size_read; i++)
printf("<%02X",inbuf[i]);
printf("\n");
}
return size_read;
} else if (size_read==-1) {
// printf ("timeout read \n");
return 0;
}
return size_read;
}
Re: посоветуйте как оптимальней портировать qnx posix non-compatible код на линукс
точнее меня интересует возможность замены блокирования с тайм-аутом (TimerTimeout()) обычным блокированием или целесообразность иммитациии при помощи select + неблокируемый i/o
рвать сисколы сигналами от таймера пока не собираюсь хотя чего в жизни не бывает
Re: посоветуйте как оптимальней портировать qnx posix non-compatible код на линукс
понял то ты правильно. вопрос стоит ли это полностью повторять и как именно. чтение с тайм-аутом у меня уже есть на select. его и заюзаю. а вот с записью я ещё в рассуждениях.
а за timer_create - спасибо. впринципе позволят реализовать TimerTimeout
Re: посоветуйте как оптимальней портировать qnx posix non-compatible код на линукс
> а за timer_create - спасибо. впринципе позволят реализовать TimerTimeout
только для 2.6.
есть еще setitimer(). оно и проще (но и более ограниченный).
работает в 2.4