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

【代码备份01】

uart.c

/*******************************************************************************
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
}

uart.h

/*******************************************************************************
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_

【代码备份02】

【完结】

时间: 2024-08-07 03:27:03

[RK_2014_0911]串口代码备份,SerialPortOpen(),SerialPortRead(),SerialPortWrite()的相关文章

JSONP平台对接代码备份

<script type="text/javascript">$(function(){  $.ajax({   url:"http://192.168.11.97:8025/battle_summary?player_guid=1000000",   async: false,   dataType:"jsonp",   jsonpCallback:"summary",   success:function(su

[python]南邮OJ代码备份爬虫

之前看过Python学习的经验,说以工程为导向学习. 自己分析了一下,一般接触Python的都有一定的其他语言基础,对于程序设计的基本逻辑,语法都有一个大概的了解.而Python这种脚本语言,没有过于独特的语法,在一定的其他语言的基础上,更是可以直接上手的. 之前看Python简明教程,半天没有进度.正好遇上Python爬虫项目,直接上手,方便快捷. 网站:http://acm.njupt.edu.cn/welcome.do?method=index,正值系统更新,于是写一个备份代码的爬虫. 使

visual studio代码备份精减

当我们想备份自己的解决方案代码时,发现本来只有kB级的代码文件,visual studio编译后竟达到了几十甚至几百MB的量级.此时为了节省硬盘空间,我们需要把一些代码无关的东西精减掉,这些东西包括: Debug文件夹.x64文件夹.ipch文件夹..sdf文件..VC.db文件 以上文件夹和文件都会在编译时产生,对于我们代码备份无关紧要,所以都可以删除.另外,不同visual studio版本,可能产生的文件夹或文件不尽相同,但基本差不多,只要看到上面那些文件,尽可删除就是.如此精减后,解决方

代码备份机

案例: 代码备份机 1.打包备份 2. 自动命名 3.大抱歉进行文件筛选,值备份特定文件 4. 定时备份 编程思路: 1.项目:拆解 2.单功能:写函数 3. 多功能:合并类 4 写代码:先写框架,后完善 5.要点: 完成比完美更重要 # 第一步:根据功能设计函数 # 1.打包 def zip_all(): '''打包''' pass def auto_name(): '''自动命名''' pass def zip_all_by_name(): '''筛选文件''' pass # 第二步:完善函

web服务文件更新自动同步、数据库主从复制、shell脚本实现网站代码备份和mysql备份

基搭建LAMP环境,并实践基于DNS做基于域名的虚拟主机中的环境,重新搭建一个同样的环境 要求: a)实现web服务文件更新的自动同步到另一台机器上 b)数据库实现主从复制 c)通过shell脚本实现网站源代码备份和mysql备份,备份策略包括全量备份.增量备份.差异备份 a,实现web服务文件更新的自动同步到另一台机器上: 1,在httpd服务器上建立基于FQDN的两个虚拟web站点,并创建相关目录. 2,修改测试windows主机的hosts文件,并编辑两个虚拟web站点对应的目录下的ind

Qt 窗体间传值(代码备份)

刚开始看的时候看的云里雾里的,现在稍微明白一点了.现在假设有一个form,一个MainWindow,如图所示: 实现点击PushButton,将文本框中的内容传输到MainWindow中,显示为Label.界面我已经提前画好.下面是备份代码: form.h: #ifndef FORM_H #define FORM_H #include <QWidget> namespace Ui { class Form; } class Form : public QWidget { Q_OBJECT pu

THREE.js代码备份——canvas - lines - colors(希尔伯特曲线3D、用HSL设置线颜色)

<!DOCTYPE html> <html lang="en"> <head> <title>three.js canvas - lines - colors</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, user-scalable=no, m

pdf对excel数据转换代码备份

一.综合测试代码 # -*- coding: utf-8 -*- """ Created on Wed Dec  2 15:46:02 2015 @author: Administrator 13-1-001之后的就发生读取发生错误,index=1976 index=4100发生错误 """ import PyPDF2,os,openpyxl from openpyxl.cell import get_column_letter,column_i

Octopus 代码备份

代码 $.extend($.validator.messages, { required: "This field is required.", remote: "Please fix this field.", email: "Please enter a valid email address.", url: "Please enter a valid URL.", date: "Please enter a v