scPtr = new Session ();
QString portName
= scPtr
->enumerate
();
if (portName == "NULL"){
qDebug() << "Serial Device NOT FOUND portName = " << portName;
}else{ qDebug() << "SerialDevice Found. portName = " << portName; }
PortListener *listener = new PortListener(portName);
//============ below does not work?===============
clickTEST0();
//============ below works======================
ba.resize(3);
ba[0] = 254 ;
ba[1] = 1;
ba[2] = 255;
int i = listener->port->write(ba,ba.length());
}
void MainWindow::clickTEST0() {
ba.resize(3);
ba[0] = 254 ;
ba[1] = 1;
ba[2] = 255;
int i = listener->port->write(ba,ba.length());
}
Session::Session(){}
QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
int tempport = 9999;
int portsize = 0;
for (int i = 0; i < ports.size(); i++) {
portsize++;
qstr = ports.at(i).enumName;
if (qstr == SerialDeviceString) {
tempport = i;
tempportname = ports.at(i).portName;
tempfriendname = ports.at(i).friendName;
} else {}
}
QString portName
= tempportname;
// update this to use your port of choice if (portName == "NULL"){ qDebug() << "SerialDevice NOT FOUND portName = " << portName;
}else{ qDebug() << " SerialDevice FOUND portName = " << portName; }
return portName;
}
PortListener
::PortListener(const QString & portName
) { //, int iVal){ //portName = this->enumerate(); if (portName=="NULL"){ //NOT CONNECTED
}else{ SetPort(portName); }
}
void PortListener
::SetPort(const QString & portName
){ this->port = new QextSerialPort(portName, QextSerialPort::EventDriven);
port->setBaudRate(BAUD115200);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
if (port
->open
(QIODevice::ReadWrite) == true) { connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
connect(port, SIGNAL(dsrChanged(bool)), this, SLOT(onDsrChanged(bool)));
if (!(port->lineStatus() & LS_DSR))
qDebug() << "warning: device is not turned on";
qDebug() << "listening for data on" << port->portName();
}
else {qDebug() << "device failed to open:" << port->errorString(); }
emit onReadyWrite("Listener Created!");
}
MainWindow::MainWindow(QWidget *parent): QMainWindow(parent){
QWidget *w = new QWidget(this);
scPtr = new Session ();
QString portName = scPtr->enumerate();
if (portName == "NULL"){
qDebug() << "Serial Device NOT FOUND portName = " << portName;
int i = QMessageBox::warning(this,tr("Connection"),tr("No SerialDevices detected."),QMessageBox::Cancel);
}else{ qDebug() << "SerialDevice Found. portName = " << portName; }
PortListener *listener = new PortListener(portName);
//============ below does not work?===============
clickTEST0();
//============ below works======================
QByteArray ba;
ba.resize(3);
ba[0] = 254 ;
ba[1] = 1;
ba[2] = 255;
int i = listener->port->write(ba,ba.length());
}
void MainWindow::clickTEST0() {
QByteArray ba;
ba.resize(3);
ba[0] = 254 ;
ba[1] = 1;
ba[2] = 255;
int i = listener->port->write(ba,ba.length());
}
Session::Session(){}
QString Session::enumerate(){
QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
int tempport = 9999;
QString tempportname = "NULL";
QString tempfriendname = "NULL";
QString qstr;
int portsize = 0;
for (int i = 0; i < ports.size(); i++) {
portsize++;
qstr = ports.at(i).enumName;
if (qstr == SerialDeviceString) {
tempport = i;
tempportname = ports.at(i).portName;
tempfriendname = ports.at(i).friendName;
} else {}
}
QString portName = tempportname; // update this to use your port of choice
if (portName == "NULL"){ qDebug() << "SerialDevice NOT FOUND portName = " << portName;
}else{ qDebug() << " SerialDevice FOUND portName = " << portName; }
return portName;
}
PortListener::PortListener(const QString & portName) { //, int iVal){ //portName = this->enumerate();
if (portName=="NULL"){ //NOT CONNECTED
}else{ SetPort(portName); }
}
void PortListener::SetPort(const QString & portName){
this->port = new QextSerialPort(portName, QextSerialPort::EventDriven);
port->setBaudRate(BAUD115200);
port->setFlowControl(FLOW_OFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
if (port->open(QIODevice::ReadWrite) == true) {
connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
connect(port, SIGNAL(dsrChanged(bool)), this, SLOT(onDsrChanged(bool)));
if (!(port->lineStatus() & LS_DSR))
qDebug() << "warning: device is not turned on";
qDebug() << "listening for data on" << port->portName();
}
else {qDebug() << "device failed to open:" << port->errorString(); }
emit onReadyWrite("Listener Created!");
}
To copy to clipboard, switch view to plain text mode
Bookmarks