码迷,mamicode.com
首页 > 其他好文 > 详细

[RK_2014_0911]串口代码备份,SerialPortOpen(),SerialPortRead(),SerialPortWrite()

时间:2014-09-11 19:07:02      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   for   

【代码备份01】

uart.c

bubuko.com,布布扣
/*******************************************************************************
Copyright (C), *** Tech. Co., Ltd.
FileName: uart.c
Author:   
Version :  
Date: 2008-1-11
Description: 串口的通用操作文件
other:
Function List:

History: // 历史修改记录
    1. Date:
        Author:
        Version:
        Modification:
*******************************************************************************/
#include "uart.h"
#include "system.h"


int SpeedArr[6] = { B1200, B1800, B2400, B4800, B9600, B19200 };

/*******************************************************************************
Function:  UartOpen
Description: 初始化串口
Calls:
Called By:
Global Accessed:
Global Updated:
Input:  1.UartInitParam:串口初始化参数
Output:
Return: -1为失败, 成功返回串口的文件描述符
Others:
*******************************************************************************/
int
UartOpen (int UartIndex, UART485_INIT_CONFIG *pUartInitConfig,
          int *pUartFd)
{
#ifndef    MCU_I686
    
#else                            //上位机模拟
    char dev[20];
    struct termios opt;

    sprintf (dev, "/dev/ttyS%d", UartIndex);
    if (((*pUartFd) = open (dev, O_RDWR | O_NOCTTY)) < 0)
    {
        printf ("can not open ttyS%d device\n", UartIndex);
        return -1;
    }

    tcgetattr ((*pUartFd), &opt);
    //设置波特率
    if ((pUartInitConfig->speed) > 5)
        return -1;
    cfsetispeed (&opt, SpeedArr[(int) pUartInitConfig->speed]);
    cfsetospeed (&opt, SpeedArr[(int) pUartInitConfig->speed]);
    //设置数据位
    opt.c_cflag &= (~CSIZE);
    switch (pUartInitConfig->databit)
    {
    case 7:
        opt.c_cflag |= CS7;
        break;
    case 8:
        opt.c_cflag |= CS8;
        break;
    default:
        printf ("Unsupported data size\n");
        return -1;
    }
    //设置停止位
    switch (pUartInitConfig->stopbit)
    {
    case 1:
        opt.c_cflag &= ~CSTOPB;
        break;
    case 2:
        opt.c_cflag |= CSTOPB;
        break;
    default:
        printf ("Unsupported stop bits\n");
        return -1;
    }
    //设置校验位
    switch (pUartInitConfig->verify)
    {
    case n:
        opt.c_cflag &= ~PARENB;
        opt.c_iflag &= ~INPCK;
        break;
    case o:
        opt.c_cflag |= (PARODD | PARENB);
        opt.c_iflag |= INPCK;
        break;
    case e:
        opt.c_cflag |= PARENB;
        opt.c_cflag &= ~PARODD;
        opt.c_iflag |= INPCK;
        break;
    default:
        printf ("Unsupported parity\n");
        return -1;
    }
    //设置超时时间
    opt.c_cc[VTIME] = 10;
    opt.c_cc[VMIN] = 0;
    //设置为RAW模式
    opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
    opt.c_iflag &= ~(IXON | IXOFF | ICRNL);
    opt.c_oflag &= ~OPOST;

    tcflush ((*pUartFd), TCIOFLUSH);
    return tcsetattr ((*pUartFd), TCSANOW, &opt);
#endif
}

/*******************************************************************************
Function:  UartClose
Description: 关闭串口
Calls:
Called By:
Global Accessed:
Global Updated:
Input:  1.UartFd:串口的文件描述符
Output:
Return: -1为失败
Others:
*******************************************************************************/
int
UartClose (int UartFd)
{
    return close (UartFd);
}

