The OpenNET Project / Index page

[ новости /+++ | форум | теги | ]

програмирование RS232 (raw io port rs232 example)


<< Предыдущая ИНДЕКС Поиск в статьях src Установить закладку Перейти на закладку Следующая >>
Ключевые слова: raw, io, port, rs232, example,  (найти похожие документы)
Date: Fri, 06 Apr 2001 00:12:54 +0400 From: "Vitaly A. Repin" <Vitaly.A.Repin@p26.f209.n5030.z2.fidonet.org> Newsgroups: fido7.ru.unix Subject: програмирование RS232 > struct termios par; > tty = open( arv[1], O_RDONLY|O_NDELAY); > ... > tcgetattr(tty, &par); > par.c_iflag |= IGNPAR|ICRNL; > par.c_cflag |= CREAD|CLOCAL; > par.c_cc[VTIME] = 0; > par.c_cc[VMIN] = 1; > tcsetattr(tty, TCSAFLUSH, &par); > while (1) > { > read(tty, &c, 1); > ... > putchar(c); > } Для начала я бы проверил результат ну хотя бы open и tcsetattr. Всё же писать надо грамотно. А вот пример из моей (работающей) программы: Инициализация: fd=open(device.c_str(),O_RDWR | O_NOCTTY | O_NDELAY); if(fd==-1) throw open_error(errno); if(tcgetattr(fd, &options)!=0) throw attr_error(errno); old_options=options; set_baud_rate(speed, speed); //setting similar speed on in and out //settings for our project set_parity(no); options.c_lflag &=~(ICANON|ECHO|ECHOE|ISIG); //row mode options.c_cflag &= ~CRTSCTS; //disable hardware flow control options.c_iflag &= ~(IXON|IXOFF|IXANY); //disable software flow control options.c_cflag |= (CLOCAL | CREAD); //enable receiver & set local mode if(tcsetattr(fd, TCSAFLUSH, &options)!=0) throw attr_error(errno); Чтение: int serial_port::read(unsigned char *buf, size_t count) throw(read_error) //return number of really readed chars { fcntl(fd, F_SETFL, FNDELAY); int cnt=::read(fd,buf,count); if(cnt==-1) throw read_error(errno); return cnt; }; Иногда также полезно узнать кол-во инф-ии в буфере: size_t serial_port::InCnt() throw(ioctl_error) { int bytes; if(ioctl(fd,FIONREAD, &bytes)!=0) throw ioctl_error(errno,"FIONREAD call failed"); return bytes; }; Good-bye! -- WBR & WBW, Vitaly.

<< Предыдущая ИНДЕКС Поиск в статьях src Установить закладку Перейти на закладку Следующая >>

 Добавить комментарий
Имя:
E-Mail:
Заголовок:
Текст:




Партнёры:
PostgresPro
Inferno Solutions
Hosting by Hoster.ru
Хостинг:

Закладки на сайте
Проследить за страницей
Created 1996-2024 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру