/*******************************************************************************
* Copyright (c), 2010, songchuangye.
* All rights reserved.
*
* File Name: SerialPort.h
* Description: 本类实现了对串口的基本操作
* Others:
* Version: 1.0 Author: songchuangye Date: 2013-09-16
* History:
* <Author> <Date> <Version > <Desc>
* scy 2013-09-16 1.0 Original
*******************************************************************************/
#include "libutil/OS.h"
#include "serialport.h"
/*
* Decription for TIMEOUT_SEC(buflen,baud);
* baud bits per second, buflen bytes to send.
* buflen*32 (32 means sending an octect-bit data by use of the maxim bits 20)
* eg. 9600bps baudrate, buflen=1024B, then TIMEOUT_SEC = 1024*32/9600+1 = 3
* don't change the two lines below unless you do know what you are doing.
*/
#define TIMEOUT_USEC(buflen,baud) ((buflen*32*1000000)/(baud*1000000)+50000)
/*******************************************************************************
* Function : CSerialPort
* Description:
* Calls : 见函数实现
* Called By :
* Input : 无
* Output : 无
* Return :
*******************************************************************************/
CSerialPort::CSerialPort()
{
#ifdef WIN32
/** 串口句柄 */
m_hComm = INVALID_HANDLE_VALUE;
#else
m_ComFd = -1;
#endif
m_WRErrTimes = 0;
}
/*******************************************************************************
* Function : ~CSerialPort
* Description:
* Calls : 见函数实现
* Called By :
* Input : 无
* Output : 无
* Return :
*******************************************************************************/
CSerialPort::~CSerialPort()
{
Close();
m_WRErrTimes = 0;
}
/*******************************************************************************
* Function : Open
* Description:
* Calls : 见函数实现
* Called By :
* Input : 无
* Output : 无
* Return :
*******************************************************************************/
SInt32 CSerialPort::Open(const char *portname, UInt32 baud, UInt8 parity, UInt32 nBits,
UInt32 nStop, UInt64 dwCommEvents)
{
/** 打开指定串口,该函数内部已经有临界区保护,上面请不要加保护 */
SInt32 ret = OpenPort(portname);
if ( ret != 0)
{
printf("open %s fail.ret:%d\n", portname, ret);
return -1;
}
//设置参数
ret = SetComOpt(baud, nBits, parity, nStop, dwCommEvents);
if ( ret != 0)
{
printf("set %s fail.ret:%d\n", portname, ret);
return -2;
}
memcpy(m_SeriaPortParam.m_PortName, portname, strlen(portname));
m_SeriaPortParam.m_BaudRate = baud;
m_SeriaPortParam.m_Parity = parity;
m_SeriaPortParam.m_Bit = nBits;
m_SeriaPortParam.m_StopBit = nStop;
m_SeriaPortParam.m_CommEvents = dwCommEvents;
m_WRErrTimes = 0;
return 0;
}
/*******************************************************************************
* Function : Open
* Description:
* Calls : 见函数实现
* Called By :
* Input : 无
* Output : 无
* Return :
*******************************************************************************/
SInt32 CSerialPort::Open(SerialPortParam seriaport)
{
return Open(seriaport.m_PortName, seriaport.m_BaudRate, seriaport.m_Parity,
seriaport.m_Bit, seriaport.m_StopBit, seriaport.m_CommEvents);
}
/*******************************************************************************
* Function : Close
* Description:
* Calls : 见函数实现
* Called By :
* Input : 无
* Output : 无
* Return :
*******************************************************************************/
void CSerialPort::Close()
{
return ClosePort();
}
/*******************************************************************************
* Function : IsOpen
* Description: 判断串口是否打开
* Calls : 见函数实现
* Called By :
* Input : 无
* Output : 无
* Return : 打开返回TRUE 关闭返回FALSE
*******************************************************************************/
Bool CSerialPort::IsOpen(const char *portname)
{
//读写错误次数大于10次就需要重新打开
if (m_WRErrTimes > 10)
{
return FALSE;
}
#ifdef WIN32
/** 打开指定的串口 */
HANDLE hComm = CreateFileA(portname, /** 设备名,COM1,COM2等 */
GENERIC_READ|GENERIC_WRITE, /** 访问模式,可同时读写 */
0, /** 共享模式,0表示不共享 */
NULL, /** 安全性设置,一般使用NULL */
OPEN_EXISTING, /** 该参数表示设备必须存在,否则创建失败 */
0,
0);
//打开失败,并且错误码是拒绝访问,表示串口已经打开
if (ERROR_ACCESS_DENIED == OS::ErrorNo())
{
return TRUE;
}
else
{
if (hComm != INVALID_HANDLE_VALUE)
{
CloseHandle(hComm);
hComm = INVALID_HANDLE_VALUE;
}
return FALSE;
}
#else
SInt32 ComFd = open(portname, O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == ComFd)
{
printf("Can't Open Serial Port.%d\n", OS::ErrorNo());
return TRUE;
}
else
{
close(ComFd);
ComFd = -1;
return FALSE;
}
#endif
}
/*******************************************************************************
* Function : ClosePort
* Description:
* Calls : 见函数实现
* Called By :
* Input : 无
* Output : 无
* Return :
*******************************************************************************/
void CSerialPort::ClosePort()
{
#ifdef WIN32
/** 如果有串口被打开,关闭它 */
if (m_hComm != INVALID_HANDLE_VALUE)
{
CloseHandle(m_hComm);
m_hComm = INVALID_HANDLE_VALUE;
}
#else
//暂不实现
if (m_ComFd != -1)
{
close(m_ComFd);
m_ComFd = -1;
}
#endif
return;
}
/*******************************************************************************
* Function : OpenPort
* Description:
* Calls : 见函数实现
* Called By :
* Input : 无
* Output : 无
* Return :
*******************************************************************************/
SInt32 CSerialPort::OpenPort(const char *portname)
{
SInt32 ret = 0;
#ifdef WIN32
if (m_hComm != INVALID_HANDLE_VALUE)
{
return -1;
}
/** 打开指定的串口 */
m_hComm = CreateFileA(portname, /** 设备名,COM1,COM2等 */
GENERIC_READ|GENERIC_WRITE, /** 访问模式,可同时读写 */
0, /** 共享模式,0表示不共享 */
NULL, /** 安全性设置,一般使用NULL */
OPEN_EXISTING, /** 该参数表示设备必须存在,否则创建失败 */
0,
0);
/** 如果打开失败,释放资源并返回 */
if (m_hComm == INVALID_HANDLE_VALUE)
{
printf("Can't Open Serial Port.Error:%d\n", OS::ErrorNo());
ret = -2;
}
#else
if (m_ComFd != -1)
{
return -1;
}
m_ComFd = open(portname, O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == m_ComFd)
{
printf("Can't Open Serial Port.%d\n", OS::ErrorNo());
return -2;
}
ret = fcntl(m_ComFd, F_SETFL, 0);
if (ret == -1)
{
LOG_WARN("fcntl failed!");
ret = -2;
}
else
{
//printf("fcntl=%d\n", ret);
////设置串口
//if (isatty(STDIN_FILENO) == 0)
//{
// LOG_WARN("standard input is not a terminal device."<<OS::ErrorNo());
//}
}
#endif
return ret;
}
/*******************************************************************************
* Function : ReadChar
* Description: 读取串口接收缓冲区中len长度字节的数据
* Calls : 见函数实现
* Called By :
* Input : 无
* Output : 无
* Return :
*******************************************************************************/
SInt32 CSerialPort::Read(UChar *cRecved, const SIn
没有合适的资源?快使用搜索试试~ 我知道了~
串口转HTTP协议发送数据工具源码
共10个文件
cpp:3个
h:2个
vcxproj:1个
1 下载量 164 浏览量
2023-11-15
16:30:28
上传
评论 1
收藏 1.69MB ZIP 举报
温馨提示
工具可以将读到的串口数据发送到配置的云端HTTP服务器,实现串口数据转HTTP协议数据,并且HTTP协议头,以及HTTP协议包体都可以自定义。
资源推荐
资源详情
资源评论
收起资源包目录
ston.zip (10个子文件)
ston
serialport.h 7KB
ston.vcxproj 5KB
ston.vcxproj.filters 1KB
comtohttpclude.h 2KB
main.cpp 5KB
comtohttpclude.cpp 7KB
serialport.cpp 18KB
bin
httpformat.xml 156B
stond.exe 4.77MB
ston.vcxproj.user 823B
共 10 条
- 1
资源评论
匠心码农
- 粉丝: 1071
- 资源: 51
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功