/*******************************************************************************
Function:  UartSend
Description: 串口的发送函数
Calls:
Called By:
Global Accessed:
Global Updated:
Input:  1.UartFd:串口的文件描述符
           2.SendBuff:发送缓冲区
           3.SendNum:发送字节数
Output:
Return: -1为失败,
Others:
*******************************************************************************/
int
UartSend (int UartFd, unsigned char *pSendBuff, int SendNum)
{
    int n_send, write_num;

    for (n_send = 0; n_send < SendNum;)
    {
        write_num = write (UartFd, &pSendBuff[n_send], SendNum - n_send);
        if (write_num <= 0)        //发送失败
            return -1;
        n_send = n_send + write_num;
    }

    return n_send;
}

/*******************************************************************************
Function:  UartRcv
Description: 串口的接收函数
Calls:
Called By:
Global Accessed:
Global Updated:
Input:  1.UartFd:串口的文件描述符
           2.RcvBuff:接收缓冲区
           3.RcvNum:接收字节数
Output:
Return: -1为失败,
Others:
*******************************************************************************/
int
UartRcv (int UartFd, unsigned char *pRcvBuff, int RcvNum, int OverTime)
{
#ifndef MCU_I686
    
#else
    int n_rcv, read_num;

    for (n_rcv = 0; n_rcv < RcvNum;)
    {
        read_num = read (UartFd, &pRcvBuff[n_rcv], RcvNum - n_rcv);
        if (read_num <= 0)        //接收超时或失败
            return -1;
        n_rcv = n_rcv + read_num;
    }

    return n_rcv;
#endif
}

/*******************************************************************************
Function:  UartClearRcvBuf
Description: 串口缓冲区的清空函数
Calls:
Called By:
Global Accessed:
Global Updated:
Input:  1.UartFd:串口的文件描述符
Output:
Return: -1为失败,
Others:
*******************************************************************************/
int
UartClearRcvBuf (int UartFd)
{
#ifndef MCU_I686
    
#else
    return tcflush (UartFd, TCIOFLUSH);
#endif
}
View Code

 

uart.h

bubuko.com,布布扣
/*******************************************************************************
Copyright (C), *** Tech. Co., Ltd.
File name: uart.h   
Author:   
Version:  
Date:     2008-1-11
Description:  串口通用操作的头文件
Others:
Function List:
    1.  int UartOpen( UART_INIT_PARAM UartInitParam );
    2.  int UartClose( int UartFd );
    3.  int UartSend( int UartFd, unsigned char *SendBuff, int SendNum );
    4.  int UartRcv( int UartFd, unsigned char *RcvBuff, int RcvNum );
    5.     int UartRcv( int UartFd );
History:
    1. Date:
        Author:
        Modification:
*******************************************************************************/

#ifndef  _UART_H_
#define _UART_H_

#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <semaphore.h>
#include <mntent.h>
#include <sys/vfs.h>
#include <getopt.h>
#include <termios.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/ioctl.h>

extern int SpeedArr[6];

typedef struct _UART485_INIT_CONFIG
{
    char speed;                    //串口速度, 0-5代表1200,1800,2400,4800,9600,19200
    char databit;                //串口数据位  5,6,7或8
    char stopbit;                //串口停止位   1或2
    char verify;                //串口校验位   ‘n‘,‘o‘,‘e‘分别代表无校验,奇校验,偶校验
} UART485_INIT_CONFIG;

int UartOpen (int UartIndex, UART485_INIT_CONFIG *pUartInitConfig,
              int *pUartFd);
int UartClose (int UartFd);

int UartSend (int UartFd, unsigned char *pSendBuff, int SendNum);

int UartRcv (int UartFd, unsigned char *pRcvBuff, int RcvNum,
             int OverTime);
int UartClearRcvBuf (int UartFd);

#endif                            //end   _UART_H_
View Code

 

【代码备份02】

 

 

 

 

【完结】

[RK_2014_0911]串口代码备份,SerialPortOpen(),SerialPortRead(),SerialPortWrite()

标签:des   style   blog   http   color   io   os   ar   for   

原文地址:http://www.cnblogs.com/tom-and-jerry/p/3966984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!