liunx 串口通信问题,跪求各位大神解答

#include <stdio.h>/*标准输入输出定义*/
#include <stdlib.h>/*标准函数库定义*/
#include <unistd.h>/*UNIX标准函数定义*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>/*文件控制定义*/
#include <termios.h>/*PPSIX 终端控制定义*/
#include <errno.h>/*错误号定义*/
#include <string.h>

#define STTY_DEV "/dev/ttySAC2"
//#define STTY_DEV "/dev/ttyS0"
#define BUFF_SIZE 512

int main()
{
int stty_fd, n;
char buffer[BUFF_SIZE];
struct termios opt;
printf("---->> input test 1 !\n\n");

/*打开串口设备*/
stty_fd = open(STTY_DEV, O_RDWR|O_NOCTTY|O_NDELAY);
if(stty_fd == -1)
{
perror("open device error");
return 0;
}
fcntl(stty_fd, F_SETFL, 0);
//printf("Open device success, wating user input ...\n");
printf("---->> input test 2 !\n\n");
/*取得当前串口配置*/
tcgetattr(stty_fd, &opt);
tcflush(stty_fd, TCIOFLUSH);
printf("---->> input test 3 !\n\n");
/*设置波特率 - 19200*/
cfsetispeed(&opt, B19200);
cfsetospeed(&opt, B19200);
printf("---->> input test 4 !\n\n");
/*设置数据位 - 8位数据位*/
opt.c_cflag &= ~CSIZE;
opt.c_cflag |= CS8;

/*设置奇偶位 - 无奇偶校验*/
opt.c_cflag &= ~PARENB;
opt.c_iflag &= ~INPCK;

/*设置停止位 - 1位停止位*/
opt.c_cflag &= ~CSTOPB;

/*设置超时时间 - 15秒*/
opt.c_cc[VTIME] = 150;
opt.c_cc[VMIN] = 0;
printf("---->> input test 5 !\n\n");
/*设置写入设备*/
if(0 != tcsetattr(stty_fd, TCSANOW, &opt))
{
perror("set baudrate");
return 0;
}
tcflush(stty_fd, TCIOFLUSH);

memset(buffer, 0, BUFF_SIZE);
    n = write(stty_fd, "hello world",11);
/*读取数据,直到接收到‘quit’字符串退出*/
/*while(1)
{*/
printf("----->> input test before!\n\n");
n =  read(stty_fd, buffer, BUFF_SIZE);
printf("----->> YYYYYYYYYYYYYYYY!\n\n");
if(n <= 0)
{
perror("read data");
//break;
}
buffer[n] = '\0';
printf("===========>>%s", buffer);
printf("----->> input test after!\n\n");
if(0 == strncmp(buffer, "quit", 4))
{
printf("User send quit!\n");
//break;
}
printf("=======>>>   Program will exit!\n");
//}
close(stty_fd);
}

//为什么read不出来,且运行到read的时候整个程序会卡住,跪求各位大神解答


不胜感激,谢谢!!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值