Linux C Serial Programming __HTML5

Source: Internet
Author: User
arch/arm/include/asm/termbits.h struct Termios {tcflag_t c_iflag;		/* Input Mode Flags * * tcflag_t C_oflag;		/* Output Mode flags */tcflag_t C_cflag;		/* Control Mode Flags */tcflag_t C_lflag;			/* Local Mode Flags */cc_t c_line;		/* Line Discipline * * cc_t C_CC[NCCS];
/* Control characters */};
The setting of the serial port is mainly to set up the members of the struct Termios structure/** *s3c6410 serial_test * Test when the application runs in the background./serial_test &/#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include & lt;fcntl.h>//File control definition #include <termios.h>//terminal control definition #include <errno.h> #define DEVICE "/dev/s3c2410_

serial0 "int serial_fd = 0; Open the serial port and initialize the settings init_serial (void) {serial_fd = open (DEVICE, O_RDWR | O_noctty |
	O_ndelay);
		if (SERIAL_FD < 0) {perror ("open");
	return-1;
	
	}//serial port main set structure Termios <termios.h> struct termios options; /**1.
	The TCGETATTR function is used to obtain terminal-related parameters. * Parameter FD is a terminal file descriptor, and the returned result is stored in the Termios structure/tcgetattr (SERIAL_FD, &options); /**2. Modify the obtained parameters * * Options.c_cflag |= (clocal | Cread)//Set control mode state, local connection, receive enable to Options.c_cflag &= ~csize;//character length, before setting data bits must screen this bit options.c_cflag &= ~crtscts;// No hardware flow control Options.c_cflag |= CS8;//8 bit data length Options.c_cflag &= ~cstopb;//1 bit stop bit options.c_iflag |= ignpar;//no parity bit option S.c_oflag = 0; Output mode Options.c_lflag = 0; Do not activate terminal mode Cfsetospeed (&options, B115200);//Set baud rate/**3.
	
	Set new properties, Tcsanow: All changes immediately take effect/Tcflush (SERIAL_FD, Tciflush);//Overflow data can be received, but not read tcsetattr (SERIAL_FD, Tcsanow, &options);
return 0;
	/** * Serial Port Send data * @fd: Serial descriptor * @data: Data to be sent * @datalen: Data length */int uart_send (int fd, char *data, int datalen) {int len = 0;
	Len = Write (fd, data, datalen);//Actual write length if (len = = datalen) {return len;
	else {Tcflush (fd, Tcoflush);//tcoflush refreshes the written data but does not transfer return-1;
return 0;
	/** * Serial port receive data * Request to start, send ASCII file on PC side/int uart_recv (int fd, char *data, int datalen) {int len=0, ret = 0;
	Fd_set Fs_read;
	
	struct Timeval tv_timeout; Fd_zero (&fs_read);
	Fd_set (FD, &fs_read);
	Tv_timeout.tv_sec = (10*20/115200+2);
	
	tv_timeout.tv_usec = 0;
	ret = Select (fd+1, &fs_read, NULL, NULL, &tv_timeout);
	printf ("ret =%d\n", ret);
		If 0 is returned, the representation exceeds the timeout time before the descriptor state changes, and the error returns-1 if (Fd_isset (FD, &fs_read)) {len = read (fd, data, datalen);
		printf ("Len =%d\n", Len);
	return Len;
		else {perror ("select");
	return-1;
return 0;

	int main (int argc, char **argv) {init_serial ();
	Char buf[]= "Hello World";
	Char buf1[10];
	Uart_send (SERIAL_FD, buf, 10);

	printf ("\ n");
	
	
	Uart_recv (SERIAL_FD, BUF1, 10);
	printf ("UART receive%s\n", BUF1);
	Close (SERIAL_FD);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.