1.我使用I.MX UART0,1编程,程序如下:
#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>
#include <limits.h>
#include <asm/ioctls.h>
#include <time.h>
#include <pthread.h>
#define DATA_LEN 0xFF /* test data's len */
//#define DEBUG 1
/*********************************************************************************************************
** Function name: openSerial
** Descriptions: open serial port at raw mod
** input paramters: iNum serial port which can be value at: 1, 2, 3, 4
** output paramters: NONE
** Return value: file descriptor
** Create by: zhuguojun
** Create Data: 2008-05-19
**--------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**--------------------------------------------------------------------------------------------------------
*********************************************************************************************************/
static int openSerial(char *cSerialName)
{
int iFd;
struct termios opt;
iFd = open(cSerialName, O_RDWR | O_NOCTTY);
if(iFd < 0) {
perror(cSerialName);
return -1;
}
tcgetattr(iFd, &opt);
//cfsetispeed(&opt, B57600);
//cfsetospeed(&opt, B57600);
cfsetispeed(&opt, B2400);
cfsetospeed(&opt, B2400);
/*
* raw mode
*/
opt.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
opt.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
opt.c_oflag &= ~(OPOST);
opt.c_cflag &= ~(CSIZE | PARENB);
opt.c_cflag |= PARENB;
opt.c_cflag &= ~PARODD;
opt.c_cflag |= CS8;
opt.c_iflag |= (INPCK | ISTRIP);
/*
* 'DATA_LEN' bytes can be read by serial
*/
opt.c_cc[VMIN] = DATA_LEN;
opt.c_cc[VTIME] = 150;
if (tcsetattr(iFd, TCSANOW, &opt)<0) {
return -1;
}
return iFd;
}
int main(void)
{
char tmp[1024];
int len;
int fd, i;
char sendbuf[]={0xFE,0xFE,0xFE,0x68,0x46,0x53,0x00,0x13,0x27,0x00,0x00,0x00,0x01,0x03,0x90,0x1F};
int lenbuf = sizeof(sendbuf);
fd = openSerial("/dev/ttySP4");
printf("serial test111!\n");
for(i = 0; i < 16; i++)
tmp[i] = i%0xFF;
//write(fd, tmp, 16);
write(fd,sendbuf,lenbuf);
while (1) {
len = read(fd, tmp, 0x01);
for(i = 0; i < len; i++)
printf(" %x", tmp[i]);
printf("\n");
}
}
然后外部RX和TX短接,发现上来的数据高位都变成了0,也就是接收本来正确的应该是0xfE,现在变成了0x7e,有时候还会出现
mxs-auart mxs-auart.1: Unhandled status 500400,
请问是啥问题 |