分类: C/C++
2012-03-22 21:07:45
下面是mini2440的发送程序,
arm-linux-gcc -o send send.c
起初用串口线跟mini2440的232接口想连接,但是不理想,可能是
mini2440的232转换芯片不好使,反正怎么都调试不通过。看看原理
图,发现有个con1,就是对应着串口1的cpu的ttl引脚,连上就ok了。
send.c-------------------------------------
#include
#include
#include
#include
#include
#include
#include
#define BAUDRATE B115200
#define DEV "/dev/tts/0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
volatile int STOP=FALSE;
main() {
int fd,c, res;
struct termios oldtio,newtio;
char buf[255];
fd = init_dev(&oldtio);
while (STOP==FALSE) {
send_cmd(fd,"hello");putchar(0x0d);
usleep(500000);
}
tcsetattr(fd,TCSANOW,&oldtio);
}
/* write the users command out the serial port */
int send_cmd(int ftty,char * str)
{
int result;
result = write(ftty,str,strlen(str));/*argv[4], strlen(argv[4]));*/
if (result < 0)
{
printf("Write command:%s to serial failed\n",str);
close(ftty);
exit(1);
}
}
int init_dev(struct termios *oldtio)
{
int fd,status;
unsigned char cmd[17];
struct termios newtio;
//open carmo device
fd = open(DEV, O_RDWR| O_NOCTTY|O_NDELAY);
if (fd ==-1)
{
perror("open"DEV);
return -1;
}
else //set to block;
fcntl(fd, F_SETFL,0);
printf("open serial ok\n");
tcgetattr(fd,oldtio); //save current serial port settings
bzero(&newtio, sizeof(newtio)); // clear struct for new port settings
//configure the serial port;
cfsetispeed(&newtio,B115200);
cfsetospeed(&newtio,B115200);
newtio.c_cflag |=CLOCAL|CREAD;
/*8N1*/
newtio.c_cflag &= ~CSIZE; /* Mask the character size bits */
newtio.c_cflag |= CS8; /* Select 8 data bits */
newtio.c_cflag &= ~PARENB;
newtio.c_cflag &= ~CSTOPB;
newtio.c_cflag &= ~CRTSCTS;//disable hardware flow control;
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);/*raw input*/
newtio.c_oflag &= ~OPOST; /*raw output*/
tcflush(fd,TCIFLUSH);//clear input buffer
newtio.c_cc[VTIME] = 100; /* inter-character timer unused */
newtio.c_cc[VMIN] = 0; /* blocking read until 0 character arrives */
tcsetattr(fd,TCSANOW,&newtio);
return fd;
}
接收串口代码,在pc上使用。
rsv.c-------------------------------
#include
#include
#include
#include
#include
#include
#include
#define BAUDRATE B115200
#define DEV "/dev/ttyS0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1
volatile int STOP=FALSE;
main() {
int fd,c, res;
char buf[255];
fd = open(DEV, O_RDWR| O_NOCTTY|O_NDELAY);
if (fd ==-1)
{
perror("open"DEV);
return -1;
}
else //set to block;
fcntl(fd, F_SETFL,0);
printf("open serial ok\n");
if(cfg_serial(fd,8,1,'N')== FALSE)
{
printf("cfg serial Error\n");
exit(1);
}
printf("start recieve\n");
while (STOP==FALSE) { /* loop for input */
static counter = 0;
counter ++;
res = read(fd,buf,200); /* returns after 200 chars have been input */
buf[res]=0; /* so we can printf... */
printf("%d(%d)\t%s\n",counter,res,buf);
fflush(stdout);
if(buf[0]=='z') STOP=TRUE;
}
}
/*
*@brief 设置串口数据位,停止位和效验位
*@param fd 类型 int 打开的串口文件句柄*
*@param databits 类型 int 数据位 取值 为 7 或者8*
*@param stopbits 类型 int 停止位 取值为 1 或者2*
*@param parity 类型 int 效验类型 取值为N,E,O,S
*/
int cfg_serial(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)
{
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_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
options.c_oflag &= ~OPOST; /*Output*/
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 转换为偶效验*/
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);
}
/* 设置停止位数*/
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')&&(parity != 'N'))
options.c_iflag |= INPCK;
options.c_cc[VTIME] = 5; // 0.5 seconds
options.c_cc[VMIN] = 1;
options.c_cflag &= ~HUPCL;
options.c_iflag &= ~INPCK;
options.c_iflag |= IGNBRK;
options.c_iflag &= ~ICRNL;
options.c_iflag &= ~IXON;
options.c_lflag &= ~IEXTEN;
options.c_lflag &= ~ECHOK;
options.c_lflag &= ~ECHOCTL;
options.c_lflag &= ~ECHOKE;
options.c_oflag &= ~ONLCR;
tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial");
return (FALSE);
}
return (TRUE);
}