易网时代-编程资源站
Welcome
微信登录
编程资源
图片资源库
蚂蚁家优选
PDF转换器
软件资源
软件开发
、
小程序制作
、
系统集成与运维
、
空间租用
、
硬件开发
、
视频监控
、
技术咨询与支持
——联系电话:0311-88999002/88999003
首页
/
操作系统
/
Linux
/
AT91SAM9260串口编程
AT91SAM9260串口编程
/*************************************************
* File name : 9260-uart-v1.0.c
* Description :
* Author : sg131971@qq.com
* Version : V1.0
* Date :
* Compiler : arm-linux-gcc-4.4.3
* Target : 9260EK(Linux-2.6.30)
* History :
* <author> <time> <version > <desc>
*************************************************/
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<errno.h>
#include<termios.h>
/*************************************************
* Function :
* Description : 注意/dev/ttyAT1~6对应于电路图上的0~5
* Calls :
* Called By :
* Input : int comport = 0~6
* Output :
* Return :
*************************************************/
int
open_port(
int
fd,
int
comport)
{
if
(comport == 0)
{
fd = open(
"/dev/ttyAT0"
, O_RDWR | O_NOCTTY | O_NDELAY);
if
(-1 == fd)
{
perror(
"can"t open serial port"
);
return
(-1);
}
else
printf(
"open ttyAT0 "
);
}
if
(comport == 1)
{
fd = open(
"/dev/ttyAT1"
, O_RDWR | O_NOCTTY | O_NDELAY);
if
(-1 == fd)
{
perror(
"can"t open serial port"
);
return
(-1);
}
else
printf(
"open ttyAT1 "
);
}
if
(comport == 2)
{
fd = open(
"/dev/ttyAT2"
, O_RDWR | O_NOCTTY | O_NDELAY);
if
(-1 == fd)
{
perror(
"can"t open serial port"
);
return
(-1);
}
else
printf(
"open ttyAT2 "
);
}
if
(comport == 3)
{
fd = open(
"/dev/ttyAT3"
, O_RDWR | O_NOCTTY | O_NDELAY);
if
(-1 == fd)
{
perror(
"can"t open serial port"
);
return
(-1);
}
else
printf(
"open ttyAT3 "
);
}
if
(comport == 4)
{
fd = open(
"/dev/ttyAT4"
, O_RDWR | O_NOCTTY | O_NDELAY);
if
(-1 == fd)
{
perror(
"can"t open serial port"
);
return
(-1);
}
else
printf(
"open ttyAT4 "
);
}
if
(comport == 5)
{
fd = open(
"/dev/ttyAT5"
, O_RDWR | O_NOCTTY | O_NDELAY);
if
(-1 == fd)
{
perror(
"can"t open serial port"
);
return
(-1);
}
else
printf(
"open ttyAT5 "
);
}
if
(comport == 6)
{
fd = open(
"/dev/ttyAT6"
, O_RDWR | O_NOCTTY | O_NDELAY);
if
(-1 == fd)
{
perror(
"can"t open serial port"
);
return
(-1);
}
else
printf(
"open ttyAT6 "
);
}
if
(fcntl(fd, F_SETFL, 0) < 0)
printf(
"fcnt failed! "
);
else
printf(
"fcntl=%d "
, fcntl(fd, F_SETFL, 0));
if
(isatty(STDIN_FILENO) == 0)
printf(
"standard input is not a terminal device "
);
else
printf(
"isatty success! "
);
return
fd;
}
/*************************************************
* Function :
* Description :
* Calls :
* Called By :
* Input :
* Output :
* Return :
*************************************************/
int
set_opt(
int
fd,
int
nSpeed,
int
nBits,
int
nFctl,
char
nEvent,
int
nStop)
{
struct
termios newtio, oldtio;
if
(tcgetattr(fd, &oldtio) != 0)
{
perror(
"SetupSerial 1"
);
exit(1);
}
bzero(&newtio,
sizeof
(newtio));
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;
switch
(nBits)
{
case
7:
newtio.c_cflag |= CS7;
break
;
case
8:
newtio.c_cflag |= CS8;
break
;
}
switch
(nFctl)
{
case
0:
newtio.c_cflag &= ~CRTSCTS;
break
;
case
1:
newtio.c_cflag |= CRTSCTS;
break
;
case
2:
newtio.c_cflag |= IXON | IXOFF | IXANY;
break
;
}
switch
(nEvent)
{
case
"O"
:
newtio.c_cflag |= PARENB;
//enble
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break
;
case
"E"
:
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break
;
case
"N"
:
newtio.c_cflag &= ~PARENB;
break
;
}
switch
(nSpeed)
{
case
2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break
;
case
4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break
;
case
9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break
;
case
115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break
;
default
:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break
;
}
if
(nStop == 1)
newtio.c_cflag &= ~CSTOPB;
else
if
(nStop == 2)
newtio.c_cflag |= CSTOPB;
/* 修改此处参数,以保证正常接收 */
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
newtio.c_oflag &= ~OPOST;
tcflush(fd, TCIFLUSH);
if
((tcsetattr(fd, TCSANOW, &newtio)) != 0)
{
perror(
"com set error"
);
return
-1;
}
printf(
"set done! "
);
return
0;
}
/*************************************************
* Function :
* Description : 注意/dev/ttyAT1~6对应于电路图上的0~5
* Calls :
* Called By :
* Input :
* Output :
* Return :
*************************************************/
int
main(
void
)
{
int
fd = -1;
int
i, nread;
char
buff[64];
if
((fd = open_port(fd, 5)) < 0)
{
perror(
"open_port erro"
);
exit(1);
}
if
((i = set_opt(fd, 115200, 8, 0,
"N"
, 1)) < 0)
{
perror(
"set_opt error"
);
return
1;
}
printf(
"ID IS: %d"
, fd);
while
(1)
{
memset(buff, 0,
sizeof
(buff));
while
((nread = read(fd, buff, 64)) > 0)
{
write(fd,
"Send Over!"
, 10);
printf(
" %s "
, buff);
}
}
if
(close(fd) < 0)
{
perror(
"close:"
);
exit(1);
}
else
printf(
"close ok"
);
exit(0);
}
收藏该网址
版权所有©石家庄振强科技有限公司2024
冀ICP备08103738号-5
网站地图