arm+linux 串口传送数据(正确版)

本文介绍了一种在ARM Linux环境下实现串口通信的正确方法,包括串口初始化、发送和接收函数,解决了之前存在的数据错误问题。通过示例代码展示了如何打开、配置、关闭串口,并进行数据读写操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    之前,利用RXD,TXD回环来读取发送的数据,原先的初始化有问题(见异常版),经常中间夹杂莫名的数据,改用下面的初始化和发送、接收函数,一切正常了。两个还未进行对比,等有空闲的时候做下对比。

#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>

 

 

 

/* the maximum number of ports we are willing to open */
#define MAX_PORTS 4

/*this array hold information about each port we have opened */
struct PortInfo{
 int busy;
 char name[32];
 int handle;
};

 

 


struct PortInfo ports[MAX_PORTS];

/*
** Function: OpenCom
**
** Description:
**    Opens a serial port with default parameters
**
** Arguments:
**    portNo - handle used for further access
**    deviceName - the name of the device to open
**
** Returns:
**    -1 on failure
*/
int OpenCom(int portNo, const char deviceName[],long baudRate)
{
    return OpenComConfig(portNo, deviceName, baudRate, 1, 8, 1, 0, 0);
}

/*

*/
long GetBaudRate(long baudRate)
{
    long BaudR;
    switch(baudRate)
    {
 case 115200:
  BaudR=B115200;
  break;
 case 57600:
  BaudR=B57600;
  break;
 case 19200:
  BaudR=B19200;
  break;
 case 9600:
  BaudR=B9600;
  break;
        case 8777:
                BaudR = B8777;
                break;
 default:
  BaudR=B0;
    }
    return BaudR;
}

/*
** Function: Op

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值