sasinop 发表于 2015-2-27 17:49:24

6410 串口,没字节最高位自动给过滤了



#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>

main()
{
int fd;
int i;
int len;
int n = 0;
int m=0;
unsigned char read_buf;
unsigned char write_buf;
struct termios opt;

write_buf=0xFF;
fd = open("/dev/ttySAC2", O_RDWR | O_NOCTTY); //默认为阻塞读方式
if(fd == -1)
{
perror("open serial 0\n");
exit(0);
}

tcgetattr(fd, &opt);
cfsetispeed(&opt, B9600);
cfsetospeed(&opt, B9600);

if(tcsetattr(fd, TCSANOW, &opt) != 0 )
{
perror("tcsetattr error");
return -1;
}

opt.c_cflag &= ~CSIZE;
opt.c_cflag |= CS8;
opt.c_cflag &= ~CSTOPB;
opt.c_cflag &= ~PARENB;
//opt.c_cflag &= ~INPCK;
opt.c_cflag |= (CLOCAL | CREAD);
/*
opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
opt.c_oflag &= ~OPOST;
opt.c_oflag &= ~(ONLCR | OCRNL); //添加的
//ISTRIP

sasinop 发表于 2015-2-27 17:50:28



#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>

main()
{
int fd;
int i;
int len;
int n = 0;
int m=0;
unsigned char read_buf;
unsigned char write_buf;
struct termios opt;

write_buf=0xFF;
fd = open("/dev/ttySAC2", O_RDWR | O_NOCTTY); //默认为阻塞读方式
if(fd == -1)
{
perror("open serial 0\n");
exit(0);
}

tcgetattr(fd, &opt);
cfsetispeed(&opt, B9600);
cfsetospeed(&opt, B9600);

if(tcsetattr(fd, TCSANOW, &opt) != 0 )
{
perror("tcsetattr error");
return -1;
}

opt.c_cflag &= ~CSIZE;
opt.c_cflag |= CS8;
opt.c_cflag &= ~CSTOPB;
opt.c_cflag &= ~PARENB;
//opt.c_cflag &= ~INPCK;
opt.c_cflag |= (CLOCAL | CREAD);
/*
opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
opt.c_oflag &= ~OPOST;
opt.c_oflag &= ~(ONLCR | OCRNL); //添加的
//ISTRIP
        opt.c_iflag &= ~(ICRNL | INLCR);
opt.c_iflag &= ~(IXON | IXOFF | IXANY); //添加的
*/

opt.c_cc = 0;
opt.c_cc = 0;

tcflush(fd, TCIOFLUSH);

printf("configure complete\n");

if(tcsetattr(fd, TCSANOW, &opt) != 0)
{
perror("serial error");
return -1;
}
printf("start send and receive data\n");

while(1)
{
n = 0;
len = 0;

m = write(fd, write_buf, 1);
printf("write %02X chars\n",write_buf);

while( (n = read(fd, read_buf, 1)) > 0 )
{
        printf("read len is %d \n ",n);
for(i = len; i < (len + n); i++)
{
write_buf = read_buf;
}
len += n;
printf("recive char is :%02X \n", read_buf);
}

sleep(2);
}
}
将串口扩展板,的串口2 的 2 和3 短接,自发自收即
# ./acom
configure complete
start send and receive data
write FF chars
write FF chars
read len is 1
recive char is :7F
write 7F chars
read len is 1
recive char is :7F
^C

结果就是,每个字节的最高位,自动给过滤了

代码中,变量是无符号的,,应该没啥问题了,,硬件啥有啥特别注意的?

还是其他地方有啥没注意到呢?
页: [1]
查看完整版本: 6410 串口,没字节最高位自动给过滤了