六.串口测验代码
咱们现已装备了mini2440的串口装备,然后依据mini2440开发板的硬件电路知道S3C2440自身总共有3个串口:UART0、1、2,其间UART0,1可组合为一个全功用的串口,在大部分的运用中,咱们只用到3个简略的串口功用(本开发板供给的Linux和WinCE驱动也是这样设置的),即一般所说的发送(TXD)和接纳(RXD),它们别离对应板上的CON1、CON2、CON3,这3个接口都是从CPU直接引出的,是TTL电平。为了便使用户运用,其间UART0做了RS232电平转化,它们对应于COM0,能够经过顺便的直连线与PC机相互通讯。咱们这个试验选用CON1作为测验串口的端口。用导线将CON1的1号(TXD1)和2号(RXD1)引脚相连,完成自发自收。
试验环境:内核linux2.6.32.2,arm-linux-gcc穿插编译器,mini2440开发板
内核装备:选中s3c2440.o samsung.o serial_core.o tty_io.o n_tty.o tty_ioctl.o tty_ldisc.o tty_buffer.o tty_port.o。
测验代码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define FALSE 1
#define TRUE 0
char *recchr=”We received:\””;
int speed_arr[] = {
B921600, B460800, B230400, B115200, B57600, B38400, B19200,
B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600,
B4800, B2400, B1200, B300,
};
int name_arr[] = {
921600, 460800, 230400, 115200, 57600, 38400, 19200,
9600, 4800, 2400, 1200, 300, 38400, 19200, 9600,
4800, 2400, 1200, 300,
};
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); //改写输入输出行列
}
}
int set_Parity(int fd,int databits,int stopbits,int parity, int flowctrl)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0) { //获取线路设置
perror(“SetupSerial 1”);
return(FALSE);
}
options.c_cflag &= ~CSIZE ; //使用CSIZE掩码把正确位从cflag中别离并清零,其他位不变
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;
options.c_iflag &= ~INPCK;
break;
case o:
case O:
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case e:
case E:
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case S:
case s:
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; //相应方位0
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,”Unsupported stop bits\n”);
return (FALSE);
}
if (flowctrl)
options.c_cflag |= CRTSCTS;
else
options.c_cflag &= ~CRTSCTS;
if (parity != n)
options.c_iflag |= INPCK;
// VTIME设定字节输入时刻计时器
options.c_cc[VTIME] = 150; // 15 seconds
//VMIN设定满意读取功用的最低字节个数
options.c_cc[VMIN] = 0;
options.c_lflag &= ~(ECHO | ICANON);
tcflush(fd,TCIFLUSH); //改写输入行列
if (tcsetattr(fd,TCSANOW,&options) != 0) { //设置线路设置
perror(“SetupSerial 3”);
return (FALSE);
}
return (TRUE);
}
int OpenDev(char *Dev) //翻开串口
{
int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
if (-1 == fd) {
perror(“Cant Open Serial Port”);
return -1;
} else
return fd;
}
int main(int argc, char *argv[])
{
int fd, next_option, havearg = 0;
char *device = “/dev/ttySAC1”;
int speed = 115200;
int flowctrl = 0;
int nread;
char buff[512];
pid_t pid;
char *xmit = “com test by ptr 2012”;
sleep(1);
fd = OpenDev(device);
if (fd > 0) {
set_speed(fd,speed);
} else {
fprintf(stderr, “Error opening %s: %s\n”, device, strerror(errno));
exit(1);
}
if (set_Parity(fd,8,1,N,flowctrl)== FALSE) {
fprintf(stderr, “Set Parity Error\n”);
close(fd);
exit(1);
}
pid = fork();
if (pid < 0) {
fprintf(stderr, “Error in fork!\n”);
} else if (pid == 0){
while(1) {
printf(“SEND: %s\n”,xmit);
write(fd, xmit, strlen(xmit));
sleep(1);
}
exit(0);
} else {
while(1) {
if (nread > 0) {
buff[nread] = \0;
printf(“RECV: %s\n”, buff);
}
}
}
close(fd);
exit(0);
}
测验成果:
虚拟机下编译arm-linux-gcc serial.c –o serial
在超级终端下运转./serial
能够看到:
SEND:com test by ptr 2012
RECV:com test by ptr 2012
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/fpga/262304.html