linux asio 读取串口,ASIO 串口编程
using namespace std;
using boost::asio::serial_port;
class CTemperatureDlg;
/*! @class
********************************************************************************
类名称 : CSerialPort
功能 : 串口通讯类
异常类 : 无
--------------------------------------------------------------------------------
备注 :
典型用法 :
--------------------------------------------------------------------------------
作者 : 毛勇
*******************************************************************************/
class CSerialPort : public CChannel
{
public:
/ 构造和虚析构
CSerialPort(boost::asio::io_service& ioService,
const CCommAttr &commAttr,
CTemperatureDlg *pDlg
);
~CSerialPort();
public:
/ 公用方法
// [[ 功能组1
//! 处理接收到的数据
void HandleReceiveFrom(const boost::system::error_code& error,
size_t nBytesReceived
);
//! 处理发生数据
void HandleSendTo(const boost::system::error_code& /*error*/,
size_t /*bytes_sent*/
);
//! 直接发送数据
void Send(const string &strSend, const string &strSendType);
//! 申请资源
//int OpenPort();
//! 关闭端口
void Close();//退出();
// [[ 功能组1
private:
/ 私有方法
// [[ 功能组1
// [[ 功能组1
private:
/ 私有成员
boost::asio::io_service &m_ioService;
serial_port m_serialPort;
enum { MAX_LENGTH = 100 };
//! 接收缓冲区
char m_chRecvBuf[MAX_LENGTH];
//! 发生缓冲区
char m_chSendBuf[MAX_LENGTH];
};
==================================================================
==================================================================
串口cpp文件
==================================================================
==================================================================
#include "StdAfx.h"
#include "SerialPort.h"
#include "Resource.h"
#include "TemperatureDlg.h"
#include "Channel.h"
#include "TemperatureDlg.h"
CSerialPort::CSerialPort(boost::asio::io_service& ioService,
const CCommAttr &commAttr,
CTemperatureDlg *pDlg)
: m_ioService(ioService)
, CChannel (commAttr, pDlg) //初始化对话框指针
, m_serialPort(ioService, commAttr.strSerialPort.c_str())
{
// =========================================================================
// = 初始化波特率 fixme:波特率需要检查下是否是支持的波特率。通过表驱动检查比较简单
m_serialPort.set_option(serial_port::baud_rate(commAttr.nBaudRate));
// =========================================================================
// = 初始化流控制
if (0 == commAttr.strFlowControl.compare("none"))
{
m_serialPort.set_option(serial_port::flow_control(serial_port::flow_control::none));//无
}
else if (0 == commAttr.strFlowControl.compare("software"))
{
m_serialPort.set_option(serial_port::flow_control(serial_port::flow_control::software));//软件
}
else if (0 == commAttr.strFlowControl.compare("hardware"))
{
m_serialPort.set_option(serial_port::flow_control(serial_port::flow_control::hardware));//硬件
}
else
{
//出错,抛出异常?
}
// =========================================================================
// = 初始化奇偶校验
if (0 == commAttr.strParity.compare("none"))
{
m_serialPort.set_option(serial_port::parity(serial_port::parity::none));
}
else if (0 == commAttr.strParity.compare("odd"))
{
m_serialPort.set_option(serial_port::parity(serial_port::parity::odd));
}
else if (0 == commAttr.strParity.compare("even"))
{
m_serialPort.set_option(serial_port::parity(serial_port::parity::even));
}
else
{
}
// =========================================================================
// = 初始化停止位
if (0 == commAttr.strStopBits.compare("one"))
{
m_serialPort.set_option(serial_port::stop_bits(serial_port::stop_bits::one));
}
else if (0 == commAttr.strStopBits.compare("onepointfive"))
{
m_serialPort.set_option(serial_port::stop_bits(serial_port::stop_bits::onepointfive));
}
else if (0 == commAttr.strStopBits.compare("two"))
{
m_serialPort.set_option(serial_port::stop_bits(serial_port::stop_bits::two));
}
// =========================================================================
// = 初始化数据位
m_serialPort.set_option(serial_port::character_size(commAttr.nCharacterSize));
// =========================================================================
// = 开始读数据
m_serialPort.async_read_some(boost::asio::buffer(m_chRecvBuf, MAX_LENGTH),
boost::bind(&CSerialPort::HandleReceiveFrom,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
);
}
CSerialPort::~CSerialPort()
{
}
void
CSerialPort::HandleSendTo( const boost::system::error_code& /*error*/, size_t /*bytes_sent*/ )
{
m_serialPort.async_read_some(//async_receive_from
boost::asio::buffer(m_chRecvBuf, MAX_LENGTH),
boost::bind( &CSerialPort::HandleReceiveFrom,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
);
return;
}
void
CSerialPort::HandleReceiveFrom( const boost::system::error_code& error, size_t nBytesReceived )
{
if (!error && nBytesReceived > 0)
{
string strRecv(m_chRecvBuf, nBytesReceived);
m_pDlg->UpdateRecvMessage(strRecv); //更新主界面的接收发送
}
m_serialPort.async_read_some(boost::asio::buffer(m_chRecvBuf, MAX_LENGTH),
boost::bind( &CSerialPort::HandleReceiveFrom,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
);
}
void
CSerialPort::Send(const string &strSend, const string &strSendType )
{
memcpy (m_chSendBuf, strSend.c_str(), strSend.length());
m_serialPort.async_write_some(boost::asio::buffer(m_chSendBuf, strSend.length()),
boost::bind(&CSerialPort::HandleSendTo,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred
)
);
}
void
CSerialPort::Close()
{
if (m_serialPort.is_open()) {
m_serialPort.close();
}
}
=====================================================================
=================== channel =========================================
/*! @file
********************************************************************************
模 块名 : 通讯模块 comm
文件名 : Channel.h
相关文件 : Channel.cpp
Product.h Product.cpp //通道
TcpServer.h TcpServer.cpp
UdpClient.h UdpClient.cpp
UdpServer.h UdpServer.cpp
SerialPort.h SerialPort.cpp
文件实现功能 : 存储软件版本号
作者 : 毛勇
版本 : 1.0
--------------------------------------------------------------------------------
多线程安全性 : 是
异常时安全性 : 是
--------------------------------------------------------------------------------
备 注 :
--------------------------------------------------------------------------------
修 改记录 :
日 期 版本 修改人 修改内容
2010/05/28 1.0.0 毛勇 创建
*******************************************************************************/
#pragma once
#include using std::string;
/*! @class
********************************************************************************
类名称 : CCommAttr
功能 : 通讯参数,用来创建连接所需要的参数。
异常类 : 无
--------------------------------------------------------------------------------
备注 :
典型用法 :
--------------------------------------------------------------------------------
作者 : 毛勇
*******************************************************************************/
class CCommAttr
{
public:
string strSerialPort;
int nBaudRate; //9600
string strParity;//none, odd, even。
string strStopBits; //停止位,构造参数为serial_port::stop_bits::type,enum类型,可以是one onepointfive two
string strFlowControl; //流控制 none software hardware
int nCharacterSize; //character_size 5,6,7,8
};
/*
父类: CChannel
子类: CTcpServer, CTcpClient, CUdpServer, CUdpClient 都是从 CChannel 派生
class CTcpServer: public CChannel
{
};
class CTcpClient: public CChannel
{
};
class CUdpServer: public CChannel
{
};
class CUdpClient: public CChannel
{
};
*/
class CTemperatureDlg;
//=============================================================================
//================= channel 类 相当于工厂模式中的 product 类==================
//=============================================================================
class CChannel
{
public:
/ 虚析构
virtual ~CChannel();
/ 保护构造函数
protected:
CChannel(const CCommAttr &commAttr, CTemperatureDlg *pDlg); //const CCommAttr &commAttr
public:
/ 公用方法
// [[ 功能组1
//virtual void Suspend() = 0;//暂停
//virtual void Startup() = 0;//启动
virtual void Close() = 0;//退出
virtual void Send(const string &strSend, const string &strSendType) = 0; //发送
// ]] 功能组1
protected:
//CCommAttr m_commAttr;
const CCommAttr m_commAttr;
CTemperatureDlg *m_pDlg;
};
==================================================================
#include "StdAfx.h"
#include "Channel.h"
//=============================================================================
//================= channel 类 相当于工厂模式中的 product 类==================
//=============================================================================
CChannel::CChannel( const CCommAttr &commAttr,
CTemperatureDlg *pDlg)
: m_commAttr(commAttr)
, m_pDlg(pDlg)
{
}
CChannel::~CChannel()
{
}
linux asio 读取串口,ASIO 串口编程相关推荐
- Linux下读取RFID卡号(C串口编程)
由于项目需要用到RFID.GPRS.摄像头等模块所以便看了一下,整理了一下学习思路,本篇先是整理一下串口读取RFID卡号的程序思路,后面还会更其他的 RFID模块: 本次采用的是125K的RFID读卡 ...
- Linux串口原理与编程
Linux 串口原理 2. How the Hardware Transfers Bytes mit这里说得巨清楚,串口相关原理的看这个就行,CPU和MCU串口接收差不多. When the ...
- linux串口介绍与编程
一.串口介绍 串口也称串行通信接口(通常指COM接口),是实际工作中经常使用的一个接口,比如Linux下使用的debug串口,它用来登录Linux系统,输出log.另外也会使用串口和外部的一些模块通信 ...
- Linux下C语言串口应用编程,Linux下串口C语言编程
Linux下串口C语言编程 (5页) 本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦! 9.9 积分 串口操作代码#include #include #inclu ...
- jy61 树莓派_用Linux树莓派来读取JY61的串口数据
简述 有很多的小伙伴说用Linux树莓派来读取JY61的串口数据不知道怎么操作.今天我和大家分享下我是从三个方面分享的.1.JY61和树莓派的连接方式及VNC的使用:2.下载到树莓派的程序是如何编写的 ...
- linux设备驱动,tty串口编程 如何查看linux下串口是否可用?串口名称等
如何查看linux下串口是否可用?串口名称等? http://zhidao.baidu.com/question/419148559.html 查看串口是否可用,可以对串口发送数据比如对com1口,e ...
- 解决Linux非root用户读写串口权限问题
解决Linux非root用户读写串口权限问题 如需转载请标明出处:http://blog.csdn.net/itas109 QQ技术交流群:129518033 目录 文章目录 解决Linux非root ...
- UART0串口编程(六):串口(UART0)之UC/OS(二)UC/OS下的串口接收任务编程
串口(UART0)之UC/OS(二) 一.串口接收数据在UC/OS设计中应注意的问题 1. 串口通信的数据接收过程: 1> UART 接收FIFO接收到预定字节后触发中断 2> ...
- linux can总线接收数据串口打包上传_USART串口通讯
在计算机科学里,大部分复杂的问题都可以通过分层来简化.如芯片被分为内核层和片上外设:STM32 标准库则是在寄存器与用户代码之间的软件层.对于通讯协议,我们也以分层的方式来理解,最基本的是把它分为物理 ...
- 串口通信与编程:串口基础知识
*************************************************** 更多精彩,欢迎进入:http://shop115376623.taobao.com ****** ...
最新文章
- Oauth2认证以及新浪微博开放平台应用
- php jquery 全选删除,jQuery+php简单实现全选删除的方法
- 34. 在排序数组中查找元素的第一个和最后一个位置 golang
- C语言中 用选择结构编译算法,C语言程序设计立体化教程(高等教育立体化精品系列规划教材)...
- 专业英语笔记:Install and Use Python
- apache源码安装必须依赖的库apr----/etc/ld.so.conf 文件介绍
- c++ 截取屏幕图片并保存
- 持续集成:什么应该自动化?
- 解决-系统策略禁止安装此设备,请与系统管理员联系
- ASP.NET设置背景图案
- 计算机的影视后期论文,浅谈影视后期制作-毕业论文提纲范文
- Thread-Specific Storage Pattern
- Python OS模块和文件处理
- 欧盟新法将个人隐私放在首位
- C语言高级部分总结,也是面试官会经常问的问题哦~
- C/C++中int的取值范围
- Jenkins框架原理
- 六级(2021/6-1) Text2
- 化工人员定位系统提供智能安全保障
- Android 编译之source和lunch
热门文章
- 计算机英语辅助翻译软件,计算机辅助翻译(CAT)软件 计算机辅助翻译(CAT)软件 v6.3.0.616...
- 1分钟教你破解风行电视禁止安装应用!
- 0x800700b7 linux,0x800700b7解决方法
- FastDFS文件存储系统
- S2B2C做得好,功劳全在一件代发功能
- LoadRunner教程(2)-LoadRunner性能测试利器
- 华为命令 hybird实验
- 批处理 bat for 详解
- MyBatisPlus——条件构造器
- Deepin 深度Linux系统安装教程