我在tiny6410 linux环境下编写了一个串口通信的程序,但是板子可以接受到数据,却发送不出去!求高人指点!源程序如下:
# include <stdio.h>
# include <stdlib.h>
# include <termio.h>
# include <unistd.h>
# include <fcntl.h>
# include <getopt.h>
# include <time.h>
# include <errno.h>
# include <string.h>
int main(int argc, char *argv[])
{
int com_fd;
struct termios opt;
const char *DeviceName = "/dev/ttySAC1";
int DeviceSpeed = B115200;
int TtySpeed = B115200;
int ByteBits = CS8;
char buff[512];
int num;
com_fd = open(DeviceName,O_RDWR,0);//打开串口设备
if(com_fd < 0)
{
fprintf(stderr,"can't open serial port!\n");
return -1;
}
//恢复串口未阻塞状态
if (fcntl(com_fd, F_SETFL, O_NONBLOCK) < 0)
{
fprintf(stderr,"fcntl failed!\n");
exit(0);
}
//初始化内存
memset(&opt,0,sizeof(struct termios));
if(tcgetattr(com_fd, &opt) < 0)//函数用于获取与终端相关的参数。参数fd为终端的文件描述符,返回的结果保存在termios结构体中
{
fprintf(stderr,"Unable to get comm port\n");
return -1;
}
//tcflush(com_fd,TCIOFLUSH);
cfsetispeed(&opt,B115200);
cfsetospeed(&opt,B115200);
if (tcsetattr(com_fd, TCSANOW, &opt) < 0)
{
fprintf(stderr,"Unable to set comm port\n");
return -1;
}
//opt.c_iflag = IGNPAR;
//opt.c_oflag &= ~(OPOST);
opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
opt.c_oflag &= ~OPOST; /*Output*/
opt.c_cflag = DeviceSpeed | HUPCL | ByteBits | CREAD | CLOCAL | CRTSCTS;
opt.c_cc[VMIN] = 1;
tcflush(com_fd,TCIOFLUSH);
if (tcsetattr(com_fd, TCSANOW, &opt) < 0)
{
fprintf(stderr,"Unable to set comm port\n");
return -1;
}
fprintf(stderr,"I'm OK!\n");
write(com_fd,"hello",5);
while(1)
{
if((num = read(com_fd,buff,512)) > 0)
{
buff[num] = '\0';
//fprintf(stderr,buff);
//fprintf(stderr,"\n");
write(com_fd,buff,num);
//write(com_fd,"hello",5);
}
}
return 0;
}