PDA

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



hezbon
16th July 2012, 13:46
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.


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include <QtGui>
#include "databaseconnection.h";
#include <mythread.h>
#include <QThread>
#include <QMutex>
#include "mysecondthread.h"
#include <QtSql>
#include <QDebug>
#include "qextserialport.h"
#include "qextserialenumerator.h"
QextSerialPort *port = new QextSerialPort("COM1",QextSerialPort::EventDriven);
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// if(databaseconnection::databse()){
// QMessageBox::information(this,"connected","connected");
// }
// else if(!(databaseconnection::databse())){
// QMessageBox::information(this,"connected failed","failed");
// }
Mythread cob;
port=new QextSerialPort();
port->setBaudRate(BAUD9600);
port->setFlowControl(FLOW_XONXOFF);
port->setParity(PAR_NONE);
port->setDataBits(DATA_8);
port->setStopBits(STOP_1);
port->open(QIODevice::ReadWrite);
connect(port,SIGNAL(readyRead()),this,SLOT(on_push Button_clicked()));
QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();

ui->setupUi(this);
foreach (QextPortInfo info, ports) {
qDebug() << "port name:" << info.portName;
ui->portlab->addItem(info.portName);
}
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{


//qDebug()<<"no data to read";
if(port->isOpen()){
QByteArray command="0x03";

QString data = port->readAll();
if(port->write(command.toHex())){
qDebug()<<"written";
}

if(data==""){
qDebug()<<"no data to read"<<data;
ui->labelread->setText("wait while trying to read="+data);
}
else if(data.size()==3&& (data.startsWith("H"))){
qDebug()<<data;
ui->labelread->setText("the data read="+data);

} //data.data();




// QMessageBox::information(this,"opened","opened");
}
else{
QMessageBox::warning(this,"error","error");
}
//connect(port, SIGNAL(readyRead()), this, SLOT(onDataAvailable()));
//port->open();

}

void MainWindow::on_pushButton_2_clicked()
{
QString bb,idn;
bb= "oloo";
idn="9000";
QSqlQuery query;
query.prepare("INSERT INTO user (name, idno) "
"VALUES (:name, :idno)");
query.bindValue(":name", bb);
query.bindValue(":idno", idn);

// query.exec();
if(query.exec()){
qDebug()<<" inserted connected";


}else{
qDebug()<<" not insert connected";
}
}

void MainWindow::on_pushButton_3_clicked()
{
if(port->isOpen()){
QString n="0x02";
QByteArray command;

QString data = port->readAll();
if(port->write(command.append(n))){
qDebug()<<"written";
}
else{
qDebug()<<" not written";
}
}
}

am not able to receive any response from the device.

^NyAw^
16th July 2012, 13:54
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.

hezbon
16th July 2012, 14:43
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.

^NyAw^
16th July 2012, 16:21
Hi,

What "open" returns?


bool bOpen = port->open(QIODevice::ReadWrite);

wysota
16th July 2012, 19:49
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.

hezbon
17th July 2012, 12:34
i have node the same. now if i want send a command like 0x32 do i send is as Qbytearray?

Lesiok
17th July 2012, 14:25
What You want to send : one byte with value 0x32 or four characters 0,x,3,2 ?

hezbon
18th July 2012, 09:02
What You want to send : one byte with value 0x32 or four characters 0,x,3,2 ?

one byte with value 0x32

Lesiok
18th July 2012, 09:23
So just use method putChar(char c). QExtSerialPort is a descendant of QIODevice class.

hezbon
19th July 2012, 09:31
So just use method putChar(char c). QExtSerialPort is a descendant of QIODevice class.
iget the following error in this code part

char vf="0x00";
putchar(vf);
QByteArray commands;
ports->write(putchar(0x00));
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:


iget the following error in this code part

char vf="0x00";
putchar(vf);
QByteArray commands;
ports->write(putchar(0x00));
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.

ports->putChar(0x00);
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.

ports->putChar(0x00);
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.

Lesiok
19th July 2012, 11:27
I think you have some documentation for this display. Read it.

hezbon
19th July 2012, 11:56
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.

wysota
19th July 2012, 21:22
I don't quite understand why you're asking on a forum dedicated to Qt how to use some 3rd party hardware device...