int open_port(int fd,int comport)
{
//char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
long vdisable;//没用
//打开串口
if (comport==1)
{
//fd = open("/dev/ttySAC0",O_RDWR|O_NOCTTY|O_NDELAY);
fd = open("/dev/s3c2410_serial0",O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd)
{
perror("Can't Open s3c2410_serial0");
return(-1);
}
else
printf("open s3c2410_serial0 .....\n");
}
else if(comport==2)
{
fd = open("/dev/s3c2410_serial1",O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd)
{
perror("Can't Open s3c2410_serial1");
return(-1);
}
else
{
printf("open s3c2410_serial1 .....\n");
}
}
else if (comport==3)
{
fd = open("/dev/s3c2410_serial2",O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd)
{
perror("Can't Open s3c2410_serial2");
return(-1);
}
else
{
printf("open s3c2410_serial2 .....\n");
}
}
else if (comport==4)
{
fd = open("/dev/s3c2410_serial3",O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd)
{
perror("Can't Open s3c2410_serial3");
return(-1);
}
else
printf("open s3c2410_serial3 .....\n");
}
//恢复串口的状态为阻塞状态,用于等待串口数据的读入
if(fcntl(fd, F_SETFL, 0) < 0)
printf("fcntl failed!\n");
else
printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));
//测试打开的文件描述符是否引用一个终端设备,以进一步确认串口是否正确打开
if(isatty(STDIN_FILENO)==0)
printf("standard input is not a terminal device\n");
else
printf("isatty success!\n");
printf("fd-open=%d\n",fd);
return fd;
}
unsigned int val=0;
int main(int argc, char **argv)
{
long ret=0;
int i;
int fd,fdd;
unsigned char buff[512];
bzero(buff, 512);
//串口4
if((fd=open_port(fd,4)) < 0)//打开串口 2
{
printf("open_port error3\n");
return -1;
}
if((i=set_opt(fd,115200,8,'N',1)) < 0)//设置串口 9600 8 N 1
{
printf("set_opt error2\n");
return -1;
}
printf("fd=%d\n",fd);
strcpy(buff,"HelloWorld");
while (1)
{
write(fd,buff,sizeof(buff));//写数据
sleep(1);
}
close(fd);
return 0;
}
2
#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>
#define DEVICE "/dev/s3c2410_serial3"
int serial_fd = 0;
//打开串口并初始化设置
init_serial(void)
{
serial_fd = open(DEVICE, O_RDWR | O_NOCTTY | O_NDELAY);
if (serial_fd < 0) {
perror("open");
return -1;
}
//串口主要设置结构体termios <termios.h>
struct termios options;
/**1. tcgetattr函数用于获取与终端相关的参数。
*参数fd为终端的文件描述符,返回的结果保存在termios结构体中
*/
tcgetattr(serial_fd, &options);
/**2. 修改所获得的参数*/
options.c_cflag |= (CLOCAL | CREAD);//设置控制模式状态,本地连接,接收使能
options.c_cflag &= ~CSIZE;//字符长度,设置数据位之前一定要屏掉这个位
options.c_cflag &= ~CRTSCTS;//无硬件流控
options.c_cflag |= CS8;//8位数据长度
options.c_cflag &= ~CSTOPB;//1位停止位
options.c_iflag |= IGNPAR;//无奇偶检验位
options.c_oflag = 0; //输出模式
options.c_lflag = 0; //不激活终端模式
cfsetospeed(&options, B115200);//设置波特率
/**3. 设置新属性,TCSANOW:所有改变立即生效*/
tcflush(serial_fd, TCIFLUSH);//溢出数据可以接收,但不读
tcsetattr(serial_fd, TCSANOW, &options);
return 0;
}
/**
*串口发送数据
*@fd:串口描述符
*@data:待发送数据
*@datalen:数据长度
*/
int uart_send(int fd, char *data, int datalen)
{
int len = 0;
len = write(fd, data, datalen);//实际写入的长度
if(len == datalen) {
return len;
} else {
tcflush(fd, TCOFLUSH);//TCOFLUSH刷新写入的数据但不传送
return -1;
}
return 0;
}
/**
*串口接收数据
*要求启动后,在pc端发送ascii文件
*/
int uart_recv(int fd, char *data, int datalen)
{
int len=0, ret = 0;
fd_set fs_read;
struct timeval tv_timeout;
FD_ZERO(&fs_read);
FD_SET(fd, &fs_read);
tv_timeout.tv_sec = (10*20/115200+2);
tv_timeout.tv_usec = 0;
ret = select(fd+1, &fs_read, NULL, NULL, &tv_timeout);
printf("ret = %d\n", ret);
//如果返回0,代表在描述符状态改变前已超过timeout时间,错误返回-1
if (FD_ISSET(fd, &fs_read)) {
len = read(fd, data, datalen);
printf("len = %d\n", len);
return len;
} else {
perror("select");
return -1;
}
return 0;
}
int main(int argc, char **argv)
{
init_serial();
char buf[]="hello world";
char buf1[10];
uart_send(serial_fd, buf, 10);
printf("\n");
uart_recv(serial_fd, buf1, 10);
printf("uart receive %s\n", buf1);
close(serial_fd);
return 0;
}