📄 s3c2410.c
字号:
/*
文件名:S3C2410串口1调试.c
编写 :bingnan
时间 :写串口操作初次便测试成功,读测试由于在PC利用串口工具发
送数据时未加'\n',直到2006-05-19日测试成功
功能 :测试S3C2410串口1的读操作和写操作,包括串口参数设置函数
说明 :串口读写操作时,读写函数在遇到'\n'时才返回,因此务必在
写串口时,在单位数据后加上回车换行符
使用 :编译命令:arm-linux-gcc comtest.c -o comtest
写操作运行:./comtest write
读操作运行:./comtest read
*/
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>#define TRUE 1#define FALSE 0
int speed_arr[] = { B921600, B460800, B230400, B115200, B57600,
B38400, B19200, B9600, B4800, B2400, B1200, B300, B110};
int name_arr[] = {921600 ,460800 ,230400 ,115200, 57600,
38400, 19200, 9600, 4800, 2400, 1200, 300, 110};
/**********************************************************************
* function : the com param set
***********************************************************************/
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);
}
}
/**********************************************************************
* stop data verify
***********************************************************************/
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)
{
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); /* ????Ч?*/
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')
options.c_iflag |= INPCK;
options.c_cc[VTIME] = 150; // 15 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);
}
int main(int argc,char *argv[])
{
int loop = 0;
char buff[1024];
int fd;
int nread;
char *dev = "/dev/ttyS1";
if(argc != 2)
{
printf("parameter error\n");
return -1;
}
fd = open(dev,O_RDWR);// | O_NONBLOCK
if(fd == -1)
{
printf("/dev/ttyS1 open error\n");
return -1;
}
printf("device is opened\n");
set_speed(fd,19200);
printf("set the device baude rate\n");
if(set_Parity(fd,8,1,'N') == FALSE)
printf("set the /dev/ttyS1 attr error\n");
if(!strcmp(argv[1],"read"))
{
printf("dev/tty1 read test\n");
while(loop < 1000)
{
loop ++;
//当读到换行符后read函数返回
if((nread = read(fd,buff,1024))>0)
{
buff[nread] = '\0';
printf("%s\n",buff);
} //usleep(100000);
}
}
else if(!strcmp(argv[1],"write"))
{
printf("dev/ttys1 write test\n");
while(loop < 1000)
{
loop ++;
sprintf(buff,"write %d times\n",loop); printf(buff);
write(fd,buff,16); usleep(100000);
}
}
else
{
printf("the second paramter is wrong\n");
} return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -