Difference between revisions of "Сериен порт"
From Ilianko
m (Protected "Упражнение 3. Сериен порт (1)" ([edit=sysop] (indefinite) [move=sysop] (indefinite))) |
|||
Line 3: | Line 3: | ||
<code><pre> | <code><pre> | ||
− | #include < | + | #include <stdio.h> /* Standard input/output definitions */ |
− | #include < | + | #include <string.h> /* String function definitions */ |
− | #include < | + | #include <unistd.h> /* UNIX standard function definitions */ |
− | #include < | + | #include <fcntl.h> /* File control definitions */ |
− | #include < | + | #include <errno.h> /* Error number definitions */ |
− | + | #include <termios.h> /* POSIX terminal control definitions */ | |
− | + | ||
− | + | int main(void) | |
− | + | { | |
− | + | int fd; // File descriptor for the port | |
− | + | struct termios options; // Serial port settings | |
− | + | ||
− | + | ||
− | + | fd = open( "/dev/ttyS0", 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; | |
− | + | } | |
− | + | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</pre></code> | </pre></code> |
Revision as of 21:35, 19 March 2011
Сериен порт
#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 */
int main(void)
{
int fd; // File descriptor for the port
struct termios options; // Serial port settings
fd = open( "/dev/ttyS0", 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;
}