Poll function and serial port settings. DOC

Source: Internet
Author: User
Tags bitmask

2015.1.24

Today Saturday, Cloudy, 8:17 get up, today is to Nanjing to get up the latest day, because last night sleep a little late, today do not have class, the whole person a little relaxed. Pack up and come to the classroom, the classroom door is not open, Hu Ming
Also arrived, the other people still sleep in the dormitory, waited for 10 minutes no one to open the door, decided to go out to stroll, to see Nanjing have any fun or special. 9 o'clock did not go out from the Hongyun mansion, back when the 10:10, left
One hours, Baidu map is still a little useful, at least not let me lost. Stroll around, before I think Nanjing is a big city, should be more prosperous or fashion, but now there is no such feeling, think of the future
Work, I think: it is not suitable for staying here. If you can, I would choose not to work in Nanjing, where there is no sense of belonging, it seems that wages are not good.
Because of the outside to buy the New Year home ticket, the station is not familiar, decided to go today to take the ticket, 1:40 left, and Zhang Hengbin together, back and forth the bus is his card, to Nanjing North station after the station is very small, take the ticket
Come out, because there is no need to know him, too small. After the station to ask Zhang Hengbin, want to stop here, he said: Good ah, this side to go to the bridge (Nanjing Yangtze River Bridge: The full bridge length of 4589 meters, road bridges longer than 1600 meters)! I'm right
Wandering around or more interested in Hangzhou, when a person on the Qiantang River Fuxing Bridge walked a back and forth, when it was night. On the bridge to find the Nanjing Yangtze River Bridge Quite long, a bite to continue to go, so, I and Zhang Hengbin two people
While walking and chatting, the bridge has the upper hand is very big, when the hot time I took off the clothes, but no one will be cold. Nanjing this side of the weather pollution is very serious, the bridge is gray, there are a lot of cargo ships under the bridge, took some photos, recorded the journey. Back
Dorm 4:10 around, because walk a long way, so feel more tired, mouth is relatively dry, eat a pear, in bed, sleep will, up when 6 points, wake up downstairs to eat dinner, go to row file called a braised crucian carp. A little extravagant.
, every day to eat rice bowls a little unbearable, so, a day has passed, the evening should learn!

IO multiplexing implemented by the poll function! , together with yesterday's Select function, they are here to achieve the same function, but the poll function is more efficient! A wider range of use!

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <fcnt1.h>

#include <poll.h>

#define MAX_BUFFER_SIZE 1024
#define In_feles 3
#define TIME_DELAY 60
#define MAX (A, B) ((a > B)? (a):(B))

int main (void)
{
struct POLLFD fds[in_files];
Char Buf[max_buffer_size];
int I, res, real_read, MAXFD;
/* First open another source file with a certain permission */
FDS[0].FD = 0;
if (FDS[1].FD = open ("in1", O_rdonly | O_nonblock)) < 0)
{
printf ("Open in1 error\n");
return 1;
}

if (FDS[2].FD = open ("in2", O_rdonly | O_nonblock)) < 0)
{
printf ("Open in2 error\n");
return 1;
}

for (i = 0; i < in_files; i++)
{
Fds[i].events = Pollin;
}
/* Loop to test if the file descriptor is ready and call the Select function to do the corresponding action on the relevant file descriptor */
while (fds[0].events | | fds[1].events | | fds[2].events)
{
If (poll (fds,in_files,0) < 0)
{
printf ("Poll error\n");
return 1;

}

for (i = 0; i < in_files; i++)
{
if (fds[i].revents)
{
memset (buf, 0, max_buffer_size);
Real_read = Read (FDS (fds[i].fd,buf,max_buffer_size);

if (Real_read < 0)
{
if (errno! = Eagain)
{
return 1;
}
}

else if (!real_read)
{
Close (FDS[I].FD);
fds[i].events = 0;
}
Else
{
if (i = = 0)
{
if ((buf[0] = = ' Q ') | | (buf[0] = = ' Q '))
{
return 1;
}
}
Else
{
Buf[real_read] = ' + ';
printf ("%s,buf");
}
}
}
}
}
Exit (0);
}

The serial port setting detailed:

The main port setting is to set the member values of the struct Termios structure body:

#include <termios.h>
struct Termios
{
unsigned short c_iflag; Input mode flag
unsigned short c_oflag; Output Mode flag
unsigned short c_cflag; Control Mode Flag
unsigned short c_lglag; Local mode flag
unsigned char c_line; Line code
unsigned char C_CC[NCC]; Control features
speed_t C_ispeed; Input speed
speed_t C_ospeed; Output speed
}

You cannot initialize a C_cflag member, and you want to use some of these options through the and or actions
The input mode flag C_iflag is used to control character input processing on the port's receiving end.

The following is a detailed explanation of the basic process of setting serial port properties:

1. Save the configuration of the original serial port:

if (tcgetattr (fd,&old_cfg)! = 0)
{
Perror ("tvgetattr");
return-1;
}

2. Activation options: clocal and Cread for local connection and accept enable, with bitmask

Newtio.c_cflag |= clocal | Cread;

Call the Dfmakeraw () function to set the terminal to the original mode

Cfmakeraw (&NEW_CFG);

3. Set the baud rate, set the baud rate has a special function,
Cfsetispeed (&new_cfg,b115200);
Cfsetospeed (&new_cfg,b115200);
In general, the user needs to set the input and output baud rate of the terminal to the same, these functions return 0 on success, and 1 on failure.

4. Set the character size, using bitmask: Generally first clear the bit mask in the data bit and re-set the requirements of the security

New_cfg.c_cflag &= ~csize; Emptying data bit settings with a data bit mask
New_cfg.c_cflag |= CS8;

5. Set the parity bit: first activate the check bit in C_cflag to enable flag Parenb and whether to do even check, but also
Activates the parity of the input data in the C_iflag to enable INPCK;

New_cfg.c_cflag |= (parodd | PARENB);
New_cfg.c_iflag |= INPCK;

To enable parity, the code is as follows:
New_cfg.c_cflag |= Parenb
New_cfg.c_cflag &= ~parodd Clear even check flag, configure as odd check
New_cfg.c_iflag |= INPCK;

6. Setting the STOP bit

If the stop bit is one, the CSTOPB is cleared, and if the stop bit is two, the CSTOPB is activated. The following are code that stops at one and two bits, respectively
New_cfg.c_cflag &= ~CSTOPB; Set the stop bit to a bit
New_cfg.c_cflag |= CSTOPB; Set the stop bit to two bits

7. Set the minimum character and wait time:
In cases where there is no special requirement on the on-off character and character time, it can be set to 0, in any case the read () function returns immediately:
New_cfg.c_cc[vtime] = 0;
New_cfg.c_cc[vmin] = 0;

8. Clear the serial buffer:
int tcdrain (int fd); Block the program until the output buffer data is completely sent
int Tcflow (int fd,int action); Used to pause or restart output
int tcflush (int fd,int queue_selectot); For emptying input, output buffers

9. Activating the configuration
tcsetattr (int fd, int optional_actions, const struct Termios *termios_p);

The function calls are as follows:
if ((Tcsetattr (fd,tcsanow,&new_cfg))! = 0)
{
Perror ("tcsetattr");
return-1;
}

*************************************************************************************************************** *******************************************
*************************************************************************************************************** *******************************************
*************************************************************************************************************** *******************************************

Poll function and serial port settings. DOC

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.