| 26 | } |
| 27 | |
| 28 | int COperateSerialPort::Start() |
| 29 | { |
| 30 | if(!m_pTerminal) |
| 31 | return -1; |
| 32 | |
| 33 | slotUpdateParameter(this); |
| 34 | |
| 35 | bool check = false; |
| 36 | check = connect(m_pTerminal, &QTermWidget::sendData, |
| 37 | this, [&](const char* buf, int len){ |
| 38 | if(!m_SerialPort.isOpen()) return; |
| 39 | QByteArray data(buf, len); |
| 40 | //qDebug(log) << "Send data:" << data << data.toHex(':'); |
| 41 | qint64 nLen = m_SerialPort.write(data); |
| 42 | if(GetStats()) |
| 43 | GetStats()->AddSends(nLen); |
| 44 | }); |
| 45 | Q_ASSERT(check); |
| 46 | check = connect(&m_SerialPort, SIGNAL(readyRead()), |
| 47 | this, SLOT(slotReadyRead())); |
| 48 | Q_ASSERT(check); |
| 49 | check = connect(&m_SerialPort, &QSerialPort::errorOccurred, |
| 50 | this, [&](QSerialPort::SerialPortError error) { |
| 51 | qCritical(log) << m_SerialPort.errorString() << error; |
| 52 | if(m_SerialPort.isOpen()) |
| 53 | emit sigError(error, m_SerialPort.errorString()); |
| 54 | }); |
| 55 | Q_ASSERT(check); |
| 56 | |
| 57 | if(-1 == m_Parameter.GetSerialPort() |
| 58 | && m_Parameter.GetSerialPortName().isEmpty()) { |
| 59 | qCritical(log) << "Serial port is invalid"; |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | int index = m_Parameter.GetSerialPort(); |
| 64 | if(0 < index && index < QSerialPortInfo::availablePorts().size()) { |
| 65 | QSerialPortInfo info = QSerialPortInfo::availablePorts().at(index); |
| 66 | if(info.portName() == m_Parameter.GetSerialPortName()) |
| 67 | m_SerialPort.setPort(info); |
| 68 | else |
| 69 | m_SerialPort.setPortName(m_Parameter.GetSerialPortName()); |
| 70 | } else { |
| 71 | if(m_Parameter.GetSerialPortName().isEmpty()) { |
| 72 | qCritical(log) << "Serial port is invalid"; |
| 73 | return -1; |
| 74 | } |
| 75 | else |
| 76 | m_SerialPort.setPortName(m_Parameter.GetSerialPortName()); |
| 77 | } |
| 78 | m_SerialPort.setBaudRate(m_Parameter.GetBaudRate()); |
| 79 | m_SerialPort.setParity(m_Parameter.GetParity()); |
| 80 | m_SerialPort.setDataBits(m_Parameter.GetDataBits()); |
| 81 | m_SerialPort.setStopBits(m_Parameter.GetStopBits()); |
| 82 | m_SerialPort.setFlowControl(m_Parameter.GetFlowControl()); |
| 83 | if(!m_SerialPort.open(QIODevice::ReadWrite)) { |
| 84 | QString szError; |
| 85 | szError = tr("Open Serial port [%1] fail errno[%2]: %3"). |
nothing calls this directly
no test coverage detected