//#include <stdio.h>
//#include <Process.h>
//#include <QCoreApplication>
//#include <QEvent>
#include <QReadWriteLock>
#include "win_qextserialport.h"
/*!
\fn Win_QextSerialPort::Win_QextSerialPort()
Default constructor. Note that the name of the device used by a Win_QextSerialPort constructed
with this constructor will be determined by #defined constants, or lack thereof - the default
behavior is the same as _TTY_LINUX_. Possible naming conventions and their associated constants
are:
\verbatim
Constant Used By Naming Convention
---------- ------------- ------------------------
_TTY_WIN_ Windows COM1, COM2
_TTY_IRIX_ SGI/IRIX /dev/ttyf1, /dev/ttyf2
_TTY_HPUX_ HP-UX /dev/tty1p0, /dev/tty2p0
_TTY_SUN_ SunOS/Solaris /dev/ttya, /dev/ttyb
_TTY_DIGITAL_ Digital UNIX /dev/tty01, /dev/tty02
_TTY_FREEBSD_ FreeBSD /dev/ttyd0, /dev/ttyd1
_TTY_LINUX_ Linux /dev/ttyS0, /dev/ttyS1
<none> Linux /dev/ttyS0, /dev/ttyS1
\endverbatim
This constructor associates the object with the first port on the system, e.g. COM1 for Windows
platforms. See the other constructor if you need a port other than the first.
*/
Win_QextSerialPort::Win_QextSerialPort():
QextSerialBase()
{
Win_Handle=INVALID_HANDLE_VALUE;
init();
}
/*!Win_QextSerialPort::Win_QextSerialPort(const Win_QextSerialPort&)
Copy constructor.
*/
Win_QextSerialPort::Win_QextSerialPort(const Win_QextSerialPort& s):
QextSerialBase(s.port)
{
Win_Handle=INVALID_HANDLE_VALUE;
_queryMode = s._queryMode;
_bytesToWrite = s._bytesToWrite;
bytesToWriteLock = new QReadWriteLock;
overlapThread = new Win_QextSerialThread(this);
memcpy(& overlap, & s.overlap, sizeof(OVERLAPPED));
memcpy(& overlapWrite, & s.overlapWrite, sizeof(OVERLAPPED));
setOpenMode(s.openMode());
lastErr=s.lastErr;
port = s.port;
Settings.FlowControl=s.Settings.FlowControl;
Settings.Parity=s.Settings.Parity;
Settings.DataBits=s.Settings.DataBits;
Settings.StopBits=s.Settings.StopBits;
Settings.BaudRate=s.Settings.BaudRate;
Win_Handle=s.Win_Handle;
memcpy(&Win_CommConfig, &s.Win_CommConfig, sizeof(COMMCONFIG));
memcpy(&Win_CommTimeouts, &s.Win_CommTimeouts, sizeof(COMMTIMEOUTS));
if (s.overlapThread->isRunning())
overlapThread->start();
}
/*!
\fn Win_QextSerialPort::Win_QextSerialPort(const QString & name)
Constructs a serial port attached to the port specified by devName.
devName is the name of the device, which is windowsystem-specific,
e.g."COM2" or "/dev/ttyS0".
*/
Win_QextSerialPort::Win_QextSerialPort(const QString & name, QextSerialBase::QueryMode mode):
QextSerialBase(name)
{
Win_Handle=INVALID_HANDLE_VALUE;
setQueryMode(mode);
init();
}
/*!
\fn Win_QextSerialPort::Win_QextSerialPort(const PortSettings& settings)
Constructs a port with default name and specified settings.
*/
Win_QextSerialPort::Win_QextSerialPort(const PortSettings& settings, QextSerialBase::QueryMode mode) {
Win_Handle=INVALID_HANDLE_VALUE;
setBaudRate(settings.BaudRate);
setDataBits(settings.DataBits);
setStopBits(settings.StopBits);
setParity(settings.Parity);
setFlowControl(settings.FlowControl);
setTimeout(settings.Timeout_Millisec);
setQueryMode(mode);
init();
}
/*!
\fn Win_QextSerialPort::Win_QextSerialPort(const QString & name, const PortSettings& settings)
Constructs a port with specified name and settings.
*/
Win_QextSerialPort::Win_QextSerialPort(const QString & name, const PortSettings& settings, QextSerialBase::QueryMode mode) {
Win_Handle=INVALID_HANDLE_VALUE;
setPortName(name);
setBaudRate(settings.BaudRate);
setDataBits(settings.DataBits);
setStopBits(settings.StopBits);
setParity(settings.Parity);
setFlowControl(settings.FlowControl);
setTimeout(settings.Timeout_Millisec);
setQueryMode(mode);
init();
}
void Win_QextSerialPort::init()
{
_bytesToWrite = 0;
overlap.Internal = 0;
overlap.InternalHigh = 0;
overlap.Offset = 0;
overlap.OffsetHigh = 0;
overlap.hEvent = CreateEvent(NULL, true, false, NULL);
overlapThread = new Win_QextSerialThread(this);
bytesToWriteLock = new QReadWriteLock;
}
/*!
\fn Win_QextSerialPort::~Win_QextSerialPort()
Standard destructor.
*/
Win_QextSerialPort::~Win_QextSerialPort() {
if (isOpen()) {
close();
}
CloseHandle(overlap.hEvent);
delete overlapThread;
delete bytesToWriteLock;
}
/*!
\fn Win_QextSerialPort& Win_QextSerialPort::operator=(const Win_QextSerialPort& s)
overrides the = operator
*/
Win_QextSerialPort& Win_QextSerialPort::operator=(const Win_QextSerialPort& s) {
setOpenMode(s.openMode());
_queryMode = s._queryMode;
_bytesToWrite = s._bytesToWrite;
bytesToWriteLock = new QReadWriteLock;
overlapThread = new Win_QextSerialThread(this);
memcpy(& overlap, & s.overlap, sizeof(OVERLAPPED));
memcpy(& overlapWrite, & s.overlapWrite, sizeof(OVERLAPPED));
lastErr=s.lastErr;
port = s.port;
Settings.FlowControl=s.Settings.FlowControl;
Settings.Parity=s.Settings.Parity;
Settings.DataBits=s.Settings.DataBits;
Settings.StopBits=s.Settings.StopBits;
Settings.BaudRate=s.Settings.BaudRate;
Win_Handle=s.Win_Handle;
memcpy(&Win_CommConfig, &s.Win_CommConfig, sizeof(COMMCONFIG));
memcpy(&Win_CommTimeouts, &s.Win_CommTimeouts, sizeof(COMMTIMEOUTS));
if (s.overlapThread->isRunning())
overlapThread->start();
return *this;
}
/*!
\fn bool Win_QextSerialPort::open(OpenMode mode)
Opens a serial port. Note that this function does not specify which device to open. If you need
to open a device by name, see Win_QextSerialPort::open(const char*). This function has no effect
if the port associated with the class is already open. The port is also configured to the current
settings, as stored in the Settings structure.
*/
bool Win_QextSerialPort::open(OpenMode mode) {
unsigned long confSize = sizeof(COMMCONFIG);
Win_CommConfig.dwSize = confSize;
DWORD dwFlagsAndAttributes = 0;
if (queryMode() == QextSerialBase::EventDriven)
dwFlagsAndAttributes += FILE_FLAG_OVERLAPPED;
LOCK_MUTEX();
if (mode == QIODevice::NotOpen)
return isOpen();
if (!isOpen()) {
/*open the port*/ /////////被修改过/////////
Win_Handle=CreateFileA(port.toLocal8Bit(), GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, dwFlagsAndAttributes, NULL);
if (Win_Handle!=INVALID_HANDLE_VALUE) {
/*configure port settings*/
GetCommConfig(Win_Handle, &Win_CommConfig, &confSize);
GetCommState(Win_Handle, &(Win_CommConfig.dcb));
/*set up parameters*/
Win_CommConfig.dcb.fBinary=TRUE;
Win_CommConfig.dcb.fInX=FALSE;
Win_CommConfig.dcb.fOutX=FALSE;
Win_CommConfig.dcb.fAbortOnError=FALSE;
Win_CommConfig.dcb.fNull=FALSE;
setBaudRate(Settings.BaudRate);
setDataBits(Settings.DataBits);
setStopBits(Settings.StopBits);
setParity(Settings.Parity);
setFlowControl(Settings.FlowControl);
setTimeout(Settings.Timeout_Millisec);
SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG));
//init event driven approach
if (queryMode() == QextSerialBase::EventDriven) {
Win_CommTimeouts.ReadIntervalTimeout = MAXDWORD;
Win_CommTimeouts.ReadTotalTimeoutMultiplier = 0;
Win_CommTimeouts.ReadTotalTimeoutConstant = 0;
Win_CommTimeouts.WriteTotalTimeoutMultiplier = 0;
Win_CommTimeouts.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(W
ojylijia
- 粉丝: 7
- 资源: 7
最新资源
- 飞思卡尔,整车VCUsimulink源码 上下电管理 扭矩控制 故障处理 适合想了解整车控制的工程师们,学习参考
- c#联合halcon机器视觉通用视觉框架2 流程化开发 缺陷检测,定位,测量,OCR识别 拉控件式
- delta机械臂,delta机器人,运动控制器,运动控制卡 本卡采用前瞻运动轨迹规划,运动采用G代码指令编程,具有G5三维空间的圆弧插补,空间直线插补功能,子程序编程功能,逻辑判断语句功能,示教编程功
- AD源文件及Verilog程序源码,包含AD的PCB源码和quartus程序源码,板子为四层板,程序为verilog,主控芯片为fpga 功能包括千兆以太网通讯(RTL8211EG芯片),micro
- 三菱PLC与两台变频器通讯控制三菱PLC通过MODBUS控制两台士林变频器程序,PLC可以用3U或者3G
- 设计区域电网输电线路高低定值闭锁式方向pscad仿真模型 功率方向元件采用90度接线,低定值启动发信,低定值启动后,如果满足高定值动作条件,保护动作,跳开线路两侧断路器 设计区域电网输电线路复合电压
- 深度强化学习DQN车间排产调度优化算法+gym环境(python代码)
- Java面试题(全栈) 完整版.md
- 三菱FX3U两轴标准程序,XZ两轴,包含轴点动,回零,相对与绝对定位,只要弄明白这个程序,就可以非常了解整个项目的程序如何去编写,从哪里开始下手
- BLDC无刷直流电机和PMSM永磁同步电机 可提供所有代码中所有算法的,每个代码都亲自验证过 基于STM32F1的有传感器和无传感驱动 直流无刷电机有传感器和无传感驱动程序, 无传感的实现是基于反电
- 移远EC800K-cn AT命令手册
- usb协议中文,个人学习整理,仅供参考
- IPOP4.1.exe
- 两阶段鲁棒优化程序 本程序采用微网为模型,主要将安装成本、运营成本以及综合效益三个方面纳入考虑范围,建立两阶段鲁棒优化模型,采用的是CCG方法,本程序为matlab编制
- C#上位机 APP监控西门子S7-1200 C#全套源代码 1,C#开发上位机手机APP,自己写的程序可提供部分 2,通过VS2019开发安卓手机app 3,全套源代码,现场运行设备实测有效 4
- MATLAB代码:考虑过网费用分摊的多产消者点对点能源交易分布式优化 关键词:点对点P2P交易 过网费用 分布式优化 ADMM 交替方向乘子法 仿真平台:MATLAB+自带的优化工具箱 主要内容
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