Linux下的串口通信
移植了一个简单的开发板与pC机的串口通信,代码如下
/*发送字符*/
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<termios.h>
#define buffer_size 100
volatile fd;
int flag_close;
int open_serial(int k)
{
if(k==0)
{
fd=open("/dev/ttySAC0", O_NOCTTY |O_NONBLOCK);
perror("open /dev/ttySAC0");
}
else
{
fd=open("/dev/ttySAC1",O_RDWR);
perror("open /dec/ttySAC1");
}
if(fd==-1)
return -1;
else
return 0;
}
int main(int argc,char *argv[])
{
char sbuf[]={"hello\n"};
int retv,i;
struct termios opt;
int length=sizeof(sbuf);
open_serial(0);
printf("ready for sending data");
tcgetattr(fd,&opt);
tcflush(fd,TCIOFLUSH);
cfmakeraw(&opt);//设置终端属性
cfsetispeed(&opt,B115200);
cfsetospeed(&opt,B115200);
/*设置8位数据位*/
opt.c_cflag &=~CSIZE;
opt.c_cflag |=CS8;
/*设置奇偶校验*/
opt.c_cflag &=~PARENB;
opt.c_cflag &=~INPCK;
tcsetattr(fd,TCSANOW,&opt);
retv=write(fd,sbuf,length);//将数据写入终端不需要1位1位的写吗?
if(retv==-1)
{
perror("write");
}
printf("the number of char sent is %d\n",retv);
flag_close=close(fd);
if(flag_close==-1)
printf("close the device failur!\n");
return 0;
}
结果运行时显示错误为:open /dev/ttySAC0: No such file or directory
write: Bad file descriptor
ready for sending datathe number of char sent is -1
close the device failur!
初学,有没有人能耐心看一下,指导一下,谢谢!
61,1 92%
/*发送字符*/
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<termios.h>
#define buffer_size 100
volatile fd;
int flag_close;
int open_serial(int k)
{
if(k==0)
{
fd=open("/dev/ttySAC0", O_NOCTTY |O_NONBLOCK);
perror("open /dev/ttySAC0");
}
else
{
fd=open("/dev/ttySAC1",O_RDWR);
perror("open /dec/ttySAC1");
}
if(fd==-1)
return -1;
else
return 0;
}
int main(int argc,char *argv[])
{
char sbuf[]={"hello\n"};
int retv,i;
struct termios opt;
int length=sizeof(sbuf);
open_serial(0);
printf("ready for sending data");
tcgetattr(fd,&opt);
tcflush(fd,TCIOFLUSH);
cfmakeraw(&opt);//设置终端属性
cfsetispeed(&opt,B115200);
cfsetospeed(&opt,B115200);
/*设置8位数据位*/
opt.c_cflag &=~CSIZE;
opt.c_cflag |=CS8;
/*设置奇偶校验*/
opt.c_cflag &=~PARENB;
opt.c_cflag &=~INPCK;
tcsetattr(fd,TCSANOW,&opt);
retv=write(fd,sbuf,length);//将数据写入终端不需要1位1位的写吗?
if(retv==-1)
{
perror("write");
}
printf("the number of char sent is %d\n",retv);
flag_close=close(fd);
if(flag_close==-1)
printf("close the device failur!\n");
return 0;
}
结果运行时显示错误为:open /dev/ttySAC0: No such file or directory
write: Bad file descriptor
ready for sending datathe number of char sent is -1
close the device failur!
初学,有没有人能耐心看一下,指导一下,谢谢!
61,1 92%
...全文
相关推荐
Linux下QT串口通信
本文将深入探讨如何在Linux下利用QT进行串口通信,并结合提供的"demo"示例代码来讲解相关知识点。首先,我们需要了解Linux下的串口基础。
linux c++ 简单串口通信
在Linux环境下进行C++编程,实现串口通信是一项基础但重要的技能,特别是在嵌入式系统、设备控制或者物联网应用中。
linux串口通信
在Linux系统中,串口通信是一种基础且重要的通信方式,常用于设备间的低速数据传输。本文将深入探讨Linux串口通信的相关知识点,包括串口的基本概念、配置、编程接口以及常见应用。**1.
Linux 串口通信实例
在IT行业中,Linux系统因其开源、稳定和强大的网络支持而被广泛应用。特别是在嵌入式设备和服务器领域,Linux串口通信扮演着至关重要的角色。
Linux 串口通信
Linux串口通信是一种在计算机与外部设备之间进行数据交换的技术,尤其在嵌入式系统、自动化设备和物联网应用中广泛使用。


驱动开发/核心开发
