http://hi.baidu.com/weiweisuo1986/item/b33200134ceaac6871d5e81d
Before you want to do a and serial port related projects, just carefully studied the next serial port, the first is to realize the serial port and PC communication.
Serial drive generally do not need us to write, are very mature drivers, to know the point is that you develop the board serial name, such as TtySAC0, and so on. So the main work is to program, the implementation of the serial port, of course, it is also understood, the serial port as a file, read and write to it.
First of all, explain my compilation environment, Linux uses the version is 2.6.30.4, the cross compiler uses the EABI4.3.3. Here is the program code, only one file: serialport.c.
/**************serialport**************************/
/*************designed by suo*********************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
Serial Port Set function
void Settermios (struct Termios *pnewtio, unsigned short ubaudrate)
{
Bzero (pnewtio,sizeof (struct termios));
Pnewtio->c_cflag = ubaudrate| cs8| cread| clocal;
Pnewtio->c_iflag = Ignpar;
Pnewtio->c_oflag = 0;
Pnewtio->c_lflag = 0;
PNEWTIO->C_CC[VINTR] = 0;
Pnewtio->c_cc[vquit] = 0;
Pnewtio->c_cc[verase] = 0;
Pnewtio->c_cc[vkill] = 0;
Pnewtio->c_cc[veof] = 4;
Pnewtio->c_cc[vtime] = 5;
Pnewtio->c_cc[vmin] = 0;
PNEWTIO->C_CC[VSWTC] = 0;
Pnewtio->c_cc[vstart] = 0;
Pnewtio->c_cc[vstop] = 0;
PNEWTIO->C_CC[VSUSP] = 0;
Pnewtio->c_cc[veol] = 0;
Pnewtio->c_cc[vreprint] = 0;
Pnewtio->c_cc[vdiscard] = 0;
Pnewtio->c_cc[vwerase] = 0;
Pnewtio->c_cc[vlnext] = 0;
Pnewtio->c_cc[veol2] = 0;
}
int main (int argc,char **argv)
{
int FD;
int ncount,ntotal;
int i,j,m;
int readbyte = 0;
struct Termios oldtio,newtio;
Char *dev = "/dev/tq2440_serial1";
if ((argc!=3) | | (SSCANF (argv[1], "%d", &ntotal)!=1))
{
printf ("Usage:comsend count datat!\n");
return-1;
}
if (Fd=open (dev,o_rdwr| o_noctty| O_ndelay)) <0)//open serial COM2
{
printf ("Can ' t open serial port!\n");
return-1;
}
Tcgetattr (Fd,&oldtio);
Settermios (&newtio,b9600);
Tcflush (Fd,tciflush);
Tcsetattr (Fd,tcsanow,&newtio);
Send data
for (i=0;i<ntotal;i++)
{
ncount = Write (Fd,argv[2],strlen (argv[2));
printf ("Send data ok!count=%d\n", ncount);
Sleep (1);
}
Receive data
for (j=0;j<20;j++)
{
ReadByte = Read (fd,buffer,512);
if (readbyte>0)
{
printf ("readlength=%d\n", readbyte);
Buffer[readbyte]= ' ";
printf ("%s\n", Buffer);
Sleep (3);
}
else printf ("Read Data Failure times=%d\n", j);
}
printf ("Receive data finished!\n");
Tcsetattr (Fd,tcsanow,&oldtio);
Close (FD);
return 0;
}
This program to achieve the function is: Run the program, input to send the number of times and to send data, to the serial 2 send data, if there is data, the program will receive the data displayed in the terminal.
The corresponding makefile are:
cross=arm-linux-
All:shuserial
Shuserial:shuserial.c
$ (CROSS) Gcc-o shuserial shuserial.c
$ (CROSS) strip shuserial
Clean
@rm-VF shuserial*.o*~
After you make the executable file SerialPort, you can debug it on the Development Board. Debugging methods: 1, the program with a U disk to the Development Board (of course, the prerequisite is the Linux system has started), my Development Board is Linux2.6.30 kernel +qtopia2.2.0. 2, connect the serial port, the development Board with serial port 2 and computer connected, PC Open HyperTerminal, set the serial COM1 properties of 9600 baud rate, 8N1, and then wait to receive. 3, open the Development Board system in the terminal terminal, input./serialport Abcdefghijk, you can run this program, the program sent to the PC serial sent 10 times "Abcdefghijk", sent to complete, if you enter the Super terminal data, The program will display the data to the terminal.
Finished, the program is so simple, only to achieve a simple transceiver function.
Use QT to implement this program later ...
Successful test procedures:
ReadUart.cpp
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
#define TRUE 1
Initialize serial port
void Settermios (struct Termios * pnewtio, int ubaudrate)
{
Bzero (pnewtio, sizeof (struct termios));
8N1
Pnewtio->c_cflag = Ubaudrate | CS8 | Cread | clocal;
Pnewtio->c_iflag = Ignpar;
Pnewtio->c_oflag = 0;
Pnewtio->c_lflag = 0; Non Icanon
PNEWTIO->C_CC[VINTR] = 0;
Pnewtio->c_cc[vquit] = 0;
Pnewtio->c_cc[verase] = 0;
Pnewtio->c_cc[vkill] = 0;
Pnewtio->c_cc[veof] = 4;
Pnewtio->c_cc[vtime] = 5;
Pnewtio->c_cc[vmin] = 0;
PNEWTIO->C_CC[VSWTC] = 0;
Pnewtio->c_cc[vstart] = 0;
Pnewtio->c_cc[vstop] = 0;
PNEWTIO->C_CC[VSUSP] = 0;
Pnewtio->c_cc[veol] = 0;
Pnewtio->c_cc[vreprint] = 0;
Pnewtio->c_cc[vdiscard] = 0;
Pnewtio->c_cc[vwerase] = 0;
Pnewtio->c_cc[vlnext] = 0;
Pnewtio->c_cc[veol2] = 0;
}
#define BUFSIZE 512
int main (int argc, char **argv)
{
int FD;
int nread;
int rlength = bufsize;
int Length = 0;
Char Buff[bufsize] = {0};
Char Cmsg[bufsize] = {0};
char *pch = buff;
int i = 0;
struct Termios oldtio, newtio;
struct Timeval TV;
Char *dev = (char *) "/DEV/TTYO1";
Char *dev = (char *) "/dev/ttys1";
Fd_set RfDs;
if (fd = open (Dev, o_rdwr | O_noctty)) <0)
{
printf ("Err:can ' t open serial port!\n");
return-1;
}
Tcgetattr (FD, &oldtio);
Settermios (&newtio, B9600);
Settermios (&newtio, B115200);
Tcflush (FD, Tciflush);
Tcsetattr (FD, Tcsanow, &newtio);
tv.tv_sec=3;
tv.tv_usec=0;
while (TRUE)
{
printf ("FD is <%d> wait...\n", FD);
Fd_zero (&rfds);
Fd_set (FD, &rfds);
if (select (1+FD, &rfds, NULL, NULL, &TV) >0)
{
printf ("wait.......\n");
if (Fd_isset (FD, &rfds))
{
printf ("fd_isset...\n");
Nread=read (FD, Buff, bufsize);
Nread=read (FD, PCH, bufsize-length);
Buff[nread]= ' ";
for (i=length;i<nread+length;i++)
{
printf ("buff[%d] = <0x%x><%c>.\n", i,buff[i],buff[i]);
printf ("%c\n", Buff[i]);
}
printf ("\ n");
printf ("nread =%d,length =%d,%s\n", nread,length, Buff);
Length + = Nread;
if (Length >= bufsize)
{
printf ("##### #Length <%d> is too long.\n", Length);
return-1;
}
printf ("Nread is%d...\n", nread);
PCH + + nread;
}
}
Else
{
memcpy (cmsg,buff,length);
printf ("Uart msg is: \ n");
printf ("%s\n", buff);
printf ("\ n");
memset (Buff, 0, bufsize);
PCH = buff;
Length = 0;
tv.tv_sec=3;
tv.tv_usec=0;
Sleep (1);
}
}
Tcsetattr (FD, Tcsanow, &oldtio);
Close (FD);
}
WriteUart.cpp
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
Serial Port Set function
void Settermios (struct Termios *pnewtio, unsigned short ubaudrate)
{
Bzero (pnewtio,sizeof (struct termios));
Pnewtio->c_cflag = ubaudrate| cs8| cread| clocal;
Pnewtio->c_iflag = Ignpar;
Pnewtio->c_oflag = 0;
Pnewtio->c_lflag = 0;
PNEWTIO->C_CC[VINTR] = 0;
Pnewtio->c_cc[vquit] = 0;
Pnewtio->c_cc[verase] = 0;
Pnewtio->c_cc[vkill] = 0;
Pnewtio->c_cc[veof] = 4;
Pnewtio->c_cc[vtime] = 5;
Pnewtio->c_cc[vmin] = 0;
PNEWTIO->C_CC[VSWTC] = 0;
Pnewtio->c_cc[vstart] = 0;
Pnewtio->c_cc[vstop] = 0;
PNEWTIO->C_CC[VSUSP] = 0;
Pnewtio->c_cc[veol] = 0;
Pnewtio->c_cc[vreprint] = 0;
Pnewtio->c_cc[vdiscard] = 0;
Pnewtio->c_cc[vwerase] = 0;
Pnewtio->c_cc[vlnext] = 0;
Pnewtio->c_cc[veol2] = 0;
}
int main (int argc,char **argv)
{
int FD;
int ncount = 0, Ulen = 0, ntotal = 0;
int i,j,m;
int readbyte = 0;
Char buffer[512];
struct Termios oldtio,newtio;
Char *dev = (char*) "/dev/ttyo1";
if ((argc!=3) | | (SSCANF (argv[1], "%d", &ntotal)!=1))
{
printf ("Usage:comsend count datat!");
return-1;
}
if (Fd=open (dev,o_rdwr| o_noctty| O_ndelay)) <0)//open serial COM2
{
printf ("Can ' t open serial port!");
return-1;
}
Tcgetattr (Fd,&oldtio);
Settermios (&newtio,b115200); B9600
Settermios (&newtio,b57600);
Tcflush (Fd,tciflush);
Tcsetattr (Fd,tcsanow,&newtio);
Send data
for (i=0;i<ntotal;i++)
{
ncount = Write (Fd,argv[2],strlen (argv[2));
Ulen = Write (fd, "\ r \ n", 2);
printf ("Send data ok!count=%d, ntotal=%d, ulen=%d.\n", Ncount, Ntotal, Ulen);
Tcflush (Fd,tciflush);
Tcsetattr (Fd,tcsanow,&newtio);
Usleep (500000);
}
/*
Receive data
for (j=0;j<20;j++)
{
ReadByte = Read (fd,buffer,512);
if (readbyte>0)
{
printf ("readlength=%d", readbyte);
Buffer[readbyte]= ' ";
printf ("%s", Buffer);
Sleep (3);
}
else printf ("Read Data Failure times=%d", j);
}
printf ("Receive data finished!");
Tcsetattr (Fd,tcsanow,&oldtio);
*/
Close (FD);
return 0;
}