Linux serial port asynchronous communication development source code

xiaoxiao2021-03-06  14

// gcc -o queryserial queryserial.c

#include #include #include #include #include

/ * THESE ARE THE HASH Definitions * / # define userbaud1200 '1' '2' # Define userbaud2400 '2' '4' # Define userbaud9600 '9' '6' # define userbaud1920 '1' '9' # Define userbaud3840 '3' '8' # define dev "/ dev / ttys0" struct termios ti)

/ * WRITE THEERS Command Out The Serial Port * / Int Send_cmd (int FTTY, CHAR * STR) {Int Result; Result = Write (FTTY, STR, STRLEN (STR)); / * Argv [4], Strlen (Argv [4])); * / if (Result <0) {Printf ("Write Command:% S TO VC312 FAILED / N", STR); Close (ftty); exit (1);}} int init_dev (Struct Termios * Oldtio) {Int fd, status; unsigned char cmd [17]; struct termios newTIO; // open carmo device fd = open (dev, o_rdwr | o_nOctty | o_ndlay); if (fd == - 1) {PERROR (" Open "dev); return -1;} else // set to block; fcntl (fd, f_setfl, 0);

Printf ("Open Carmo OK / N");

tcgetattr (fd, oldtio); // save current serial port settings bzero (& newtio, sizeof (newtio)); // clear struct for new port settings // configure the serial port; cfsetispeed (& newtio, B115200); cfsetospeed (& newtio, B115200); newTIO.C_CFLAG | = clocal | cread; / * 8n1 * / newtio.c_cflag & = ~ csize; / * mask the character size bits * / newtio.c_cflag | = CS8; / * SELECT 8 DATA BITS * / NEWTIO .c_cflag & = ~ parenb; newtio.c_cflag & = ~ cstopb; newTIO.C_CFLAG & = ~ CRTSCTS; // disable hardware flow control; newtio.c_lflag & = ~ (Icanon | Echo | Echoe | ISIG); / * RAW INPUT * / NewTIO.C_OFLAG & = ~ opost; / * Raw Output * / TCFLUSH (FD, TCIFLUSH); // Clear INPUT BUFFER NEWTIO.C_CC [VTIME] = 100; / * Inter-Character Timer Unused * / NewTIO.C_CC [ Vmin] = 0; / * blocking read untric 0 character arrives * / tcsetattr (fd, tcsanow, & newtio); / * ioctl (fd, tiocmget, & status); Printf ("The Serial Status IS 0x% X / N", Status | = TiOCM_RTS; IOCTL (FD, Tiocmset, & status); Status = 0; IOCTL (FD, Tiocmget, & Status); Printf ("The Serial Status NOW IS 0x% x / n", status); * / RETURN FD;} int = 0; fd_fd, len, count = 0; fd_set infut; struct timeval timeout; char buffer [640 * 500 * 3]; char * ptr = buffer;

FD_ZERO (& INPUT); fd_set (ftty, & infut); max_fd = ftty 1; / * do the select * / while (1) {/ * Initialize the timeout structure * / timeout.tv_sec = 1; timeout.tv_usec = 0; n = SELECT (MAX_FD, & INPUT, NULL, NULL, & TIMEOUT); if (n <0) PERROR ("SELECT FAILED"); ELSE IF (n == 0) {PUTS ("Timeout"); Break;} else { IOCTL (ftty, fion, & len); if (! len) {fprintf (stderr, "communication closed by server / n"); retURN -1;} len = read (ftty, ptr, len); // ptr [LEN ] = 0; // Printf ("READED% D DATA:% S / N", LEN, PTR); Write (streamfd, ptr, len); PTR = LEN; count = le;}} buffer [count] = 0; Printf ("Readed% D DATA:% S / N", Count, Buffer; Return 1; Int Main (int Argc, char * argv []) {Int fd, count; char buffer [255]; Char cmd [30]; int socket; struct termios oldtio; // cmd [0] = 0xff; cmd [1] = 0xE1; cmd [2] = 0x52; cmd [3] = 0x52; cmd [4] = 0x80; cmd [5] = 0x80; cmd [6] = 0; // IF (send_cmd (fd, cmd) <1) EXI T (0); // USleep (400 * 1000); fd = init_dev (& oldtio); if (fd <0) exit (-1); cmd [0] = 0xff; cmd [1] = 0xE1; cmd [2 ] = 0x58; CMD [3] = 0x58; cmd [4] = 0x66; cmd [5] = 0x66; cmd [6] = 0; IF (send_cmd (fd, cmd) <1) exit (0); Sprintf Filename, "1.jpg"); //"/tmp/get%d.jpg" ,i); socket = Open (filename, o_creat | o_rdwr); read_frame (fd, socket); Close (socket); TcSetattr FD, TCSANOW, & Oldtio);

转载请注明原文地址:https://www.9cbs.com/read-48773.html

New Post(0)