i am trying to write to perform a read/write operation on the above device which require command. the control come from the host to the device and if the device would respond appropriately. the device is a motorized IC/magnetic card reader/writer.
Qt Code:
  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QtCore>
  4. #include <QtGui>
  5. #include "databaseconnection.h";
  6. #include <mythread.h>
  7. #include <QThread>
  8. #include <QMutex>
  9. #include "mysecondthread.h"
  10. #include <QtSql>
  11. #include <QDebug>
  12. #include "qextserialport.h"
  13. #include "qextserialenumerator.h"
  14. QextSerialPort *port = new QextSerialPort("COM1",QextSerialPort::EventDriven);
  15. MainWindow::MainWindow(QWidget *parent) :
  16. QMainWindow(parent),
  17. ui(new Ui::MainWindow)
  18. {
  19. // if(databaseconnection::databse()){
  20. // QMessageBox::information(this,"connected","connected");
  21. // }
  22. // else if(!(databaseconnection::databse())){
  23. // QMessageBox::information(this,"connected failed","failed");
  24. // }
  25. Mythread cob;
  26. port=new QextSerialPort();
  27. port->setBaudRate(BAUD9600);
  28. port->setFlowControl(FLOW_XONXOFF);
  29. port->setParity(PAR_NONE);
  30. port->setDataBits(DATA_8);
  31. port->setStopBits(STOP_1);
  32. port->open(QIODevice::ReadWrite);
  33. connect(port,SIGNAL(readyRead()),this,SLOT(on_pushButton_clicked()));
  34. QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
  35.  
  36. ui->setupUi(this);
  37. foreach (QextPortInfo info, ports) {
  38. qDebug() << "port name:" << info.portName;
  39. ui->portlab->addItem(info.portName);
  40. }
  41. }
  42.  
  43. MainWindow::~MainWindow()
  44. {
  45. delete ui;
  46. }
  47.  
  48. void MainWindow::on_pushButton_clicked()
  49. {
  50.  
  51.  
  52. //qDebug()<<"no data to read";
  53. if(port->isOpen()){
  54. QByteArray command="0x03";
  55.  
  56. QString data = port->readAll();
  57. if(port->write(command.toHex())){
  58. qDebug()<<"written";
  59. }
  60.  
  61. if(data==""){
  62. qDebug()<<"no data to read"<<data;
  63. ui->labelread->setText("wait while trying to read="+data);
  64. }
  65. else if(data.size()==3&& (data.startsWith("H"))){
  66. qDebug()<<data;
  67. ui->labelread->setText("the data read="+data);
  68.  
  69. } //data.data();
  70.  
  71.  
  72.  
  73.  
  74. // QMessageBox::information(this,"opened","opened");
  75. }
  76. else{
  77. QMessageBox::warning(this,"error","error");
  78. }
  79. //connect(port, SIGNAL(readyRead()), this, SLOT(onDataAvailable()));
  80. //port->open();
  81.  
  82. }
  83.  
  84. void MainWindow::on_pushButton_2_clicked()
  85. {
  86. QString bb,idn;
  87. bb= "oloo";
  88. idn="9000";
  89. QSqlQuery query;
  90. query.prepare("INSERT INTO user (name, idno) "
  91. "VALUES (:name, :idno)");
  92. query.bindValue(":name", bb);
  93. query.bindValue(":idno", idn);
  94.  
  95. // query.exec();
  96. if(query.exec()){
  97. qDebug()<<" inserted connected";
  98.  
  99.  
  100. }else{
  101. qDebug()<<" not insert connected";
  102. }
  103. }
  104.  
  105. void MainWindow::on_pushButton_3_clicked()
  106. {
  107. if(port->isOpen()){
  108. QString n="0x02";
  109. QByteArray command;
  110.  
  111. QString data = port->readAll();
  112. if(port->write(command.append(n))){
  113. qDebug()<<"written";
  114. }
  115. else{
  116. qDebug()<<" not written";
  117. }
  118. }
  119. }
To copy to clipboard, switch view to plain text mode 
am not able to receive any response from the device.