|
小弟最近在研究LINUX的串口编程,看过了《Linux 程序设计》和《Linux Serial Programming HOW-TO》上的有关串口编程的介绍。我试验着把COM1和COM2直接连接起来。稍微改编了一下lpg中miniterm.c,分成两个程序read_com.c和write_com.c。我分别在tty1和tty2运行这两个程序。令人不解的是这两个程序不能正常的通讯(我的COM1和COM2经测试,同一条线,在WINDOWS下是好的)。另外,为何两个程序的打开的文件描述符都是3?
read_com.c和write_com.c分别如下。望高手指点帮着解决这个问题,并希望再点拨小弟一下有关Linux串口编程的细节(我想用两个串口,每个控制一个设备)。如果有更详细的资料或者是代码请告诉小弟。谢谢!!
- /* File: write_com.c */
- #include <termios.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/signal.h>
- #define BAUDRATE B38400
- #define MODEMDEVICE "/dev/ttyS1"
- #define ENDMINITERM 2 /* ctrl-b to quit miniterm */
- #define _POSIX_SOURCE 1 /* POSIX compliant source */
- #define FALSE 0
- #define TRUE 1
- volatile int STOP = FALSE;
- void child_handler(int s)
- {
- STOP = TRUE;
- }
- main()
- {
- int fd, c;
- struct termios oldtio, newtio, oldstdtio, newstdtio;
- struct sigaction sa;
- /*
- Open modem device for reading and writing and not as controlling tty
- because we don't want to get killed if linenoise sends CTRL-C.
- */
- fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
- if (fd < 0) {
- perror(MODEMDEVICE);
- exit(-1);
- }
- tcgetattr(fd, &oldtio); /* save current modem settings */
- /*
- Set bps rate and hardware flow control and 8n1 (8bit,no parity,1 stopbit).
- Also don't hangup automatically and ignore modem status.
- Finally enable receiving characters.
- */
- newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
- /*
- Ignore bytes with parity errors and make terminal raw and dumb.
- */
- newtio.c_iflag = IGNPAR;
- /*
- Raw output.
- */
- newtio.c_oflag = 0;
- /*
- Don't echo characters because if you connect to a host it or your
- modem will echo characters for you. Don't generate signals.
- */
- newtio.c_lflag = 0;
- /* blocking read until 1 char arrives */
- newtio.c_cc[VMIN] = 1;
- newtio.c_cc[VTIME] = 0;
- /* now clean the modem line and activate the settings for modem */
- tcflush(fd, TCIFLUSH);
- tcsetattr(fd, TCSANOW, &newtio);
- /*
- Strange, but if you uncomment this command miniterm will not work
- even if you stop canonical mode for stdout. This is a linux bug.
- */
- // tcsetattr(1, TCSANOW, &newtio); /* stdout settings like modem settings */
- /* next stop echo and buffering for stdin */
- tcgetattr(0, &oldstdtio);
- tcgetattr(0, &newstdtio); /* get working stdtio */
- newstdtio.c_lflag &= ~(ICANON | ECHO);
- tcsetattr(0, TCSANOW, &newstdtio);
- /* terminal settings done, now handle in/ouput */
- switch (fork()) {
- case 0: /* child */
- /* user input */
- //close(1); /* stdout not needed */
- printf("Fork child process\n");
- for (c = getchar(); c != ENDMINITERM; c = getchar()) {
- write(fd, &c, 1);
- printf("write %c to fd %d\n", c, fd);
- }
- tcsetattr(fd, TCSANOW, &oldtio); /* restore old modem setings */
- tcsetattr(0, TCSANOW, &oldstdtio); /* restore old tty setings */
- close(fd);
- exit(0); /* will send a SIGCHLD to the parent */
- break;
- case -1:
- perror("fork");
- tcsetattr(fd, TCSANOW, &oldtio);
- close(fd);
- exit(-1);
- default: /* parent */
- //close(0); /* stdin not needed */
- printf("Fork parent process\n");
- sa.sa_handler = child_handler;
- sa.sa_flags = 0;
- sigaction(SIGCHLD, &sa, NULL); /* handle dying child */
- while (STOP == FALSE) { /* modem input handler */
- printf("Looping for read...\n");
- read(fd, &c, 1); /* modem */
- //write(1, &c, 1); /* stdout */
- printf("receive %c\n");
- }
- wait(NULL); /* wait for child to die or it will become a zombie */
- break;
- }
- }
复制代码
- /* File: read_com.c */
- #include <termios.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/signal.h>
- #define BAUDRATE B38400
- #define MODEMDEVICE "/dev/ttyS0"
- #define ENDMINITERM 2 /* ctrl-b to quit miniterm */
- #define _POSIX_SOURCE 1 /* POSIX compliant source */
- #define FALSE 0
- #define TRUE 1
- volatile int STOP = FALSE;
- void child_handler(int s)
- {
- STOP = TRUE;
- }
- main()
- {
- int fd, c;
- struct termios oldtio, newtio, oldstdtio, newstdtio;
- struct sigaction sa;
- /*
- Open modem device for reading and writing and not as controlling tty
- because we don't want to get killed if linenoise sends CTRL-C.
- */
- fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
- if (fd < 0) {
- perror(MODEMDEVICE);
- exit(-1);
- }
- tcgetattr(fd, &oldtio); /* save current modem settings */
- /*
- Set bps rate and hardware flow control and 8n1 (8bit,no parity,1 stopbit).
- Also don't hangup automatically and ignore modem status.
- Finally enable receiving characters.
- */
- newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
- /*
- Ignore bytes with parity errors and make terminal raw and dumb.
- */
- newtio.c_iflag = IGNPAR;
- /*
- Raw output.
- */
- newtio.c_oflag = 0;
- /*
- Don't echo characters because if you connect to a host it or your
- modem will echo characters for you. Don't generate signals.
- */
- newtio.c_lflag = 0;
- /* blocking read until 1 char arrives */
- newtio.c_cc[VMIN] = 1;
- newtio.c_cc[VTIME] = 0;
- /* now clean the modem line and activate the settings for modem */
- tcflush(fd, TCIFLUSH);
- tcsetattr(fd, TCSANOW, &newtio);
- /*
- Strange, but if you uncomment this command miniterm will not work
- even if you stop canonical mode for stdout. This is a linux bug.
- */
- // tcsetattr(1, TCSANOW, &newtio); /* stdout settings like modem settings */
- /* next stop echo and buffering for stdin */
- tcgetattr(0, &oldstdtio);
- tcgetattr(0, &newstdtio); /* get working stdtio */
- newstdtio.c_lflag &= ~(ICANON | ECHO);
- tcsetattr(0, TCSANOW, &newstdtio);
- /* terminal settings done, now handle in/ouput */
- switch (fork()) {
- case 0: /* child */
- /* user input */
- //close(1); /* stdout not needed */
- printf("Fork child process\n");
- for (c = getchar(); c != ENDMINITERM; c = getchar()) {
- write(fd, &c, 1);
- printf("write %c to fd %d\n", c, fd);
- }
- tcsetattr(fd, TCSANOW, &oldtio); /* restore old modem setings */
- tcsetattr(0, TCSANOW, &oldstdtio); /* restore old tty setings */
- close(fd);
- exit(0); /* will send a SIGCHLD to the parent */
- break;
- case -1:
- perror("fork");
- tcsetattr(fd, TCSANOW, &oldtio);
- close(fd);
- exit(-1);
- default: /* parent */
- //close(0); /* stdin not needed */
- printf("Fork parent process\n");
- sa.sa_handler = child_handler;
- sa.sa_flags = 0;
- sigaction(SIGCHLD, &sa, NULL); /* handle dying child */
- while (STOP == FALSE) { /* modem input handler */
- printf("Looping for read...\n");
- read(fd, &c, 1); /* modem */
- //write(1, &c, 1); /* stdout */
- printf("receive %c\n", c);
- }
- wait(NULL); /* wait for child to die or it will become a zombie */
- break;
- }
- }
复制代码 |
|