实现了任工需要设置的所有参数
This commit is contained in:
@ -1,59 +0,0 @@
|
||||
#include "qtserialport.h"
|
||||
|
||||
int QtSerialport::OpenSerialPort(string portName)
|
||||
{
|
||||
QList<QSerialPortInfo> infos = QSerialPortInfo::availablePorts();
|
||||
|
||||
//std::cout<<"number of availablePorts:"<<infos.size()<<std::endl;
|
||||
|
||||
//如果在构造函数中创建m_serial就会出现错误:
|
||||
//QObject: Cannot create children for a parent that is in a different thread.
|
||||
//(Parent is QSerialPort(0x2e31b20), parent's thread is QThread(0x2e2f130), current thread is QThread(0x2e31110)
|
||||
m_serial = new QSerialPort();
|
||||
|
||||
if(m_serial->isOpen())//如果串口已经打开了 先给他关闭了
|
||||
{
|
||||
m_serial->clear();
|
||||
m_serial->close();
|
||||
}
|
||||
|
||||
m_serial->setPortName(QString::fromStdString(portName));
|
||||
m_serial->open(QIODevice::ReadWrite);
|
||||
|
||||
|
||||
bool x=SetBaudrate(9600);
|
||||
if(x)
|
||||
{
|
||||
std::cout<<"波特率被成功设置为:"<<m_serial->baudRate()<<std::endl;
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cout<<"波特率设置失败!"<<std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int QtSerialport::CloseSerialPort()
|
||||
{
|
||||
m_serial->close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int QtSerialport::SetBaudrate(int baudrate)
|
||||
{
|
||||
bool x=m_serial->setBaudRate(baudrate);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int QtSerialport::SendData(const char chrMessage[], const unsigned short usLen)
|
||||
{
|
||||
//QIODevice::write(const char *data, qint64 maxSize)
|
||||
m_serial->write(chrMessage, usLen);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int QtSerialport::ReadData()
|
||||
{
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user