I. Linux The following serial port programming and those components
1. Open the serial port
2. Serial port initialization
3. Read or write serial ports
4. Close the serial port
2. Opening of Serial Port
Since the serial port is in the linux It is regarded as a file, so it should be opened before it is operated.
1. In Linxu, serial devices are accessed through serial terminal device files, that is, by accessing / dev/ttyS0,/dev/ttyS1,/dev/ttyS2.
2. Call the open() function to replace the serial device. For the open operation of the serial port, the O_NOCTTY parameter must be used.
O_NOCTTY: This means that a terminal device is opened and the program will not become the control terminal of the port. If this flag is not used, a task input (eg: keyboard stop signal, etc.) will affect the process.
O_NDELAY: Indicates that you do not care about the state of the DCD signa l line (whether the other end of the port is activated or stopped).
3. Opening the serial module has that and some components.
1 > Call the open() function to open the serial port and get the device file descriptor of the serial port
2 > Obtain the state of serial port and judge whether it is blocked or not
3>test Is the open file descriptor a terminal device?
Procedures:
/*****************************************************************
* Name: * UART0_Open
* Function: Open the serial port and return the description of the serial device file
* Entry parameter: fd File descriptor port: serial password (ttyS0,ttyS1,ttyS2)
* Export parameter: * Correct return to 1, error return to 0
*****************************************************************/
int UART0_Open(int fd,char* port)
{
fd = open( port, O_RDWR|O_NOCTTY|O_NDELAY);
if (FALSE == fd)
{
perror("Can't Open Serial Port");
return(FASLE);
}
// Determine whether the state of the serial port is blocked or not.
if(fcntl(fd, F_SETFL, 0) < 0)
{
printf("fcntl failed!/n");
return(FALSE);
}
else
{
printf("fcntl=%d/n",fcntl(fd, F_SETFL,0));
}
//test Is it a terminal device?
if(0 == isatty(STDIN_FILENO))
{
printf("standard input is not a terminal device/n");
return(FALSE);
}
else
{
printf("isatty success!/n");
}
printf("fd->open=%d/n",fd);
return fd;
}
3. Initialization of Serial Port
1. Serial port initialization in linux is the same as the previous serial port initialization. Serial port baud rate, data flow control, frame format (i.e. number of data bits, stop bits, check bits, data flow control) need to be set.
2. The serial initialization module consists of those parts:
Setting baud rate
2 > Setting Data Flow Control
2 > Format the frame (i.e. number of data bits, stop bits, check bits)
Brother John explained:
1 > termios structure is used when setting serial port parameters, so the function must be used first.
Tcgettattr (fd, & options) obtains the pointer of serial port to termios structure.
2 > The input/output baud rate of serial port is set by cfsetispeed function and cfsetospeed function. In general, the input and output baud rates are equal.
3 > Setting data bits can be achieved by modifying c_flag in termios mechanism. CS5, CS6, CS7 and CS8 correspond to 5, 6, 7 and 8 data bits. When setting data bits, CSIZE must be used as bit shielding.
4 > What methods are used to mark the beginning and end of data transmission in data flow control?
5 > After setting baud rate, data flow control, data bits, check bits, stop bits, stop bits, minimum waiting time and minimum receiving characters are set.
6 > After the configuration is completed, the configuration is activated through the tcsetattr() function.
3. Procedures:
/*******************************************************************
* Name: * UART0_Set
* Function: Set up serial data bits, stop bits and validation bits
* Entry parameters: fd Serial Port File Descriptor
* speed
* flow_ctrl Data Flow Control
* Data bits. Data bits. Value 7 or 8
* Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits
* Values of parity type are N, E, O, S
* Export parameter: * Correct return to 1, error return to 0
*******************************************************************/
int UART0_Set(int fd,int speed,int flow_ctrl,int databits,int stopbits,int parity)
{
int i;
int status;
int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300 };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300 };
struct termios options;
/* tcgetattr (fd, & options) obtains the parameters related to the FD pointing object and saves them in options. This function can also test whether the configuration is correct, whether the serial port is available, etc. If the call is successful, the return value of the function is 0, and if the call fails, the return value of the function is 1.
*/
if ( tcgetattr( fd,&options) != 0)
{
perror("SetupSerial 1");
return(FALSE);
}
// Setting Serial Port Input and Output Baud Rates
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if (speed == name_arr[i])
{
cfsetispeed(&Options, speed_arr[i]);
cfsetospeed(&Options, speed_arr[i]);
}
}
// Modify the control mode to ensure that the program does not occupy the serial port
options.c_cflag |= CLOCAL;
// Modify the control mode so that input data can be read from serial port
options.c_cflag |= CREAD;
// Setting up data flow control
switch(flow_ctrl)
{
case 0: // No Flow Control
options.c_cflag &= ~CRTSCTS;
break;
case 1: // Use Hardware Flow Control
options.c_cflag |= CRTSCTS;
break;
case 2: // Using Software Flow Control
options.c_cflag |= IXON | IXOFF | IXANY;
break;
}
// Setting data bits
Options.c_cflag &=~CSIZE; //Shield other flags
switch (databits)
{
case 5 :
options.c_cflag |= CS5;
break;
case 6 :
options.c_cflag |= CS6;
break;
case 7 :
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size/n");
return (FALSE);
}
// Setting Check Bit
switch (parity)
{
case 'n':
Case'N': // No parity bit.
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'o':
Case'O': // Set to Odd Check
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case 'e':
Case'E': // Set to Dual Check
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 's':
Case'S': // Set to Space
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity/n");
return (FALSE);
}
// Setting stop bits
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits/n");
return (FALSE);
}
// Modify the output mode to output raw data
options.c_oflag &= ~OPOST;
// Set waiting time and minimum receive character
options.c_cc[VTIME] = 1; /* Read a character and wait for 1*(1/10)s*/
options.c_cc[VMIN] = 1; /* The minimum number of characters read is 1*/
// If a data overflow occurs, the data is received, but no longer read.
tcflush(fd,TCIFLUSH);
// Activation configuration (set the modified termios data to the serial port)
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("com set error!/n");
return (FALSE);
}
return (TRUE);
}
/*******************************************************************
* Name: * UART0_Init()
* Function: Serial Initialization
* Entry parameters: fd file descriptor
* speed
* flow_ctrl Data Flow Control
* Data bits. Data bits. Value 7 or 8
* Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits Stop bits
* Values of parity test type are N, E, O, S.
* Export parameters:) Correct return to 1, error return to 0
*******************************************************************/
int UART0_Init(int fd, int speed,int flow_ctrlint databits,int stopbits,int parity)
{
int err;
// Setting Serial Data Frame Format
if (UART0_Set(fd,115200,0,8,1,'N') == FALSE)
{
return FALSE;
}
else
{
return TRUE;
}
}
Note:
If it's not a development terminal or something like that, but a serial port to transmit data, without the need for serial port to process, then use Raw Mode mode to communicate, the settings are as follows:
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
options.c_oflag &= ~OPOST; /*Output*/
4. Read-write function of serial port:
1. Read-write serial port is realized by using read function and write function.
2. Procedures
/*******************************************************************
* Name: * UART0_Recv
* Function: Receiving Serial Data
* Entry parameter: fd File descriptor
* rcv_buf: Data in receiving serial port is stored in rcv_buf buffer
* data_len: The length of a frame of data
* Export parameters:) Correct return to 1, error return to 0
*******************************************************************/
int UART0_Recv(int fd, char *rcv_buf,int data_len)
{
int len,fs_sel;
fd_set fs_read;
struct timeval time;
FD_ZERO(&fs_read);
FD_SET(fd,&fs_read);
time.tv_sec = 10;
time.tv_usec = 0;
// Using select to realize multi-channel communication of serial port
fs_sel = select(fd+1,&fs_read,NULL,NULL,&time);
if(fs_sel)
{
len = read(fd,data,data_len);
return len;
}
else
{
return FALSE;
}
}
/*******************************************************************
* Name: UART0_Send
* Function: Send data
* Entry parameter: fd File descriptor
* send_buf: Store serial port to send data
* data_len: Number of data in a frame
* Export parameters:) Correct return to 1, error return to 0
*******************************************************************/
int UART0_Send(int fd, char *send_buf,int data_len)
{
int len = 0;
len = write(fd,send_buf,data_len);
if (len == data_len )
{
return len;
}
else
{
tcflush(fd,TCOFLUSH);
return FALSE;
}
}
V. Close the serial port
After completing the operation on the serial device, the close function is called to close the file descriptor.
Procedures:
/******************************************************
* Name: * UART0_Close
* Function: Close the serial port and return the description of the serial device file
* Entry parameter: [fd]: File descriptor
* Export parameters: void
*******************************************************************/
void UART0_Close(int fd)
{
close(fd);
}
A complete procedure