Linux串口初始化流程

Linux串口初始化流程demo

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <linux/ioctl.h>
#include <sys/ioctl.h>
#include <semaphore.h>
#include <termios.h>

#define HI_SUCCESS              0
#define HI_FAILURE              (-1)

/*****************************************************************************
* Function:       UART_Read
* Description:   从串口读取数据
* Input:         s32Fd :串口fd
                 s32Len :读取长度
* Output:        pData :读数据指针
* Return:        成功返回读取到的数据长度,否则返回-1
* Others:        无
*****************************************************************************/
int UART_Read(int s32Fd, char * pData, int s32Len)
{
    if (pData == NULL)
    {
        return HI_FAILURE;
    }

    if (s32Fd < 0)
    {
        return HI_FAILURE;
    }

    return read(s32Fd, pData, s32Len);
}

/*****************************************************************************
* Function:       UART_Write
* Description:   往串口写入数据
* Input:         s32Fd :串口fd
                 pData :数据指针
                 s32Len :写入长度
* Output:        无
* Return:        成功返回写入的数据长度,否则返回-1
* Others:        无
*****************************************************************************/
int UART_Write(int s32Fd, const char * pData, int s32Len)
{
    int s32WriteLen = 0;

    if (pData == NULL)
    {
        return HI_FAILURE;
    }

    s32WriteLen = write(s32Fd, pData, s32Len);
    if (s32WriteLen < 0 && errno != EAGAIN)
    {
        printf("write failed - errno=%d (%s)\n", errno, strerror(errno));
    }

    return s32WriteLen;
}

/*****************************************************************************
* Function:      UART_Config
* Description:   串口配置
* Input:         s32Fd :串口fd
                 s32BaudRate :波特率
                 u8DataBit :数据位
                 u8StopBit :停止位
                 u8OeBit :校验位
* Output:        无
* Return:        成功返回0,否则返回错误码
* Others:        无
*****************************************************************************/
static int UART_Config(int s32Fd, int s32BaudRate, unsigned char  u8DataBit, unsigned char  u8StopBit, signed char u8OeBit)
{
    struct termios newtio;

    bzero(&newtio,sizeof(newtio));

    /* 设置接收使能 */
    newtio.c_cflag |= CLOCAL | CREAD;

    /* 设置波特率 */
    switch(s32BaudRate)
    {
        case 2400:
            cfsetispeed(&newtio, B2400);
            cfsetospeed(&newtio, B2400);
            break;
        case 4800:
            cfsetispeed(&newtio, B4800);
            cfsetospeed(&newtio, B4800);
            break;
        case 9600:
            cfsetispeed(&newtio, B9600);
            cfsetospeed(&newtio, B9600);
            break;
        case 38400:
             cfsetispeed(&newtio, B38400);
             cfsetospeed(&newtio, B38400);
             break;
        case 57600:
             cfsetispeed(&newtio, B57600);
             cfsetospeed(&newtio, B57600);
             break;
        case 115200:
            cfsetispeed(&newtio, B115200);
            cfsetospeed(&newtio, B115200);
            break;
        case 460800:
            cfsetispeed(&newtio, B460800);
            cfsetospeed(&newtio, B460800);
            break;
        default:
            printf("No this baudrate set !!!\n");
            cfsetispeed(&newtio, B9600);
            cfsetospeed(&newtio, B9600);
            break;
    }

    /* 设置数据位 */
    switch(u8DataBit)
    {
        case 7: newtio.c_cflag |= CS7; break;
        case 8: newtio.c_cflag |= CS8; break;
        default: newtio.c_cflag |= CS8; break;
    }

    /* 设置停止位 */
    switch(u8StopBit)
    {
        case 1: newtio.c_cflag &= ~CSTOPB; break;
        case 2: newtio.c_cflag |= CSTOPB; break;
        default: newtio.c_cflag &= ~CSTOPB; break;
    }

    /* 设置校验位 */
    switch(u8OeBit)
    {
        case 'O':    /* 奇校验 */
            newtio.c_cflag |= PARENB;
            newtio.c_cflag |= (INPCK | ISTRIP);
            newtio.c_cflag |= PARODD;
            break;
        case 'E':    /* 偶校验 */
            newtio.c_cflag |= PARENB;
            newtio.c_cflag |= (INPCK | ISTRIP);
            newtio.c_cflag &= ~PARODD;
            break;
        case 'N':    /* 不校验 */
        default:
            newtio.c_cflag &= ~PARODD;
            break;
    }

    newtio.c_cc[VTIME] = 0;  /* 等待时间 */
    newtio.c_cc[VMIN] = 0;   /* 最小接收字符数 */

    tcflush(s32Fd, TCIFLUSH);   /* 处理未接收字符 */

    /* 激活配置 */
    if(0 != tcsetattr(s32Fd, TCSANOW, &newtio))
    {
        printf("uart config error!\n");
        return HI_FAILURE;
    }

    return HI_SUCCESS;
}

/*****************************************************************************
* Function:      UART_Close
* Description:   关闭串口
* Input:         s32Fd :串口fd
* Output:        无
* Return:        成功返回0,否则返回错误码
* Others:        无
*****************************************************************************/
int UART_Close(int s32Fd)
{
    close(s32Fd);
    return HI_SUCCESS;
}

/*****************************************************************************
* Function:      UART_Open
* Description:   打开串口
* Input:         enUartDev :串口设备类型
* Output:        无
* Return:        成功返回fd,否则返回-1
* Others:        无
*****************************************************************************/
int UART_Open()
{
    int s32Fd = 0;
    int s32Ret = 0;

    /* 打开串口 */
    s32Fd = open("/dev/ttyS4", O_RDWR | O_NOCTTY  | O_NONBLOCK);
    if(s32Fd < 0)
    {
        printf("Open error!\n");
        return HI_FAILURE;
    }

    /* 配置串口 */

    s32Ret =  UART_Config(s32Fd,115200, 8,1, 'N');
    if (HI_SUCCESS != s32Ret)
    {
         UART_Close(s32Fd);
        return HI_FAILURE;
    }

    return s32Fd;
}

unsigned char MCU_CalcCheckSum(unsigned char  *dat, unsigned char  len) {
    unsigned char checkSum = 0xFF;
    while (len--) {
        checkSum ^= (*dat);
        dat++;
    }
    return checkSum;
}


int main()
{
    int s32Fd = 0;
    s32Fd = UART_Open();

    char msg[8] = {0x55, 0xaa};
    msg[2] = 0xf5;
    msg[3] = 0x01;
    msg[4] = 0x00;
    msg[5] = 0x00;
    msg[6] = 0x00;
    msg[7] = MCU_CalcCheckSum(msg, 7);


    UART_Write(s32Fd,msg,8);
    UART_Close(s32Fd);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值