- Setting Serial Port Baud Rate
- Qt Serial Port Baud Rates
- Baud Rates For Serial Ports
- Pc Serial Port Baud Rate
- Serial Baud Rates
QT Serial Port Reading. This makes the calling thread unable to do anything until new data arrives on this serial port. If this thread was a GUI thread, This will.
Setting Serial Port Baud Rate
Permalink![Qt serial port baud rate Qt serial port baud rate](/uploads/1/2/6/2/126207373/123896134.png)
Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign up Find file Copy path
04a52ebDec 21, 2017
1 contributor
![Common serial port baud rates Common serial port baud rates](/uploads/1/2/6/2/126207373/137538739.png)
#!/usr/bin/env python |
# coding: utf-8 |
# Serial 설정과 동작을 관리하는 모듈 |
import sys |
from PyQt5.QtWidgets import QWidget |
from PyQt5.QtWidgets import QBoxLayout |
from PyQt5.QtWidgets import QGridLayout |
from PyQt5.QtWidgets import QLabel |
from PyQt5.QtWidgets import QComboBox |
from PyQt5.QtWidgets import QGroupBox |
from PyQt5.QtWidgets import QApplication |
from PyQt5.QtCore import Qt |
from PyQt5.QtCore import QThread |
from PyQt5.QtSerialPort import QSerialPort |
from PyQt5.QtSerialPort import QSerialPortInfo |
from PyQt5.QtCore import QIODevice |
from PyQt5.QtCore import QWaitCondition |
from PyQt5.QtCore import QMutex |
from PyQt5.QtCore import QByteArray |
from PyQt5.QtCore import pyqtSlot |
from PyQt5.QtCore import pyqtSignal |
__author__ ='Deokyu Lim <[email protected]>' |
__platform__ = sys.platform |
classSerialReadThread(QThread): |
'' |
시리얼 연결이 성공하면 항상 데이터를 수신할 수 있어야 하므로 |
스레드로 만들어야 한다. |
'' |
# 사용자 정의 시그널 선언 |
# 받은 데이터 그대로를 전달 해주기 위해 QByteArray 형태로 전달 |
received_data = pyqtSignal(QByteArray, name='receivedData') |
def__init__(self, serial): |
QThread.__init__(self) |
self.cond = QWaitCondition() |
self._status =False |
self.mutex = QMutex() |
self.serial = serial |
def__del__(self): |
self.wait() |
defrun(self): |
'' |
들어온 데이터가 있다면 시그널을 발생 |
:return: |
'' |
whileTrue: |
self.mutex.lock() |
ifnotself._status: |
self.cond.wait(self.mutex) |
buf =self.serial.readAll() |
if buf: |
self.received_data.emit(buf) |
self.usleep(1) |
self.mutex.unlock() |
deftoggle_status(self): |
self._status =notself._status |
ifself._status: |
self.cond.wakeAll() |
@pyqtSlot(bool, name='setStatus') |
defset_status(self, status): |
self._status = status |
ifself._status: |
self.cond.wakeAll() |
classSerialController(QWidget): |
# 시리얼포트 상수 값 |
BAUDRATES= ( |
QSerialPort.Baud1200, |
QSerialPort.Baud2400, |
QSerialPort.Baud4800, |
QSerialPort.Baud9600, |
QSerialPort.Baud19200, |
QSerialPort.Baud38400, |
QSerialPort.Baud57600, |
QSerialPort.Baud115200, |
) |
DATABITS= ( |
QSerialPort.Data5, |
QSerialPort.Data6, |
QSerialPort.Data7, |
QSerialPort.Data8, |
) |
FLOWCONTROL= ( |
QSerialPort.NoFlowControl, |
QSerialPort.HardwareControl, |
QSerialPort.SoftwareControl, |
) |
PARITY= ( |
QSerialPort.NoParity, |
QSerialPort.EvenParity, |
QSerialPort.OddParity, |
QSerialPort.SpaceParity, |
QSerialPort.MarkParity, |
) |
STOPBITS= ( |
QSerialPort.OneStop, |
QSerialPort.OneAndHalfStop, |
QSerialPort.TwoStop, |
) |
received_data = pyqtSignal(QByteArray, name='receivedData') |
sent_data = pyqtSignal(str, name='sentData') |
def__init__(self): |
QWidget.__init__(self, flags=Qt.Widget) |
# 위젯 선언 |
self.gb = QGroupBox(self.tr('Serial')) |
self.cb_port = QComboBox() |
self.cb_baud_rate = QComboBox() |
self.cb_data_bits = QComboBox() |
self.cb_flow_control = QComboBox() |
self.cb_parity = QComboBox() |
self.cb_stop_bits = QComboBox() |
# 시리얼 인스턴스 생성 |
# 시리얼 스레드 설정 및 시작 |
self.serial = QSerialPort() |
self.serial_info = QSerialPortInfo() |
self.serial_read_thread = SerialReadThread(self.serial) |
self.serial_read_thread.received_data.connect(lambdav: self.received_data.emit(v)) |
self.serial_read_thread.start() |
self.init_widget() |
definit_widget(self): |
self.setWindowTitle('Serial Controller') |
layout = QBoxLayout(QBoxLayout.TopToBottom, parent=self) |
grid_box = QGridLayout() |
grid_box.addWidget(QLabel(self.tr('Port')), 0, 0) |
grid_box.addWidget(self.cb_port, 0, 1) |
grid_box.addWidget(QLabel(self.tr('Baud Rate')), 1, 0) |
grid_box.addWidget(self.cb_baud_rate, 1, 1) |
grid_box.addWidget(QLabel(self.tr('Data Bits')), 2, 0) |
grid_box.addWidget(self.cb_data_bits, 2, 1) |
grid_box.addWidget(QLabel(self.tr('Flow Control')), 3, 0) |
grid_box.addWidget(self.cb_flow_control, 3, 1) |
grid_box.addWidget(QLabel(self.tr('Parity')), 4, 0) |
grid_box.addWidget(self.cb_parity, 4, 1) |
grid_box.addWidget(QLabel(self.tr('Stop Bits')), 5, 0) |
grid_box.addWidget(self.cb_stop_bits, 5, 1) |
self._fill_serial_info() |
self.gb.setLayout(grid_box) |
layout.addWidget(self.gb) |
self.setLayout(layout) |
def_fill_serial_info(self): |
# 시리얼 상수 값들을 위젯에 채운다 |
self.cb_port.insertItems(0, self._get_available_port()) |
self.cb_baud_rate.insertItems(0, [str(x) for x inself.BAUDRATES]) |
self.cb_data_bits.insertItems(0, [str(x) for x inself.DATABITS]) |
flow_name = {0: 'None', 1: 'Hardware', 2: 'Software'} |
self.cb_flow_control.insertItems(0, [flow_name[x] for x inself.FLOWCONTROL]) |
parity_name = {0: 'None', 2: 'Even', 3: 'Odd', 4: 'Space', 5: 'Mark'} |
self.cb_parity.insertItems(0, [parity_name[x] for x inself.PARITY]) |
stop_bits_name = {1: '1', 3: '1.5', 2: '2'} |
self.cb_stop_bits.insertItems(0, [stop_bits_name[x] for x inself.STOPBITS]) |
@staticmethod |
defget_port_path(): |
'' |
현재플래폼에 맞게 경로 또는 지정어를 반환 |
:return: |
'' |
return {'linux': '/dev/ttyS', 'win32': 'COM'}[__platform__] |
def_get_available_port(self): |
'' |
255개의 포트를 열고 닫으면서 사용가능한 포트를 찾아서 반환 |
:return: |
'' |
available_port =list() |
port_path =self.get_port_path() |
for number inrange(255): |
port_name = port_path +str(number) |
ifnotself._open(port_name): |
continue |
available_port.append(port_name) |
self.serial.close() |
return available_port |
def_open(self, port_name, baudrate=QSerialPort.Baud9600, data_bits=QSerialPort.Data8, |
flow_control=QSerialPort.NoFlowControl, parity=QSerialPort.NoParity, stop_bits=QSerialPort.OneStop): |
'' |
인자값으로 받은 시리얼 접속 정보를 이용하여 해당 포트를 연결한다. |
:param port_name: |
:param baudrate: |
:param data_bits: |
:param flow_control: |
:param parity: |
:param stop_bits: |
:return: bool |
'' |
info = QSerialPortInfo(port_name) |
self.serial.setPort(info) |
self.serial.setBaudRate(baudrate) |
self.serial.setDataBits(data_bits) |
self.serial.setFlowControl(flow_control) |
self.serial.setParity(parity) |
self.serial.setStopBits(stop_bits) |
returnself.serial.open(QIODevice.ReadWrite) |
defconnect_serial(self): |
serial_info = { |
'port_name': self.cb_port.currentText(), |
'baudrate': self.BAUDRATES[self.cb_baud_rate.currentIndex()], |
'data_bits': self.DATABITS[self.cb_data_bits.currentIndex()], |
'flow_control': self.FLOWCONTROL[self.cb_flow_control.currentIndex()], |
'parity': self.PARITY[self.cb_parity.currentIndex()], |
'stop_bits': self.STOPBITS[self.cb_stop_bits.currentIndex()], |
} |
status =self._open(**serial_info) |
self.serial_read_thread.setStatus(status) |
return status |
defdisconnect_serial(self): |
returnself.serial.close() |
@pyqtSlot(bytes, name='writeData') |
defwrite_data(self, data): |
self.serial.writeData(data) |
classForm(QWidget): |
'' |
테스트용도의 단독 실행때만 사용하는 폼 |
'' |
def__init__(self): |
QWidget.__init__(self, flags=Qt.Widget) |
self.te = QTextEdit() |
self.pb = QPushButton('Connect') |
self.pb_send = QPushButton('Send') |
self.serial = SerialController() |
self.init_widget() |
definit_widget(self): |
'' |
현재 위젯의 모양등을 초기화 |
'' |
self.setWindowTitle('Hello World') |
form_lbx = QBoxLayout(QBoxLayout.TopToBottom, parent=self) |
self.setLayout(form_lbx) |
self.pb.clicked.connect(self.slot_clicked_connect_button) |
self.serial.received_data.connect(self.read_data) |
test_data =bytes([0x02]) +bytes('TEST DATA', 'utf-8') +bytes([0x03]) |
self.pb_send.clicked.connect(lambda: self.serial.writeData(test_data)) |
form_lbx.addWidget(self.serial) |
form_lbx.addWidget(self.te) |
form_lbx.addWidget(self.pb_send) |
form_lbx.addWidget(self.pb) |
# 많이 사용하는 옵션을 미리 지정해 둔다. |
# 9600 8N1 |
self.serial.cb_baud_rate.setCurrentIndex(3) |
self.serial.cb_data_bits.setCurrentIndex(3) |
@pyqtSlot(QByteArray, name='readData') |
defread_data(self, rd): |
self.te.insertPlainText(str(rd, 'ascii', 'replace')) |
@pyqtSlot(name='clickedConnectButton') |
defslot_clicked_connect_button(self): |
ifself.serial.serial.isOpen(): |
self.serial.disconnect_serial() |
else: |
self.serial.connect_serial() |
self.pb.setText({False: 'Connect', True: 'Disconnect'}[self.serial.serial.isOpen()]) |
if__name__'__main__': |
from PyQt5.QtWidgets import QPushButton |
from PyQt5.QtWidgets import QTextEdit |
app = QApplication(sys.argv) |
excepthook = sys.excepthook |
sys.excepthook =lambdat, val, tb: excepthook(t, val, tb) |
form = Form() |
form.show() |
exit(app.exec_()) |
Copy lines Copy permalink
Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Qt Serial Port Baud Rates
Sign up Find file Copy path
samkristoffResolving Mac/Win differences1b90649Jun 30, 2017
Baud Rates For Serial Ports
2 contributors
Pc Serial Port Baud Rate
#include'serial.h' |
#include<QCoreApplication> |
#include<QMutex> |
#include<QTime> |
#include<QThread> |
Serial::Serial(QObject *parent) : QObject(parent) |
{ |
qDebug() << 'Serial::Serial()' << 'thread: ' << QThread::currentThread(); |
this->port = newQSerialPort(this); |
} |
Serial::~Serial() { |
qDebug() << 'Serial::~Serial() - Begin' << 'thread: ' << QThread::currentThread(); |
close(); |
deletethis->port; |
qDebug() << 'Serial::~Serial() - End' << 'thread: ' << QThread::currentThread(); |
} |
//Open the specified port with the specified baud rate. Returns true if successful, false otherwise. |
boolSerial::open(QString portName, qint32 baudRate){ |
//QSerialPort port; |
this->port->setPortName(portName); |
//Set the baud rate to 9600 before opening to grease the wheels for Mac |
this->setBaudRate(9600); |
if(this->port->open(QIODevice::ReadWrite)) { |
//Update to the actual desired baud rate |
this->port->setBaudRate(baudRate); |
returntrue; |
} else { |
returnfalse; |
} |
} |
//Open the specified port with the specified baud rate. Returns true if successful, false otherwise. |
boolSerial::open(QSerialPortInfo portInfo, qint32 baudRate){ |
//QSerialPort port; |
this->port->setPort(portInfo); |
//Set the baud rate to 9600 before opening to grease the wheels for Mac |
this->setBaudRate(9600); |
if(this->port->open(QIODevice::ReadWrite)) { |
//Update to the actual desired baud rate |
this->port->setBaudRate(baudRate); |
returntrue; |
} else { |
qDebug() << 'Failed to open' << portInfo.portName() << ' : ' << this->port->error(); |
returnfalse; |
} |
} |
//Returns true if the serial port is open |
boolSerial::isOpen() { |
returnthis->port->isOpen(); |
} |
//Write data to the port. Returns true if numBytes were successfully written to the port, false otherwise. |
boolSerial::write(constchar *data, int numBytes) { |
if(!this->port->isOpen()) |
{ |
returnfalse; |
} |
else |
{ |
if(this->port->write(data, numBytes) != numBytes) |
{ |
returnfalse; |
} |
returntrue; |
} |
} |
//Write data to the port. Returns true if all bytes were successfully written, false otherwise. |
boolSerial::write(QByteArray data) { |
if(!this->port->isOpen()) |
{ |
returnfalse; |
} |
else |
{ |
if(this->port->write(data) data.length()) |
{ |
returntrue; |
} |
returnfalse; |
} |
} |
//Write the specified data to the serial port. Read response data until the number of ms specified in timeout ellaspes between response bytes, then return response data. |
QByteArray Serial::writeRead(QByteArray data, int timeout){ |
returnwriteRead(data, timeout, timeout); |
} |
//Write the specified data to the serial port. Delay up to the specified delay ms for the first response byte, |
//then read response data until the number of ms specified in timeout ellaspes between response bytes, then return response data. |
QByteArray Serial::writeRead(QByteArray data, int delay, int timeout) { |
QByteArray resp; |
this->write(data); |
if(waitForBytesAvailable(1, delay)){ |
//Read first incoming data |
resp.append(this->port->readAll()); |
//Continue reading until timout expires |
while(waitForBytesAvailable(1, timeout)) { |
resp.append(this->port->readAll()); |
} |
return resp; |
} |
return resp; |
} |
//Write the specified data to the serial port. Wait up to delay ms for the first byte to be returned. Return an empty array if delay expires. |
//While data is being returned wait up to timeout ms for the inoming byte. Return data if timeout expires. |
//Return data immediatly if a complete JSON object or complete chunked transfer is detected. |
QByteArray Serial::fastWriteRead(QByteArray data, int delay, int timeout) { |
QMutex mutex; |
mutex.lock(); |
qDebug() << 'Serial::fastWriteRead()' << 'thread: ' << QThread::currentThread() << 'Send:' << data; |
QByteArray resp; |
QTime stopWatch; |
stopWatch.start(); |
//Clear incoming buffer before writing new command |
QByteArray flushedData = this->port->readAll(); |
qDebug() << 'Flushed ' << flushedData.length() << 'in' << stopWatch.elapsed() << 'ms'; |
//Write Command |
this->write(data); |
//Wait for resposne to start |
stopWatch.restart(); |
if(waitForBytesAvailable(1, delay)){ |
qDebug() << 'Waited' << stopWatch.elapsed() << 'ms for first byte'; |
//Read first incoming data |
stopWatch.restart(); |
resp.append(this->port->readAll()); |
qDebug() << 'Took' << stopWatch.elapsed() << 'ms to read first burst'; |
//Checking if incoming data is a JSON object, OSJB, or other. |
stopWatch.restart(); |
if(resp[0] '{') { |
//---------- JSON ---------- |
qDebug() <<'Incoming Data Looks Like JSON'; |
int openBracketCount = 0; |
//Process initial data |
for(int i=0; i< resp.length(); i++) { |
if(resp[i] '{') { |
openBracketCount++; |
} elseif(resp[i] '}') { |
openBracketCount--; |
} |
if(openBracketCount <= 0) { |
qDebug() << 'Serial::fastWriteRead()' << 'thread: ' << QThread::currentThread() << 'Found the end in ' << stopWatch.elapsed() << '- Response:' << resp; |
emit fastWriteReadResponse(resp); |
mutex.unlock(); |
return resp; |
} |
} |
//Continue reading until timeout expires |
stopWatch.restart(); |
while(waitForBytesAvailable(1, timeout)) { |
qDebug() << 'waiting for timeout for' << stopWatch.elapsed() << 'ms'; |
while(this->port->bytesAvailable() > 0) { |
char respByte = this->port->read(1)[0]; |
resp.append(respByte); |
if(respByte '{') { |
openBracketCount++; |
} elseif(respByte '}') { |
openBracketCount--; |
} |
if(openBracketCount <= 0) { |
qDebug() << 'Serial::fastWriteRead()' << 'thread: ' << QThread::currentThread() << 'Response:' << resp; |
emit fastWriteReadResponse(resp); |
mutex.unlock(); |
return resp; |
} |
} |
} |
} |
else |
{ |
//---------- OTHER - Assume Chunked---------- |
//Clear any leading, non-chunked data |
while((resp.length()) > 0 && (!(resp[0] >= '0' && resp[0] <= '9') && !(resp[0] >= 'a' && resp[0] <= 'f') && !(resp[0] >= 'A' && resp[0] <= 'F'))) |
{ |
qDebug() << 'Trimming ' << resp[0] << ' from start of response'; |
resp = resp.mid(1); |
} |
//Continue reading until timeout expires |
stopWatch.restart(); |
while(waitForBytesAvailable(1, timeout)) { |
qDebug() << 'waiting for timeout for' << stopWatch.elapsed() << 'ms'; |
while(this->port->bytesAvailable() > 0) { |
char respByte = this->port->read(1)[0]; |
resp.append(respByte); |
//Check if chunk is done |
if(respByte 'n') { |
if(validChunkedData(resp)) |
{ |
qDebug() << '----------Chunked Transfer Complete----------'; |
emit fastWriteReadResponse(resp); |
mutex.unlock(); |
return resp; |
} |
} |
} |
} |
} |
} |
qDebug() << 'Serial::fastWriteRead()' << 'thread: ' << QThread::currentThread() << 'Timeout - Response:' << resp; |
emit fastWriteReadResponse(resp); |
mutex.unlock(); |
return resp; |
} |
//Returns the chunk size if the specified input is in valid chunk format or -1 otherwise |
intSerial::getChunkSize(QString data) { |
int endIndex = data.indexOf('rn'); |
if(endIndex > 0) { |
bool ok = false; |
unsignedint chunkSize = data.left(endIndex).toUInt(&ok, 16); |
if(ok){ |
return chunkSize; |
} else { |
return -1; |
} |
} else { |
return -1; |
} |
} |
//Returns the number of bytes available at the port or -1 if the port is not open |
intSerial::bytesAvailable() { |
QTime stopWatch; |
if(!this->port->isOpen()) |
{ |
return -1; |
} |
else{ |
stopWatch.restart(); |
//This is required on Mac for bytes to be readable. Do not change. |
#if defined(TARGET_OS_MAC) |
this->port->waitForReadyRead(0); |
#else |
this->port->waitForReadyRead(1); |
#endif |
int numBytes = this->port->bytesAvailable(); |
//qDebug() << 'Took' << stopWatch.elapsed() << 'ms' << 'and found' << numBytes << 'bytes'; |
returnthis->port->bytesAvailable(); |
} |
} |
//Wait for the specified number of bytes to be avialable or timeout. Returns true if the specified number of bytes are available, false otherwise. |
boolSerial::waitForBytesAvailable(int numBytes, int timeout) { |
QTime startTime = QTime::currentTime(); |
QTime doneTime = startTime.addMSecs(timeout); |
while(QTime::currentTime() < doneTime) { |
if(bytesAvailable() >= numBytes) { |
returntrue; |
} |
} |
returnfalse; |
} |
/* |
//Read the specified number of bytes from the port into the provided buffer. This function returns true if numBytes were succesfully read and false otherwise. |
bool Serial::read(char* rxBuffer, int numBytes) { |
this->port.waitForReadyRead(0); |
if(!this->port.isOpen()) |
{ |
//Port not open |
return false; |
} |
else if(this->port.bytesAvailable() < numBytes) |
{ |
//Not enough bytes available |
return false; |
} |
else { |
if(this->port.read(rxBuffer, numBytes) < 0){ |
return false; |
} |
return true; |
} |
} |
*/ |
//Read the specified number of bytes from the serial buffer. Data is returned as a byte array. |
QByteArray Serial::read(qint64 numBytes) { |
//This is required on Mac for bytes to be readable. Do not change. |
#if defined(TARGET_OS_MAC) |
this->port->waitForReadyRead(0); |
#else |
this->port->waitForReadyRead(1); |
#endif |
returnthis->port->read(numBytes); |
} |
//Read all available bytes from the serial buffer. Data is returned as a byte array. |
QByteArray Serial::read() { |
if(!this->port->isOpen()) { |
returnNULL; |
} |
QTime stopWatch; |
stopWatch.restart(); |
//This is required on Mac for bytes to be readable. Do not change. |
#if defined(TARGET_OS_MAC) |
this->port->waitForReadyRead(0); |
#else |
this->port->waitForReadyRead(1); |
#endif |
if(stopWatch.elapsed() > 2){ |
qDebug() << 'Read took took long' << stopWatch.elapsed(); |
} |
returnthis->port->readAll(); |
} |
//Close the serial port. |
voidSerial::close() { |
qDebug() << 'Serial::close()' << 'thread: ' << QThread::currentThread(); |
//Set the baud rate back to 9600 before closing to grease the wheels for Mac |
this->setBaudRate(9600); |
if(port->isOpen()) { |
this->port->close(); |
} |
} |
//Refresh the system serial port info and return it. |
QList<QSerialPortInfo> Serial::getSerialPortInfo() { |
returnQSerialPortInfo::availablePorts(); |
} |
//Assert a reset by setting RTS and DTR high for 100ms, DTR low for 50ms then DTR high for 100ms. Returns true on success false otherwise. |
boolSerial::assertReset() { |
//Set RTS and DTR |
if(this->port->setRequestToSend(true) && this->port->setDataTerminalReady(true)){ |
delay(100); |
if(!this->port->setDataTerminalReady(false)) { |
returnfalse; |
} else { |
delay(50); |
if(!this->port->setDataTerminalReady(true)) { |
returnfalse; |
} else { |
delay(100); |
returntrue; |
} |
} |
} else { |
returnfalse; |
} |
} |
//Delay the specified number of ms. |
voidSerial::delay(int ms){ |
QTime stopWatch; |
stopWatch.start(); |
QTime startTime = QTime::currentTime(); |
QTime doneTime = startTime.addMSecs(ms); |
while (QTime::currentTime() < doneTime) { |
QCoreApplication::processEvents(QEventLoop::AllEvents, 1); |
} |
} |
intSerial::getBaudRate() { |
returnthis->port->baudRate(); |
} |
//Set the serial port baud rate. Returns true on success, false otherwise. |
boolSerial::setBaudRate(int baudRate) { |
returnthis->port->setBaudRate(baudRate); |
} |
//Return the current port name; |
QString Serial::getName(){ |
returnthis->port->portName(); |
} |
//Clear all bytes from the UART input buffer and return the number of bytes returned |
intSerial::flushInputBuffer(){ |
qDebug() << 'Serial::flushInputBuffer() Begin'; |
//Byte are not available until waitForReadyRead() is called |
if(this->port != 0) { |
QTime stopWatch; |
stopWatch.start(); |
this->port->waitForReadyRead(1); |
qDebug() << 'waitForReadyRead() took ' << stopWatch.elapsed(); |
stopWatch.restart(); |
int flushCount = this->port->readAll().length(); |
qDebug() << 'readAll() took ' << stopWatch.elapsed(); |
qDebug() << 'Serial::flushInputBuffer()' << flushCount; |
return flushCount; |
}else { |
return0; |
} |
} |
//Soft reset the serial port. This funciton closes and reopens the serial port with the same settings. |
boolSerial::softReset(){ |
QString name = this->getName(); |
int baudRate = this->getBaudRate(); |
this->close(); |
bool success = this->open(name, baudRate); |
emit softResetResponse(success); |
return success; |
} |
boolSerial::validChunkedData(QByteArray data) { |
while(getChunkSize(data) >= 0) |
{ |
int chunkSize = getChunkSize(data); |
int startOfChunk = data.indexOf('rn') + 2; |
//Return false if chunk is not complete |
if(data.length() < startOfChunk + chunkSize + 2) { |
qDebug() << 'Incomplete Chunk'; |
returnfalse; |
} |
//If chunk size is 0 and the chunk is complete we're at the end |
if(chunkSize 0) |
{ |
qDebug() << 'Found Valid End Of Chunked Transfer'; |
returntrue; |
} |
QByteArray chunk = data.mid(startOfChunk, chunkSize); |
//qDebug() << 'Valid' << chunkSize << 'Byte Chunk:' << chunk; |
//qDebug() << 'Remaining Data' << data.mid(startOfChunk + chunkSize + 2).length() << 'Bytes' << data.mid(startOfChunk + chunkSize + 2); |
data = data.mid(startOfChunk + chunkSize + 2); |
} |
returnfalse; |
} |
Serial Baud Rates
Copy lines Copy permalink