-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathextserialportmodbusdevice.cpp
131 lines (109 loc) · 2.89 KB
/
extserialportmodbusdevice.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "extserialportmodbusdevice.h"
#include <QDebug>
ExtSerialPortModbusDevice::ExtSerialPortModbusDevice(QObject *parent)
: AbstractModbusDevice(parent)
{
initDevice();
}
ExtSerialPortModbusDevice::ExtSerialPortModbusDevice(const QString& spName,
int spBaudRate, QObject *parent)
: AbstractModbusDevice(parent)
{
m_spName = spName;
m_nBaudrate = spBaudRate;
initDevice();
}
void ExtSerialPortModbusDevice::setSerialPort(const QString& spName)
{
m_spName = spName;
}
bool ExtSerialPortModbusDevice::setBaudrate(int nBaudrate)
{
if (!AbstractModbusDevice::setBaudrate(nBaudrate))
return false;
if (isOpen())
{
m_serialPort.setBaudRate2(nBaudrate);
}
return true;
}
bool ExtSerialPortModbusDevice::setDataBits(ModbusDataBits dataBits)
{
if (!AbstractModbusDevice::setDataBits(dataBits))
return false;
if (isOpen())
{
m_serialPort.setDataBits((DataBitsType)dataBits);
}
return true;
}
bool ExtSerialPortModbusDevice::setStopBits(ModbusStopBits stopBits)
{
if (!AbstractModbusDevice::setStopBits(stopBits))
return false;
if (isOpen())
{
m_serialPort.setStopBits((StopBitsType)stopBits);
}
return true;
}
bool ExtSerialPortModbusDevice::setParityBits(ModbusParityBits parityBits)
{
if (!AbstractModbusDevice::setParityBits(parityBits))
return false;
if (isOpen())
{
m_serialPort.setParity((ParityType)parityBits);
}
return true;
}
bool ExtSerialPortModbusDevice::open()
{
close();
m_serialPort.setPortName(m_spName);
if (!m_serialPort.open(QIODevice::ReadWrite))
{
qWarning()<<QString("serialPort %1 open failed")
.arg(m_spName);
return false;
}
m_serialPort.setBaudRate2(m_nBaudrate);
m_serialPort.setDataBits((DataBitsType)m_nDataBits);
m_serialPort.setStopBits((StopBitsType)m_nStopBits);
m_serialPort.setParity((ParityType)m_nParityBits);
m_serialPort.setFlowControl(FLOW_OFF);
m_isOpened = true;
return true;
}
void ExtSerialPortModbusDevice::close()
{
if (m_serialPort.isOpen())
{
m_serialPort.close();
}
m_isOpened = false;
}
int ExtSerialPortModbusDevice::recvData(char *buf, int maxSize)
{
//检查指针buf是否为空
int nBytes = (int)m_serialPort.bytesAvailable();
if (nBytes > 0)
{
if (m_serialPort.lastError() == E_RECEIVE_PARITY_ERROR)
{
m_serialPort.read(buf, qMin(nBytes, maxSize));
qDebug()<<"E_RECEIVE_PARITY_ERROR";
return 0;
}
return m_serialPort.read(buf, qMin(nBytes, maxSize));
}
return 0;
}
int ExtSerialPortModbusDevice::sendData(const QByteArray &datas)
{
return m_serialPort.write(datas);
}
void ExtSerialPortModbusDevice::initDevice()
{
m_serialPort.setTimeout(0, 500);
}