#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#define BAUDRATE B115200 ///Baud rate : 115200
#define DEVICE "/dev/ttyAMA0"
#define SIZE 1024
int nFd = 0;
struct termios stNew;
struct termios stOld;
//Open Port & Set Port
int SerialInit()
{
nFd = open(DEVICE, O_RDWR|O_NOCTTY|O_NDELAY);
if(-1 == nFd)
{
perror("Open Serial Port Error!\n");
return -1;
}
if( (fcntl(nFd, F_SETFL, 0)) < 0 )
{
perror("Fcntl F_SETFL Error!\n");
return -1;
}
if(tcgetattr(nFd, &stOld) != 0)
{
perror("tcgetattr error!\n");
return -1;
}
stNew = stOld;
cfmakeraw(&stNew);//将终端设置为原始模式,该模式下所有的输入数据以字节为单位被处理
//set s
linux 下串口通信
博客内容提及在Linux系统中遇到串口通信问题,具体表现为在/dev/ttyUSB*设备下能够正常工作,但在/dev/ttyAMA*下无法进行通信。作者推测可能是串口配置不正确,但目前未进行深入排查,计划未来研究解决这个问题。参考了一篇CSDN上的相关文章。
摘要由CSDN通过智能技术生成