yuchuan2008 Blog Access: 487773 posts number: 518 Blog points: 10011 blog level: On the user group: normal user Registration time: 2008-09-03 16:29 article category
All posts (518) Embedded system development ((+) ARM technology (reprint) (4) Bootloader (1) Linux applications (3) Linux Foundation (reprint (3) Cross-compilation (reprint) (11) Embedded C Language Foundation (10) Linux device driver ((149) Linux kernel (reprint (19) Learn && work (93) Other (2) unassigned posts (196) Article archive
2010 (42)
2009 (179)
2008 (297) My friend's recent visitors
Paulscho
But_buea
Guest
YSHISX Subscription recommended blog Linux Memory Basics · Python Strip Lstrip rstrip... JSON overview and Python-to-json ... · Build trust relationships among Linux hosts ... · Interesting bit manipulation hot words Special libevent thbaselock bugfree2 VC plug-in production • Quick Setup IP monowall link application--Serial programming 2008-12-16 14:37:20
Category: LINUX
This example is found on the Internet, and can be modified to use the actual project (given in the DVS in the actual application) application-serial Programming #include <stdio.h>/* standard input/output definition */
#include <stdlib.h>/* Standard function library definition */
#include <unistd.h>/*unix standard function definition */
#include <sys/types.h>/**/
#include <sys/stat.h>/**/
#include <fcntl.h>/* File Control Definitions */
#include <termios.h>/*ppsix Terminal control definition */
#include <errno.h>/* error Number Definition */*
* @brief Set serial communication rate
* @param FD Type int Open the file handle of the serial port
* @param speed type int serial port
* @return void
*/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,};
void Set_speed (int fd, int speed)
{
int i;
int status;
struct Termios Opt;
Tcgetattr (FD, &opt);
for (i= 0; I < sizeof (Speed_arr)/sizeof (int); i++)
{
if (speed = = Name_arr[i])
{
Tcflush (FD, Tcioflush);
Cfsetispeed (&opt, speed_arr[i]);
Cfsetospeed (&opt, speed_arr[i]);
Status = Tcsetattr (FD, Tcsanow, &opt);
if (Status! = 0)
Perror ("tcsetattr fd1");
Return
}
Tcflush (Fd,tcioflush);
}
}
/**
* @brief set serial data bit, stop bit and parity bit
* @param open serial port file handle with FD type int *
* @param databits type int data bit value is 7 or 8*
* @param stopbits type int stop bit value is 1 or
* @param parity type int validation type value is N,e,o,,s
*/
int set_parity (int fd,int databits,int stopbits,int Parity)
{
struct Termios options;
if (tcgetattr (fd,&options)! = 0)
{
Perror ("Setupserial 1");
return (FALSE);
}
Options.c_cflag &= ~csize;
Switch (databits)/* Sets the number of data bits */
{
Case 7:
Options.c_cflag |= CS7;
Break
Case 8:
Options.c_cflag |= CS8;
Break
Default
fprintf (stderr, "Unsupported data size\n");
return (FALSE);
}
Switch (parity)
{
Case ' n ':
Case ' N ':
Options.c_cflag &= ~parenb; /* Clear parity Enable */
Options.c_iflag &= ~INPCK; /* Enable parity checking */
Break
Case ' O ':
Case ' O ':
Options.c_cflag |= (parodd | PARENB); /* Set to be miraculous */
Options.c_iflag |= INPCK; /* disnable parity checking */
Break
Case ' E ':
Case ' E ':
Options.c_cflag |= Parenb; /* Enable parity */
Options.c_cflag &= ~parodd; /* Convert to parity */
Options.c_iflag |= INPCK; /* disnable parity checking */
Break
Case ' S ':
Case ' s ':/*as no parity*/
Options.c_cflag &= ~parenb;
Options.c_cflag &= ~CSTOPB;
Break
Default
fprintf (stderr, "Unsupported parity\n");
return (FALSE);
}
/* Set Stop bit */
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);
}
/* Set input parity option */
if (parity! = ' n ')
Options.c_iflag |= INPCK;
Options.c_cc[vtime] = 150; Seconds
Options.c_cc[vmin] = 0;
Tcflush (Fd,tciflush); /* Update the options and do it now */
if (tcsetattr (fd,tcsanow,&options)! = 0)
{
Perror ("Setupserial 3");
return (FALSE);
}
return (TRUE);
}
/**
* @breif Open the serial port
*/
int Opendev (char *dev)
{
int fd = open (Dev, O_RDWR); //| O_noctty | O_ndelay
if ( -1 = = FD)
{/* Set the number of data bits */
Perror ("Can ' t Open Serial Port");
return-1;
}
Else
return FD;
}
/*
* @breif Main ()
*/
int main (int argc, char **argv)
{
int FD;
int nread;
Char buff[512];
Char *dev = "/DEV/TTYS1";
FD = Opendev (dev);
if (fd>0)
Set_speed (fd,19200);
Else
{
printf ("Can ' t Open Serial port!\n");
Exit (0);
}
if (set_parity (fd,8,1, ' N ') = = FALSE)
{
printf ("Set Parity error\n");
Exit (1);
}
while (1)
{
while ((Nread = Read (fd,buff,512)) >0)
{
printf ("\nlen%d\n", nread);
buff[nread+1]= ' + ';
printf ("\n%s", buff);
}
}
Close (FD);
Exit (0);
} DVS:/*********************************************************************************
* System Header File *.h
**********************************************************************************/
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <linux/delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include "def_type.h"
#include "App.h"
#include "Comapi.h"//=======================================================================//
Function: Complete the opening of the serial device, with the underlying interactive device status
Parameter: void
Return:-1 open failed >0 open successfully
Description: Open the serial port 1
=======================================================================//int baudrate = 9600;
int databit = 8;
int stopbit = 1;
int parity = 0;
int FlowControl = 0; int opencom ()
{
Char dev[]= "/DEV/TTYAMA1";
int fdcom = open (dev, o_rdwr);