Difference between revisions of "Сериен порт"
From Ilianko
Line 28: | Line 28: | ||
#include <errno.h> /* Error number definitions */ | #include <errno.h> /* Error number definitions */ | ||
#include <termios.h> /* POSIX terminal control definitions */ | #include <termios.h> /* POSIX terminal control definitions */ | ||
+ | |||
+ | #define PORTNAME "/dev/ttyS0" | ||
int main(void) | int main(void) | ||
Line 35: | Line 37: | ||
− | fd = open( | + | fd = open( PORTNAME, O_RDWR | O_NOCTTY | O_NONBLOCK); |
if (fd == -1)//Could not open the port. | if (fd == -1)//Could not open the port. |
Revision as of 14:53, 22 March 2011
Contents
Цел на упражнението
Преговор типове данни, оператори. Достъп до сериен порт.
Променливи
Преобразуване на буква в двоичен код
Проверка по четност
Инсталация GTKTerm
:~$ sudo apt-get install gtkterm
Комуникация през терминал и сериен порт
Комуникация през C, POSIX и сериен порт
Сериен порт
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#define PORTNAME "/dev/ttyS0"
int main(void)
{
int fd; // File descriptor for the port
struct termios options; // Serial port settings
fd = open( PORTNAME, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd == -1)//Could not open the port.
perror("open_port: Unable to open port");
else
fcntl(fd, F_SETFL, 0);
tcgetattr(fd, &options);
cfsetispeed(&options, B2400);
cfsetospeed(&options, B2400);
options.c_cflag |= ( CREAD);
options.c_cflag |= ( CLOCAL);
options.c_cflag &= ( ~CSTOPB);
tcsetattr(fd,TCSANOW, &options);
write(fd, "a \n", 3); \\write to serial port
return 0;
}