#include <QtGui>
#include <stdlib.h>
#include <stdio.h>
#include "picconnector.h"
#include "mpusbapi.h"
PICConnector
::PICConnector(QWidget *parent
){
LoadDLL();
myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
}
//--------------------------------------------------------------------------------
// Load DLL file
void PICConnector::LoadDLL()
{
hinstLib = LoadLibrary(TEXT("mpusbapi.dll"));
if (hinstLib == NULL) {
printf("ERROR: unable to load DLL\n");
tr("ERROR: unable to load DLL"),
FreeLibrary(hinstLib);
}
// Get function pointer
else{
MPUSBGetDLLVersion=(DWORD(*)(void))\
GetProcAddress(hinstLib,"_MPUSBGetDLLVersion");
MPUSBGetDeviceCount=(DWORD(*)(PCHAR))\
GetProcAddress(hinstLib,"_MPUSBGetDeviceCount");
MPUSBOpen=(HANDLE(*)(DWORD,PCHAR,PCHAR,DWORD,DWORD))\
GetProcAddress(hinstLib,"_MPUSBOpen");
MPUSBWrite=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\
GetProcAddress(hinstLib,"_MPUSBWrite");
MPUSBRead=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\
GetProcAddress(hinstLib,"_MPUSBRead");
MPUSBReadInt=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\
GetProcAddress(hinstLib,"_MPUSBReadInt");
MPUSBClose=(BOOL(*)(HANDLE))GetProcAddress(hinstLib,"_MPUSBClose");
if((MPUSBGetDeviceCount == NULL) || (MPUSBOpen == NULL) ||
(MPUSBWrite == NULL) || (MPUSBRead == NULL) ||
(MPUSBClose == NULL) || (MPUSBGetDLLVersion == NULL) ||
(MPUSBReadInt == NULL)){
tr("ERROR: unable to find DLL function"),
FreeLibrary(hinstLib);
}
}
//FreeLibrary(hinstLib);
}
//--------------------------------------------------------------------------------
PICConnector::~PICConnector()
{
if (myOutPipe != INVALID_HANDLE_VALUE)
MPUSBClose(myOutPipe);
if ( myInPipe != INVALID_HANDLE_VALUE)
MPUSBClose(myInPipe);
myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
// Always check to close the library too.
if (hinstLib != NULL) FreeLibrary(hinstLib);
}
//--------------------------------------------------------------------------------
// retorna true se conseguiu abrir(inicializar) o dispositivo
bool PICConnector::Open()
{
// Always open one device only
if (MPUSBGetDeviceCount(vid_pid.toLocal8Bit().data()) > 0) {
myOutPipe = MPUSBOpen(0,vid_pid.toLocal8Bit().data(),out_pipe.toLocal8Bit().data(),MP_WRITE,0);
myInPipe = MPUSBOpen(0,vid_pid.toLocal8Bit().data(),out_pipe.toLocal8Bit().data(),MP_READ,0);
if(myOutPipe == INVALID_HANDLE_VALUE || myInPipe == INVALID_HANDLE_VALUE)
{
tr("ERROR: Failed to open data pipes"),
return FALSE;
}//end if
// testa a conexão - envia ping
// DWORD ReceiveLength, SentLength;
//
// byte send_buffer[1];
// byte recv_buffer[1];
// send_buffer[0] = PING;
// MPUSBWrite(myOutPipe,send_buffer,1,&SentLength,1000);
// MPUSBRead(myInPipe,recv_buffer,MAXSIZE,&ReceiveLength,1000);
// if ((ReceiveLength!=1)|(recv_buffer[0]!=PING)){
// QMessageBox::warning(this, tr("Teste Conexao"),
// tr("ERROR: O teste de conexao falhou\n"),
// QMessageBox::Ok);
// }
}
else {
tr("ERROR: No devices connected"),
return FALSE;
}
return TRUE;
}
//--------------------------------------------------------------------------------
void PICConnector::Close()
{
MPUSBClose(myOutPipe);
MPUSBClose(myInPipe);
myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
}
//--------------------------------------------------------------------------------
DWORD PICConnector::getDLLversion()
{
return MPUSBGetDLLVersion();
}
//--------------------------------------------------------------------------------
// envia "data" e le o que foi enviado para checar que foi transmitido corretamente
DWORD PICConnector::SendData(PVOID SendData, DWORD SendLength, PVOID ReceiveData,
PDWORD ReceiveLength, DWORD SendDelay, DWORD ReceiveDelay)
{
DWORD SentDataLength;
DWORD ExpectedReceiveLength = *ReceiveLength;
if(myOutPipe != INVALID_HANDLE_VALUE && myInPipe != INVALID_HANDLE_VALUE)
{
if(MPUSBWrite(myOutPipe,SendData,SendLength,&SentDataLength,SendDelay))
{
if(MPUSBRead(myInPipe,ReceiveData, ExpectedReceiveLength,
ReceiveLength,ReceiveDelay))
{
if(*ReceiveLength == ExpectedReceiveLength)
{
return 1; // Success!
}
else if(*ReceiveLength < ExpectedReceiveLength)
{
return 2; // Partially failed, incorrect receive length
}//end if else
}
else
CheckInvalidHandle();
}
else
CheckInvalidHandle();
}//end if
return 0; // Operation Failed
}//end SendReceivePacket
//--------------------------------------------------------------------------------
void PICConnector::CheckInvalidHandle()
{
if(GetLastError() == ERROR_INVALID_HANDLE)
{
// Most likely cause of the error is the board was disconnected.
MPUSBClose(myOutPipe);
MPUSBClose(myInPipe);
myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
}//end if
else
tr
("Error Code %1").
arg(QString::number(GetLastError
(),
16)),
}//end CheckInvalidHandle
#include <QtGui>
#include <stdlib.h>
#include <stdio.h>
#include "picconnector.h"
#include "mpusbapi.h"
PICConnector::PICConnector(QWidget *parent)
: QWidget(parent)
{
LoadDLL();
myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
}
//--------------------------------------------------------------------------------
// Load DLL file
void PICConnector::LoadDLL()
{
hinstLib = LoadLibrary(TEXT("mpusbapi.dll"));
if (hinstLib == NULL) {
printf("ERROR: unable to load DLL\n");
QMessageBox::warning(this, tr("LoadLibrary"),
tr("ERROR: unable to load DLL"),
QMessageBox::Ok);
FreeLibrary(hinstLib);
}
// Get function pointer
else{
MPUSBGetDLLVersion=(DWORD(*)(void))\
GetProcAddress(hinstLib,"_MPUSBGetDLLVersion");
MPUSBGetDeviceCount=(DWORD(*)(PCHAR))\
GetProcAddress(hinstLib,"_MPUSBGetDeviceCount");
MPUSBOpen=(HANDLE(*)(DWORD,PCHAR,PCHAR,DWORD,DWORD))\
GetProcAddress(hinstLib,"_MPUSBOpen");
MPUSBWrite=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\
GetProcAddress(hinstLib,"_MPUSBWrite");
MPUSBRead=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\
GetProcAddress(hinstLib,"_MPUSBRead");
MPUSBReadInt=(DWORD(*)(HANDLE,PVOID,DWORD,PDWORD,DWORD))\
GetProcAddress(hinstLib,"_MPUSBReadInt");
MPUSBClose=(BOOL(*)(HANDLE))GetProcAddress(hinstLib,"_MPUSBClose");
if((MPUSBGetDeviceCount == NULL) || (MPUSBOpen == NULL) ||
(MPUSBWrite == NULL) || (MPUSBRead == NULL) ||
(MPUSBClose == NULL) || (MPUSBGetDLLVersion == NULL) ||
(MPUSBReadInt == NULL)){
QMessageBox::warning(this, "LoadDLL",
tr("ERROR: unable to find DLL function"),
QMessageBox::Ok);
FreeLibrary(hinstLib);
}
}
//FreeLibrary(hinstLib);
}
//--------------------------------------------------------------------------------
PICConnector::~PICConnector()
{
if (myOutPipe != INVALID_HANDLE_VALUE)
MPUSBClose(myOutPipe);
if ( myInPipe != INVALID_HANDLE_VALUE)
MPUSBClose(myInPipe);
myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
// Always check to close the library too.
if (hinstLib != NULL) FreeLibrary(hinstLib);
}
//--------------------------------------------------------------------------------
// retorna true se conseguiu abrir(inicializar) o dispositivo
bool PICConnector::Open()
{
// Always open one device only
if (MPUSBGetDeviceCount(vid_pid.toLocal8Bit().data()) > 0) {
myOutPipe = MPUSBOpen(0,vid_pid.toLocal8Bit().data(),out_pipe.toLocal8Bit().data(),MP_WRITE,0);
myInPipe = MPUSBOpen(0,vid_pid.toLocal8Bit().data(),out_pipe.toLocal8Bit().data(),MP_READ,0);
if(myOutPipe == INVALID_HANDLE_VALUE || myInPipe == INVALID_HANDLE_VALUE)
{
QMessageBox::warning(this, tr("MPUSBOpen"),
tr("ERROR: Failed to open data pipes"),
QMessageBox::Ok);
return FALSE;
}//end if
// testa a conexão - envia ping
// DWORD ReceiveLength, SentLength;
//
// byte send_buffer[1];
// byte recv_buffer[1];
// send_buffer[0] = PING;
// MPUSBWrite(myOutPipe,send_buffer,1,&SentLength,1000);
// MPUSBRead(myInPipe,recv_buffer,MAXSIZE,&ReceiveLength,1000);
// if ((ReceiveLength!=1)|(recv_buffer[0]!=PING)){
// QMessageBox::warning(this, tr("Teste Conexao"),
// tr("ERROR: O teste de conexao falhou\n"),
// QMessageBox::Ok);
// }
}
else {
QMessageBox::warning(this, tr("MPUSBGetDeviceCount"),
tr("ERROR: No devices connected"),
QMessageBox::Ok);
return FALSE;
}
return TRUE;
}
//--------------------------------------------------------------------------------
void PICConnector::Close()
{
MPUSBClose(myOutPipe);
MPUSBClose(myInPipe);
myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
}
//--------------------------------------------------------------------------------
DWORD PICConnector::getDLLversion()
{
return MPUSBGetDLLVersion();
}
//--------------------------------------------------------------------------------
// envia "data" e le o que foi enviado para checar que foi transmitido corretamente
DWORD PICConnector::SendData(PVOID SendData, DWORD SendLength, PVOID ReceiveData,
PDWORD ReceiveLength, DWORD SendDelay, DWORD ReceiveDelay)
{
DWORD SentDataLength;
DWORD ExpectedReceiveLength = *ReceiveLength;
if(myOutPipe != INVALID_HANDLE_VALUE && myInPipe != INVALID_HANDLE_VALUE)
{
if(MPUSBWrite(myOutPipe,SendData,SendLength,&SentDataLength,SendDelay))
{
if(MPUSBRead(myInPipe,ReceiveData, ExpectedReceiveLength,
ReceiveLength,ReceiveDelay))
{
if(*ReceiveLength == ExpectedReceiveLength)
{
return 1; // Success!
}
else if(*ReceiveLength < ExpectedReceiveLength)
{
return 2; // Partially failed, incorrect receive length
}//end if else
}
else
CheckInvalidHandle();
}
else
CheckInvalidHandle();
}//end if
return 0; // Operation Failed
}//end SendReceivePacket
//--------------------------------------------------------------------------------
void PICConnector::CheckInvalidHandle()
{
if(GetLastError() == ERROR_INVALID_HANDLE)
{
// Most likely cause of the error is the board was disconnected.
MPUSBClose(myOutPipe);
MPUSBClose(myInPipe);
myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
}//end if
else
QMessageBox::warning(this, tr("LoadLibrary"),
tr("Error Code %1").arg(QString::number(GetLastError(),16)),
QMessageBox::Ok);
}//end CheckInvalidHandle
To copy to clipboard, switch view to plain text mode
#include <QtGui>
#include "testempusb.h"
#include "picconnector.h"
testempusb
::testempusb(QWidget *parent
)
{
device = new PICConnector;
label
= new QLabel(tr
("Get DLL Version:"));
label2
= new QLabel(tr
("Data to Send(00-FF):"));
label->setBuddy(lineEdit);
label2->setBuddy(lineEdit2);
connect(okButton, SIGNAL(clicked()),this, SLOT(ShowDLLversion()));
connect(sendButton,SIGNAL(clicked()),this,SLOT(Send()));
firstLayout->addWidget(label);
firstLayout->addWidget(lineEdit);
firstLayout->addWidget(okButton);
secondLayout->addWidget(label2);
secondLayout->addWidget(lineEdit2);
secondLayout->addWidget(sendButton);
boxLayout->addLayout(firstLayout);
boxLayout->addLayout(secondLayout);
setLayout(boxLayout);
}
//--------------------------------------------------------------------------------
void testempusb::ShowDLLversion()
{
DWORD temp = device->getDLLversion();
int aux1 = HIWORD(temp)>>8;
int aux2 = HIWORD(temp) & 0xFF;
int aux3 = LOWORD(temp)>>8;
int aux4 = LOWORD(temp) & 0xFF;
lineEdit
->setText
( tr
("MPUSBAPI Version:%1.%2.%3.%4").
arg(QString::number(aux1,
10))}
//--------------------------------------------------------------------------------
void testempusb::Send()
{
sendButton->setDisabled(true);
if (device->Open()){
BYTE send_buf[64],receive_buf[64];
DWORD RecvLength=1;
send_buf[0] = PING;
bool ok;
send_buf[1] = lineEdit2->text().toInt(&ok,16);
if( device->SendData(send_buf,2,receive_buf,&RecvLength,1000,1000) == 1){ //testar ==0
if(RecvLength != 1 || receive_buf[0] != PING ){
tr("Failed to transmit DATA"),
}
else{
tr("Dados transmitidos com sucesso!"),
}
}
}
device->Close();
sendButton->setEnabled(true);
}
#include <QtGui>
#include "testempusb.h"
#include "picconnector.h"
testempusb::testempusb(QWidget *parent)
: QWidget(parent)
{
device = new PICConnector;
label = new QLabel(tr("Get DLL Version:"));
label2 = new QLabel(tr("Data to Send(00-FF):"));
lineEdit = new QLineEdit;
lineEdit2 = new QLineEdit;
label->setBuddy(lineEdit);
label2->setBuddy(lineEdit2);
okButton = new QPushButton(tr("&Ok"));
sendButton = new QPushButton(tr("&Send"));
connect(okButton, SIGNAL(clicked()),this, SLOT(ShowDLLversion()));
connect(sendButton,SIGNAL(clicked()),this,SLOT(Send()));
QHBoxLayout *firstLayout = new QHBoxLayout;
firstLayout->addWidget(label);
firstLayout->addWidget(lineEdit);
firstLayout->addWidget(okButton);
QHBoxLayout *secondLayout = new QHBoxLayout;
secondLayout->addWidget(label2);
secondLayout->addWidget(lineEdit2);
secondLayout->addWidget(sendButton);
QVBoxLayout *boxLayout = new QVBoxLayout;
boxLayout->addLayout(firstLayout);
boxLayout->addLayout(secondLayout);
setLayout(boxLayout);
}
//--------------------------------------------------------------------------------
void testempusb::ShowDLLversion()
{
DWORD temp = device->getDLLversion();
int aux1 = HIWORD(temp)>>8;
int aux2 = HIWORD(temp) & 0xFF;
int aux3 = LOWORD(temp)>>8;
int aux4 = LOWORD(temp) & 0xFF;
lineEdit->setText( tr("MPUSBAPI Version:%1.%2.%3.%4").arg(QString::number(aux1,10))
.arg(QString::number(aux2,10)).arg(QString::number(aux3,10))
.arg(QString::number(aux4,10)));
}
//--------------------------------------------------------------------------------
void testempusb::Send()
{
sendButton->setDisabled(true);
if (device->Open()){
BYTE send_buf[64],receive_buf[64];
DWORD RecvLength=1;
send_buf[0] = PING;
bool ok;
send_buf[1] = lineEdit2->text().toInt(&ok,16);
if( device->SendData(send_buf,2,receive_buf,&RecvLength,1000,1000) == 1){ //testar ==0
if(RecvLength != 1 || receive_buf[0] != PING ){
QMessageBox::warning(this, tr("Send"),
tr("Failed to transmit DATA"),
QMessageBox::Ok);
}
else{
QMessageBox::warning(this, tr("Send"),
tr("Dados transmitidos com sucesso!"),
QMessageBox::Ok);
}
}
}
device->Close();
sendButton->setEnabled(true);
}
To copy to clipboard, switch view to plain text mode
Bookmarks