六.串口測(cè)試代碼我們已經(jīng)配置了mini2440的串口配置,然后根據(jù)mini2440開(kāi)發(fā)板的硬件電路知道S3C2440本身總共有3個(gè)串口:UART0、1、2,其中UART0,1可組合為一個(gè)全功能的串口,在大部分的應(yīng)用中,我們只用到3個(gè)簡(jiǎn)單的串口功能(本開(kāi)發(fā)板提供的Linux和WinCE驅(qū)動(dòng)也是這樣設(shè)置的),即通常所說(shuō)的發(fā)送(TXD)和接收(RXD),它們分別對(duì)應(yīng)板上的CON1、CON2、CON3,這3個(gè)接口都是從CPU直接引出的,是TTL電平。為了方便用戶使用,其中UART0做了RS232電平轉(zhuǎn)換,它們對(duì)應(yīng)于COM0,可以通過(guò)附帶的直連線與PC機(jī)互相通訊。我們這個(gè)實(shí)驗(yàn)選用CON1作為測(cè)試串口的端口。用導(dǎo)線將CON1的1號(hào)(TXD1)和2號(hào)(RXD1)引腳相連,實(shí)現(xiàn)自發(fā)自收。
本文引用地址:http://cafeforensic.com/article/201611/319915.htm
實(shí)驗(yàn)環(huán)境:內(nèi)核linux2.6.32.2,arm-linux-gcc交叉編譯器,mini2440開(kāi)發(fā)板
內(nèi)核配置:選中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。
測(cè)試代碼如下:
#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); //獲取線路設(shè)置
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
if (speed == name_arr[i]) {
tcflush(fd, TCIOFLUSH); //刷新輸入輸出隊(duì)列
cfsetispeed(&Opt, speed_arr[i]); //設(shè)置輸入波特率
cfsetospeed(&Opt, speed_arr[i]); //設(shè)置輸出波特率
status = tcsetattr(fd, TCSANOW, &Opt); //設(shè)置線路設(shè)置
if (status != 0)
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH); //刷新輸入輸出隊(duì)列
}
}
int set_Parity(int fd,int databits,int stopbits,int parity, int flowctrl)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0) { //獲取線路設(shè)置
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 sizen");
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 parityn");
return (FALSE);
}
switch (stopbits) {
case 1:
options.c_cflag &= ~CSTOPB; //相應(yīng)位置0
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bitsn");
return (FALSE);
}
if (flowctrl)
options.c_cflag |= CRTSCTS;
else
options.c_cflag &= ~CRTSCTS;
if (parity != n)
options.c_iflag |= INPCK;
// VTIME設(shè)定字節(jié)輸入時(shí)間計(jì)時(shí)器
options.c_cc[VTIME] = 150; // 15 seconds
//VMIN設(shè)定滿足讀取功能的最低字節(jié)個(gè)數(shù)
options.c_cc[VMIN] = 0;
options.c_lflag &= ~(ECHO | ICANON);
tcflush(fd,TCIFLUSH); //刷新輸入隊(duì)列
if (tcsetattr(fd,TCSANOW,&options) != 0) { //設(shè)置線路設(shè)置
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
int OpenDev(char *Dev) //打開(kāi)串口
{
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: %sn", device, strerror(errno));
exit(1);
}
if (set_Parity(fd,8,1,N,flowctrl)== FALSE) {
fprintf(stderr, "Set Parity Errorn");
close(fd);
exit(1);
}
pid = fork();
if (pid < 0) {
fprintf(stderr, "Error in fork!n");
} else if (pid == 0){
while(1) {
printf("SEND: %sn",xmit);
write(fd, xmit, strlen(xmit));
sleep(1);
}
exit(0);
} else {
while(1) {
if (nread > 0) {
buff[nread] =