Help using QSerialPort
-
I am having trouble sending commands to a motor through a serial interface.
Bellow I try to send a homing command (first byte is the device id, second byte is the command, and there are 4 more bytes of data which are not used for this command)
@int main(){
QSerialPortInfo info("COM1");
qDebug()<<info.manufacturer()<<endl;
qDebug()<<info.description()<<endl;
qDebug()<<info.portName()<<endl;
QSerialPort port(info);port.setBaudRate(QSerialPort::Baud9600); port.setDataBits(QSerialPort::Data8); port.setFlowControl(QSerialPort::NoFlowControl); port.setParity(QSerialPort::NoParity); port.setStopBits(QSerialPort::OneStop); port.open(QSerialPort::ReadWrite); char cmd_data[]={0,1,0,0,0,0}; port.write(cmd_data); delay(3000); // delay 3s cout<<"Closing Serial Port"<<endl; port.close(); return 0;
}@
Nothing happens.... The device works with an external program..
What am I doing wrong ? Is this the right way to send the command ? -
first you have to open the port
and then do the settingsand also
consider the return status of all the settings and
port open methods -
Hi.
Did you check is serialport opened and data realy sended? If yes try compare real data sended to a device from your programm and external programm. Are they identical?
You can use "free serialport monitor":http://www.serial-port-monitor.com/ for that. -
Hi.
Yes, please do all as guys says...
Also this wrong:
[code]
...
port.write(cmd_data);
delay(3000); // delay 3s
...
[/code]need:
[code]
...
port.write(cmd_data);
waitForBytesWritten(3000); // delay 3s
...
[/code] -
Hi
I am having a problem communicating with my motor. When I give it some commands that take more time to realize (for example a long move or a home command) I am having problem when reading a response in the sense that I cannot always get the 6 bytes corresponding to a response. This thing also happens when I use a Sleep function (from "windows.h") with sufficient time for the command to complete.
I have this functions for sending...
@bool MotorAccess::sendCommand(const QByteArray& cmd){
int bytes_sent = port_->write(cmd);
if (bytes_sent != 6){
return false;
}
port_->waitForBytesWritten(100);return true;
}@
and reading...
@QByteArray MotorAccess::getResponse(){
bool read_success = true;
QByteArray response = QByteArray();// = port_->readAll();// discard data if buffers have more data then needed
// this can happen when manual accessing the motor
while (port_->bytesAvailable()>6) {
#ifdef DEBUG_MODE
qDebug()<<port_->bytesAvailable();
#endif
port_->read(1);
}read_success = port_->waitForReadyRead(100);
if (port_->bytesAvailable() == 0) {
return response;
}
response.append(port_->read(1));while (read_success && response.count() < 6 ){
read_success = port_->waitForReadyRead(100);
response.append(port_->read(1));
}
if ((! read_success) || response.count() != 6) {
#ifdef DEBUG_MODEqDebug()<<"Read Problem. Bytes available: "<<port_->bytesAvailable();
#endif
}return response;
}@
I would really appreciate some help on this. Thanks