Results 1 to 13 of 13

Thread: sending command to IC magnetic card reader through serial port e.g command 0x03

  1. #1
    Join Date
    Jul 2012
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default sending command to IC magnetic card reader through serial port e.g command 0x03

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: sending command to IC magnetic card reader through serial port e.g command 0x03

    Hi,

    Wich port are you opening? You don't passed the name of the port to open and you are trying to open it. Have you looked what "open" is returning? Surely it returns false. Also you can check how many bytes have you wrote in "write" method.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Jul 2012
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: sending command to IC magnetic card reader through serial port e.g command 0x03

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    Wich port are you opening? You don't passed the name of the port to open and you are trying to open it. Have you looked what "open" is returning? Surely it returns false. Also you can check how many bytes have you wrote in "write" method.
    in the public declaration in think i have declared the port as given it a name com1, so i don't clearly understand what you are saying really.
    secondly, all other port when i access are just right so this device is what in need to receive a feedback command from it.

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: sending command to IC magnetic card reader through serial port e.g command 0x03

    Hi,

    What "open" returns?
    Qt Code:
    1. bool bOpen = port->open(QIODevice::ReadWrite);
    To copy to clipboard, switch view to plain text mode 
    Òscar Llarch i Galán

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: sending command to IC magnetic card reader through serial port e.g command 0x03

    Quote Originally Posted by hezbon View Post
    in the public declaration in think i have declared the port as given it a name com1, so i don't clearly understand what you are saying really.
    Your code seems to imply you have a global variable called "port" that you initialize with a new object where indeed you pass "COM1" as the parameter but then in the constructor you assign a different object to that pointer and there you're not passing "COM1" anymore.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jul 2012
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: sending command to IC magnetic card reader through serial port e.g command 0x03

    i have node the same. now if i want send a command like 0x32 do i send is as Qbytearray?

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,531
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending command to IC magnetic card reader through serial port e.g command 0x03

    What You want to send : one byte with value 0x32 or four characters 0,x,3,2 ?

  8. #8
    Join Date
    Jul 2012
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: sending command to IC magnetic card reader through serial port e.g command 0x03

    Quote Originally Posted by Lesiok View Post
    What You want to send : one byte with value 0x32 or four characters 0,x,3,2 ?
    one byte with value 0x32

  9. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,531
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending command to IC magnetic card reader through serial port e.g command 0x03

    So just use method putChar(char c). QExtSerialPort is a descendant of QIODevice class.

  10. #10
    Join Date
    Jul 2012
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: sending command to IC magnetic card reader through serial port e.g command 0x03

    Quote Originally Posted by Lesiok View Post
    So just use method putChar(char c). QExtSerialPort is a descendant of QIODevice class.
    iget the following error in this code part
    Qt Code:
    1. char vf="0x00";
    2. putchar(vf);
    3. QByteArray commands;
    4. ports->write(putchar(0x00));
    To copy to clipboard, switch view to plain text mode 
    D:\Qt\embedded-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug\..\embedded\mainwindow.c pp:120: error: invalid conversion from 'const char*' to 'char'

    D:\Qt\embedded-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug\..\embedded\mainwindow.c pp:123: error: invalid conversion from 'int' to 'const char*'
    D:\Qt\embedded-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug\..\embedded\mainwindow.c pp:123: error: initializing argument 1 of 'QByteArray::QByteArray(const char*)'


    Added after 15 minutes:


    Quote Originally Posted by hezbon View Post
    iget the following error in this code part
    Qt Code:
    1. char vf="0x00";
    2. putchar(vf);
    3. QByteArray commands;
    4. ports->write(putchar(0x00));
    To copy to clipboard, switch view to plain text mode 
    D:\Qt\embedded-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug\..\embedded\mainwindow.c pp:120: error: invalid conversion from 'const char*' to 'char'

    D:\Qt\embedded-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug\..\embedded\mainwindow.c pp:123: error: invalid conversion from 'int' to 'const char*'
    D:\Qt\embedded-build-desktop-Qt_4_7_4_for_Desktop_-_MinGW_4_4__Qt_SDK__Debug\..\embedded\mainwindow.c pp:123: error: initializing argument 1 of 'QByteArray::QByteArray(const char*)'
    please dont look at the error i have done this and no error has been realized sorry i gotten rid of the errors here is my new code.
    Qt Code:
    1. ports->putChar(0x00);
    To copy to clipboard, switch view to plain text mode 
    now my problem is the lcd display is 8x6 matrix and is connected via serial and by default it displays a,b,c,d,..i and now i want to display my data using my laptop from my programm and am using serial to usb converter, when i send the above, i should see something because there is that command for the gadget, i want to do a heads up display of the number of people in a department, but after sending the command nothing clearly happens.


    Added after 21 minutes:


    please dont look at the error i have done this and no error has been realized sorry i gotten rid of the errors here is my new code.
    Qt Code:
    1. ports->putChar(0x00);
    To copy to clipboard, switch view to plain text mode 
    now my problem is the lcd display is 8x6 matrix and is connected via serial and by default it displays a,b,c,d,..i and now i want to display my data using my laptop from my programm and am using serial to usb converter, when i send the above, i should see something because there is that command for the gadget, i want to do a heads up display of the number of people in a department, but after sending the command nothing clearly happens.
    Last edited by hezbon; 19th July 2012 at 09:31.

  11. #11
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,531
    Thanked 281 Times in 276 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sending command to IC magnetic card reader through serial port e.g command 0x03

    I think you have some documentation for this display. Read it.

  12. #12
    Join Date
    Jul 2012
    Posts
    19
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: sending command to IC magnetic card reader through serial port e.g command 0x03

    Quote Originally Posted by Lesiok View Post
    I think you have some documentation for this display. Read it.
    funnily enough the supplier never gave the documentation of the device. i have looked at it and i got a chinese which i translated to english but images could not be displayed, i dont know whether it is within my code or the serial lcd but if anyone can know a better lcd to display plz tell so that i buy it.

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: sending command to IC magnetic card reader through serial port e.g command 0x03

    I don't quite understand why you're asking on a forum dedicated to Qt how to use some 3rd party hardware device...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 4
    Last Post: 10th July 2010, 17:34
  2. serial port printer sending commands, how?
    By triperzonak in forum Qt Programming
    Replies: 3
    Last Post: 6th April 2009, 14:51
  3. sending QImage over serial port
    By yagabey in forum Qt Programming
    Replies: 5
    Last Post: 16th January 2008, 21:38

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.