- 论坛徽章:
- 0
|
下面是关于最近自己的一个串口编程程序:
/************************************
version:0.1
date :12/01/2010
author :yangchar
************************************/
#include stdio.h> /* Standard input/output definitions */
#include stdlib.h> /* Standard lib */
#include string.h> /* String function definitions */
#include unistd.h> /* UNIX standard function definitions */
#include fcntl.h> /* File control definitions */
#include errno.h> /* Error number definitions */
#include termios.h> /* POSIX terminal control definitions */
#include sys/types.h>
#include sys/select.h>
#include sys/stat.h>
#define DEBUG 1
int open_port(const char *dev_name)
{
int fd,val; /* File descriptor for the port */
fd = open(dev_name, O_RDWR | O_NOCTTY | O_NDELAY);
if (-1 == fd)
{
perror("open_port: Unable to open tty " );
}
else
{
if(DEBUG)
printf("The %s is opened \n",dev_name);
}
if( (val=fcntl(fd, F_SETFL, 0)) 0)
perror("fcntl failed");
else
{
if(DEBUG)
printf("The fcntl is %d \n",val);
}
if ( isatty(fd) == 0 )
perror("This is not a tty device ");
else
{
if(DEBUG)
printf("isatty success! \n");
}
return (fd);
}
int set_port (int fd , int nSpeed , int nBits , char nEvent , int nStop )
{
struct termios new_ios,old_ios;
if ( tcgetattr ( fd , &old_ios ) !=0 )
perror("Save the terminal error");
else
{
if(DEBUG)
printf("Save the terminal success \n");
}
bzero( &new_ios , sizeof( struct termios ));
new_ios=old_ios;
new_ios.c_cflag |= CLOCAL |CREAD ;
new_ios.c_cflag &= ~CSIZE ;
switch (nBits)
{
case 5:
new_ios.c_cflag |=CS5 ;
break ;
case 6:
new_ios.c_cflag |=CS6 ;
break ;
case 7:
new_ios.c_cflag |=CS7 ;
break ;
case 8:
new_ios.c_cflag |=CS8 ;
break ;
default:
perror("Wrong nBits");
break ;
}
switch (nSpeed )
{
case 2400:
cfsetispeed(&new_ios , B2400);
cfsetospeed(&new_ios , B2400);
break;
case 4800:
cfsetispeed(&new_ios , B4800);
cfsetospeed(&new_ios , B4800);
break;
case 9600:
cfsetispeed(&new_ios , B9600);
cfsetospeed(&new_ios , B9600);
break;
case 19200:
cfsetispeed(&new_ios , B19200);
cfsetospeed(&new_ios , B19200);
break;
case 115200:
cfsetispeed(&new_ios , B115200);
cfsetospeed(&new_ios , B115200);
break;
case 460800:
cfsetispeed(&new_ios , B460800);
cfsetospeed(&new_ios , B460800);
break;
default:
perror("Wrong nSpeed");
break;
}
switch (nEvent )
{
case 'o':
case 'O':
new_ios.c_cflag |= PARENB ;
new_ios.c_cflag |= PARODD ;
new_ios.c_iflag |= (INPCK | ISTRIP);
break ;
case 'e':
case 'E':
new_ios.c_iflag |= (INPCK | ISTRIP);
new_ios.c_cflag |= PARENB ;
new_ios.c_cflag &= ~PARODD ;
break ;
case 'n':
case 'N':
new_ios.c_cflag &= ~PARENB ;
new_ios.c_iflag &= ~INPCK ;
break ;
default:
("Wrong nEvent");
break ;
}
if ( nStop == 1 )
new_ios.c_cflag &= ~CSTOPB ;
else if ( nStop == 2 )
new_ios.c_cflag |= CSTOPB ;
/*No hardware control*/
new_ios.c_cflag &= ~CRTSCTS;
new_ios.c_cc[VTIME] = 0 ;
new_ios.c_cc[VMIN] = 0 ;
/*row model*/
new_ios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
new_ios.c_oflag &= ~OPOST;
tcflush(fd,TCIFLUSH) ;
if (tcsetattr(fd,TCSANOW,&new_ios) != 0 )
{
perror("Set the terminal error");
tcsetattr(fd,TCSANOW,&old_ios);
return -1 ;
}
else
{
if(DEBUG)
printf("Set the terminal success \n");
}
return 0;
}
int main (int argc , char *argv[])
{
int fd ;
int nwrite,nread;
int length;
char buff[10];
unsigned char data_pan_left[7] ={0xa0,0x01,0x02,0x00,0x00,0x00,0x00};
unsigned char data_pan_stop[7] ={0xa0,0x01,0x0f,0x00,0x00,0x00,0x00};
unsigned char data_pan_right[7]={0xa0,0x01,0x01,0x00,0x00,0x00,0x00};
/*
char buff[] ="0123456789";
length = strlen(buff);
*/
char device_name[] = "/dev/ttyS1";
if((fd=open_port(device_name)) 0)
{
perror("Open the port error");
exit(1);
}
if (set_port(fd ,19200,8,'N',1) 0 )
{
perror("Set the port error");
exit(1);
}
/*
if( (nwrite = write(fd,buff , length ))
nwrite=write(fd,data_pan_left,7);
printf("write %d bytes\n",nwrite);
sleep(10);
nwrite=write(fd,data_pan_stop,7);
printf("write %d bytes\n",nwrite);
sleep(10);
nwrite=write(fd,data_pan_right,7);
printf("write %d bytes\n",nwrite);
sleep(10);
nwrite=write(fd,data_pan_stop,7);
printf("write %d bytes\n",nwrite);
/* while((nread=read(fd,buff,2))
close (fd );
return 0 ;
}
/*Note:I write the the CSTOPB to CSTOP,so the data is error*/
更多细节请看:http://digilander.libero.it/robang/rubrica/serial.htm#config
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/92500/showart_2159925.html |
|