C++ 从WriteFile转换为QtSerial write
我正在尝试转换一些旧代码以利用C++ 从WriteFile转换为QtSerial write,c++,qt,serial-port,C++,Qt,Serial Port,我正在尝试转换一些旧代码以利用QtSerial集成 我有以下资料: void SendCmd(HANDLE Handle, UCHAR Address, UCHAR Command, UCHAR Type, UCHAR Motor, INT Value) { UCHAR TxBuffer[9]; DWORD BytesWritten; int i; TxBuffer[0]=Address; TxBuffer[1]=Command; TxBuff
QtSerial
集成
我有以下资料:
void SendCmd(HANDLE Handle, UCHAR Address, UCHAR Command, UCHAR Type, UCHAR Motor, INT Value)
{
UCHAR TxBuffer[9];
DWORD BytesWritten;
int i;
TxBuffer[0]=Address;
TxBuffer[1]=Command;
TxBuffer[2]=Type;
TxBuffer[3]=Motor;
TxBuffer[4]=Value >> 24;
TxBuffer[5]=Value >> 16;
TxBuffer[6]=Value >> 8;
TxBuffer[7]=Value & 0xff;
TxBuffer[8]=0;
for(i=0; i<8; i++)
TxBuffer[8]+=TxBuffer[i];
//Send the datagram
WriteFile(Handle, TxBuffer, 9, &BytesWritten, NULL);
}
当我发送命令时,我使用(例如):
我在QT中使用上述代码时遇到了一些问题。比如异步读取问题
我尝试修改上述代码以使用QSerialPort
:
对于端口的句柄:
QSerialPort serial;
serial.setPortName("COM5");
serial.open(QIODevice::ReadWrite);
serial.setBaudRate(QSerialPort::Baud115200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::HardwareControl);
if (serial.isOpen() && serial.isWritable())
{
qDebug() << "Serial is open";
}
QSerialPort序列;
serial.setPortName(“COM5”);
serial.open(QIODevice::ReadWrite);
串行数据传输速率(QSerialPort::Baud115200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
串行.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::HardwareControl);
if(serial.isOpen()&&serial.isWritable())
{
qDebug()由于QSerialPort
扩展自QIODevice
,只需使用read
和write
方法从端口发送和接收数据即可。在这方面,QSerialPort
的作用与使用Qt打开的任何其他文件一样。当您向串行端口写入内容时,最简单的simp方法是请将数据放入QByteArray
中,然后将其写出来。类似于以下内容的操作应该有效:
void SendCmd(QSerialPort* port, UCHAR Address, UCHAR Command, UCHAR Type, UCHAR Motor, INT Value)
{
QByteArray ba;
UCHAR checksum = 0;
ba.append(Address);
ba.append(Command);
ba.append(Type);
ba.append(Motor);
ba.append(Value >> 24;
ba.append(Value >> 16);
ba.append(Value >> 8);
ba.append(Value & 0xff);
for(i=0; i<8; i++)
checksum += ba[i];
ba.append( checksum );
//Send the datagram
port->write( ba );
}
QSerialPort serial;
serial.setPortName("COM5");
serial.open(QIODevice::ReadWrite);
serial.setBaudRate(QSerialPort::Baud115200);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::HardwareControl);
if (serial.isOpen() && serial.isWritable())
{
qDebug() << "Serial is open";
}
void SendCmd(QSerialPort* port, UCHAR Address, UCHAR Command, UCHAR Type, UCHAR Motor, INT Value)
{
QByteArray ba;
UCHAR checksum = 0;
ba.append(Address);
ba.append(Command);
ba.append(Type);
ba.append(Motor);
ba.append(Value >> 24;
ba.append(Value >> 16);
ba.append(Value >> 8);
ba.append(Value & 0xff);
for(i=0; i<8; i++)
checksum += ba[i];
ba.append( checksum );
//Send the datagram
port->write( ba );
}
/* When the seral port is made */
/* (using new signal/slot syntax) */
connect( serialPort, &QSerialPort::readyRead,
this, &MyClass::readyRead );
/* ... other initialization code ... */
void MyClass::readyRead(){
QByteArray data = serialPort.readAll();
/* Data processing here */
}