A serial port read operation question, please answer!
Source: Internet
Author: User
A serial port read operation question, please answer! -- Linux general technology-Linux technology and application information. For details, refer to the following section. This is a window configuration, and then open the program for reading through the serial port. When I compile the makefile file (make-f port_makefile), the following error occurs:
Evan @ evan-desktop :~ /Example2 $ make-f port_makefile
Gcc-c open_port.c-o open_port.o
Open_port.c: In function 'open _ port ':
Open_port.c: 9: error: 'O _ rdwr' undeclared (first use in this function)
Open_port.c: 9: error: (Each undeclared identifier is reported only once
Open_port.c: 9: error: for each function it appears in .)
Open_port.c: 9: error: 'O _ NiOCTTY 'undeclared (first use in this function)
Open_port.c: 9: error: 'O _ ndelay' undeclared (first use in this function)
Open_port.c: 15: warning: incompatible implicit declaration of built-in function 'printf'
Open_port.c: 19: error: 'O _ NOCTTY 'undeclared (first use in this function)
Open_port.c: 38: error: 'f _ SETFL 'undeclared (first use in this function)
Open_port.c: 39: warning: incompatible implicit declaration of built-in function 'printf'
Open_port.c: 43: error: 'stdin _ FILENO 'undeclared (first use in this function)
Make: *** [open_port.o] Error 1
Evan @ evan-desktop :~ /Example2 $
I checked the open_port.c file repeatedly. At the beginning, I thought it was missing or didn't add any header file, but I checked it. I checked every program and maybe I checked it horizontally, I cannot see any problem, or it may be that the port_makefile file is wrong. I am very grateful to the expert for his advice.
Several programs are as follows:
/* File name set_opt.c, function: Serial Port Configuration Setting */
Int set_opt (int fd, int nspeed, int nbits, char nevent, int nstop)
{
Struct termios newtio, oldtio;
/* Save the existing serial port parameter settings for testing. If the serial port number or other errors occur, related information will be provided */
If (tcgetattr (fd, & oldtio )! = 0)
{
Perror ("setupserial 1 ");
Return-1;
}
Bzero (& newtio, sizeof (newtio); // clear all struct Parameters
/* Step 1: Set the character size */
Newtio. c_cflag | = CLOCAL | CREAD; // enable local connection and accept
Newtio. c_cflag & = ~ CSIZE; // remove the bitmask from the data bit and then reset it.
/* Set the data bit */
Switch (nbits)
{
Case 7:
Newtio. c_cflag | = CS7;
Break;
Case 8:
Newtio. c_cflag | = CS8;
Break;
}
/* Set the parity bit */
Case 460800:
Cfsetispeed (& newtio, B460800 );
Cfsetospeed (& newtio, B460800 );
Break;
Default:
Cfsetispeed (& newtio, B9600 );
Cfsetospeed (& newtio, B9600 );
Break;
}
/* Set the stop bit */
Switch (nstop)
{
Case 1:
Newtio. c_cflag & = ~ CSTOPB;
Break;
Case 0:
Newtio. c_cflag | = CSTOPB;
Break;
}
/* Set the wait time and minimum accepted characters */
Newtio. c_cc [VTIME] = 0;
Newtio. c_cc [VMINJ] = 0;
/* Process unreceived characters */
Tcflush (fd, TCIFLUSH );
/* Activate new configuration */
If (tcsetattr (fd, TCSANOW, & newtio ))! = 0)
{
Perror ("com set error ");
Return-1;
}
Printf ("set done! \ N ");
Return 0;
}
/* File name: open_port.c. function: Open the serial port */
// # Include
Int open_port (int fd, int comport)
{
Char * dev [] = {"/dev/ttys0", "/dev/ttys1", "/dev/ttys2 "};
Switch (comport)
{
Case 1:
If (fd = open ("/dev/ttys0", O_RDWR | O_NiOCTTY | O_NDELAY) <0)
{
Perror ("can not open serial port ");
Return (-1 );
}
Else
Printf ("open/dev/ttys0 sucess \ n ");
//}
Case 2:
If (fd = open ("/dev/ttys1", O_RDWR | O_NOCTTY | O_NDELAY) <0)
{
Perror ("can not open serial port ");
Return (-1 );
}
Else
Printf ("open/dev/ttys1 sucess \ n ");
//}
Case 3:
If (fd = open ("/dev/ttys2", O_RDWR | O_NOCTTY | O_NDELAY) <0)
{
Perror ("can not open serial port ");
Return (-1 );
}
Else
Printf ("open/dev/ttys2 sucess \ n ");
}
/* Restore the serial port to the blocking status */
If (fcntl (fd, F_SETFL, 0) <0)
Printf ("fcntl failde! \ N ");
Else
Printf ("fcntl = % d \ n", fcntl (fd, F_SETFL, 0 ));
/* Test whether the device is a terminal device */
If (isatty (STDIN_FILENO) = 0)
Printf ("standard input is not a terminal device \ n ");
Else
Printf ("isatty sucess! \ N ");
Printf ("fd-open = % d \ n", fd );
Return fd;
}
/* File name: port. c, header function file */
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
/* File name port_main.c */
# Include
/* Read serial port */
Int main ()
{
Int fd;
Int nread, I;
Char buff [] = "hello \ n ";
If (fd = open_port (fd, 1) <0)
{
Perror ("open_port error ");
Return;
}
If (I = set_port (fd, 115200,8, 'n', 1) <0)
{
Perror ("set_port error ");
Return;
}
Printf ("fd = % d \ n", fd );
Fd = 3;
Nread = read (fd, buff, 8 );
Printf ("nread-% d, % s \ n", nread, buff );
Close (fd );
Return 0;
}
/* Port_makefike file */
Port: open_port.o set_opt.o main. o
Gcc-o port main. o set_opt.o open_port.o
Main. o: port_main.c port. h
Gcc-c main. c-o main. o
Open_port.o: open_port.c port. h
Gcc-c open_port.c-o open_port.o
Set_opt.o: set_opt.c port. h
Gcc-c set_opt.c-o set_opt.o
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