如何实现在LINUX系统上直接控制串口RTS信号电平
如何实现在LINUX系统上直接控制串口RTS信号电平,使RTS信号有效或无效 <br />现在需要直接控制这个信号来控制一个设备。哪位知道请告诉一下非常感谢!最好给个例子,急!!在线等啊在下用下面代码控制过
#include <stdio.h><br />#include <sys/types.h><br />#include <sys/stat.h><br />#include <sys/ioctl.h><br />#include <fcntl.h><br />#include <signal.h><br />#include <termios.h><br />#define false 0<br />#define true 1<br /><br />void sighandler ( int status );<br />int setup_com(int fd);<br /><br />int wait_flag = false;<br />/* set up the options for serial port */<br /><br />int fd;<br /><br /><br />int setup_com(int fd)<br />{<br /> struct termios options; <br /> <br /> tcgetattr(fd, &options);<br /> /*<br /> * Set the baud rates to 115200...<br /> */<br /> cfsetispeed(&options, B57600);<br /> cfsetospeed(&options, B57600);<br /><br /> /*<br /> * Enable the receiver and set local mode...<br /> */<br /><br /> options.c_cflag |= (CLOCAL | CREAD);<br /><br /> /*<br /> * Set global options.<br /> */<br /> options.c_cflag &= ~PARENB;<br /> options.c_cflag &= ~PARODD;<br /> options.c_cflag &= ~CSTOPB;<br /> options.c_cflag &= ~CSIZE;<br /> options.c_cflag |= CS8;<br /> <br /> /* set the input options */<br /><br /> options.c_iflag &=~(IXON | IXOFF | IXANY);<br /> options.c_iflag &=~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);<br /> options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);<br /> //options.c_lflag |= ISIG;<br /><br /> /* set the output options */<br /> options.c_oflag=0;// &= ~OPOST; <br /><br /> /* set the timeout options */<br /> options.c_cc = 0;<br /> options.c_cc = 10;<br /><br /> tcsetattr(fd, TCSANOW, &options);<br /> <br /> return 1;<br /><br /><br /><br />}<br /><br /><br />void sighandler ( int status )<br />{<br /> printf("receive data.");<br /> wait_flag = true;<br />}<br /><br /><br />int main(int argc, char **argv)<br />{<br /><br /> int nbytes,i;<br /> unsigned char *buffer;<br /> int ctrlbits;<br /><br /> struct sigaction action; <br /> <br /> fd = open("/dev/ttyS2", O_RDWR | O_NOCTTY | O_NDELAY );<br /> <br /> if (fd == -1)<br /> {<br /> perror("open_port: Unable to open /dev/ttyS0 - ");<br /> return -1;<br /> }<br /> else<br /> {<br /><br /> action.sa_handler = sighandler;<br /> action.sa_flags = 0;<br /> sigaction(SIGIO, &action, NULL);<br /><br /> fcntl(fd, F_SETOWN, getpid());<br /> <br /> fcntl(fd,F_SETFL,FASYNC);<br /> //fcntl(fd, F_SETFL, 0);<br /> <br /> setup_com(fd); <br /><br /> //receive enable<br /> ioctl(fd,TIOCMGET,&ctrlbits);<br /><br /> ctrlbits |= TIOCM_RTS;//set enable<br /> //ctrlbits &= ~TIOCM_RTS;//disable<br /> <br /> ioctl(fd,TIOCMSET,&ctrlbits);<br /><br /><br /> <br /> while(1)<br /> {<br /> if (wait_flag == true )<br /> {<br /> <br /> if((nbytes = read(fd, buffer, 5)) > 0)<br /> printf("nbytes =0x%x.
",nbytes);<br /> for(i=0;i<nbytes;i++)<br /> {<br /> printf("the receive data =0x%x.
",buffer);<br /> }<br /> <br /> printf("send data.
");<br /> //send enable<br /> ioctl(fd,TIOCMGET,&ctrlbits);<br /> ctrlbits &= ~TIOCM_RTS;//send enable<br /> ioctl(fd,TIOCMSET,&ctrlbits);<br /> <br /> buffer+=1;<br /> <br /> if((nbytes=write(fd, buffer, 1))>0);<br /> sleep(1);<br /> <br /> //receive enable<br /> ioctl(fd,TIOCMGET,&ctrlbits);<br /> ctrlbits |= TIOCM_RTS;//send enable<br /> ioctl(fd,TIOCMSET,&ctrlbits);<br /> wait_flag = false;<br /> <br /> }<br /> <br /> }<br /><br /> close(fd);<br /> }<br /><br /> return 0; <br />}
问题已经解决
问题已经解决了<br />谢谢freeworld
页:
[1]