涓插彛灏佽
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了涓插彛灏佽相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/private' title='private'>private
stat 鎸囦护 slots 鍙栨暟 nec bre har app1銆佸ご鏂囦欢
#ifndef MYSERIAL_H #define MYSERIAL_H#include <QSerialPort> #include "mymethod.h" class MySerial : public QSerialPort { Q_OBJECT public: MySerial(QObject *parent); ~MySerial(); private: QSerialPort* serialPort = NULL;//涓插彛瀵硅薄 QByteArray HEAD;//鎶ユ枃澶?/span> void init();//鍒濆鍖?/span>private slots: void openSlot(SerialPortInfo serialPortInfo);//杩炴帴鍑芥暟 void rcvSlot();//鎺ユ敹鏁版嵁妲藉嚱鏁?/span> void sendSlot(QByteArray ba);//鍙戦€佹暟鎹Ы鍑芥暟 void closeSlot();//鍏抽棴妲藉嚱鏁?/span> signals: void serialStateSignal(QString info);//鍚戝鍙戝嚭鐨勮皟璇曚俊鎭?/span> void getPacketSignal(int type, QByteArray ba);//tcp灏嗕笉鍚岀殑鍖呭彂鍑哄幓 }; #endif // MYSERIAL_H
2銆佹簮鏂囦欢
#include "myserial.h" MySerial::MySerial(QObject *parent) : QSerialPort(parent) { this->init(); } MySerial::~MySerial() { } /*****************************************************************/ //浣滆€?鏈卞皬鍕? //鍑芥暟鍚嶇О:鍒濆鍖? //鍑芥暟鍙傛暟:NULL //鍑芥暟杩斿洖鍊?NULL //鍑芥暟浣滅敤:NULL //澶囨敞:NULL /*****************************************************************/ void MySerial::init() { HEAD.resize(2); HEAD[0] = 0x55; HEAD[1] = 0xAA; } /*****************************************************************/ //浣滆€?鏈卞皬鍕? //鍑芥暟鍚嶇О:鎵撳紑涓插彛 //鍑芥暟鍙傛暟:NULL //鍑芥暟杩斿洖鍊?NULL //鍑芥暟浣滅敤:NULL //澶囨敞:NULL /*****************************************************************/ void MySerial::openSlot(SerialPortInfo serialPortInfo) { if (NULL == serialPort) { serialPort = new QSerialPort(); QObject::connect(serialPort, SIGNAL(readyRead()), this, SLOT(rcvSlot())); } if (serialPort->isOpen()) { return; } serialPort->setPortName(serialPortInfo.portName); serialPort->setBaudRate(serialPortInfo.baudRate, QSerialPort::AllDirections); serialPort->setDataBits(serialPortInfo.dataBits);//鏁版嵁浣?/span> serialPort->setParity(serialPortInfo.parity);//鏍¢獙浣?/span> serialPort->setStopBits(serialPortInfo.stopBits);//鍋滄浣?/span> serialPort->setFlowControl(QSerialPort::NoFlowControl);//娴佹帶鍒?/span> if (serialPort->open(QIODevice::ReadWrite))//鎵撳紑涓插彛 { emit serialStateSignal("涓插彛鎵撳紑鎴愬姛..."); } else { emit serialStateSignal("涓插彛鎵撳紑澶辫触!!!"); } } /*****************************************************************/ //浣滆€?鏈卞皬鍕? //鍑芥暟鍚嶇О:鎺ユ敹鏁版嵁 //鍑芥暟鍙傛暟:NULL //鍑芥暟杩斿洖鍊?NULL //鍑芥暟浣滅敤:NULL //澶囨敞:NULL /*****************************************************************/ void MySerial::rcvSlot() { static QByteArray allBa;//鐢ㄦ潵瀛樻墍鏈夋枃浠?/span> allBa.append(serialPort->readAll());//璇诲彇鏁版嵁 int head = allBa.indexOf(HEAD);//鎶ユ枃澶寸殑浣嶇疆 uint16_t length = 0;//瀛橀暱搴﹀瓧鑺?/span> QByteArray currentBa; int id = 0; while (-1 != head) { allBa = allBa.mid(head);//鍘绘帀鎶ュご涔嬪墠鐨?/span> memcpy(&length, allBa.data() + 2, 2); if (allBa.size() >= length)//闀垮害澶?杩涜瑙f瀽 { currentBa = allBa.mid(0, length);//鑾峰彇褰撳墠瀹屾垚鍖?/span> id = Mymethod::getPacketType(currentBa); if (-1 != id) { emit getPacketSignal(id, currentBa); } allBa = allBa.mid(2); } else { break; } head = allBa.indexOf(HEAD);//鍒锋柊鎶ユ枃澶寸殑浣嶇疆 } } /*****************************************************************/ //浣滆€?鏈卞皬鍕? //鍑芥暟鍚嶇О:鍙戦€? //鍑芥暟鍙傛暟:NULL //鍑芥暟杩斿洖鍊?NULL //鍑芥暟浣滅敤:NULL //澶囨敞:NULL /*****************************************************************/ void MySerial::sendSlot(QByteArray ba) { if (NULL == serialPort) { emit serialStateSignal("涓插彛鏈垵濮嬪寲!!!"); return; } if (!serialPort->isOpen()) { emit serialStateSignal("涓插彛鏈墦寮€!!!"); return; } if (ba.size() != serialPort->write(ba)) { emit serialStateSignal("鎸囦护鍙戦€佸け璐?!!"); } else { emit serialStateSignal("鍙戦€佹寚浠ゆ垚鍔?.."); } } /*****************************************************************/ //浣滆€?鏈卞皬鍕? //鍑芥暟鍚嶇О:鍏抽棴 //鍑芥暟鍙傛暟:NULL //鍑芥暟杩斿洖鍊?NULL //鍑芥暟浣滅敤:NULL //澶囨敞:NULL /*****************************************************************/ void MySerial::closeSlot() { if (NULL == serialPort) { return; } serialPort->close(); emit serialStateSignal("宸插叧闂覆鍙?.."); }
3銆佽嚜瀹氫箟鍑芥暟锛岃幏鍙栨姤鏂囩被鍨?/p>
int Mymethod::getPacketType(const QByteArray& ba) { int result = -1; uint16_t length = 0; if ((ba[0] == (char)0x55) && (ba[1] == (char)0xAA))//鎶ユ枃澶存纭?/span> { memcpy(&length, ba.data() + 2, 2); if (length == ba.size())//闀垮害姝g‘ { QByteArray tempBa; tempBa.resize(ba.size() - 1); memcpy(tempBa.data(), ba.data(), ba.size() - 1); char crc = Mymethod::CRC(tempBa); if (crc == ba[ba.size() - 1])//CRC姝g‘ { result = (int)ba[4]; } } } return result; }
以上是关于涓插彛灏佽的主要内容,如果未能解决你的问题,请参考以下文章