Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pic 18F2550 MPUSBRead doesn't word

Status
Not open for further replies.

2Fake

New Member
Pic 18F2550 MPUSBRead doesn't work

Hi I've just try to read something from the Pic18F2550. The Writing works, but i can't read something from the pic. Can someone help me to find the failure? Here my Sourcecode

Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "stdio.h"
#include <windows.h>
#include <QErrorMessage>
#include <mpusbapi.h>
#include <iostream>
#include "mpusbapi.h"
#include <QMessageBox>
#include "math.h"


using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    LoadDLL();

    PCHAR pVID_PID = "vid_04d8&pid_000c";

    BYTE SendData[64];
    DWORD SendLength = 16;
    PBYTE ReceiveData;
    DWORD ReceiveLength=16;
    UINT SendDelay = 50;
    UINT ReceiveDelay = 50;


    DWORD version = MPUSBGetDLLVersion();
    DWORD count =MPUSBGetDeviceCount(pVID_PID);

    cout << "Version: " << version << endl;
    cout << "Count: " << count << endl;

    SendData[0]=0x3f;
    cout << "Sendung: " << (int)SendData[0] << endl;

    cout << "Aufruf SendReceivePacket: " << SendReceivePacket(SendData, SendLength, ReceiveData, ReceiveLength, SendDelay, ReceiveDelay) << endl;
}

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

DWORD MainWindow::SendReceivePacket(PBYTE SendData, DWORD SendLength, PBYTE ReceiveData, DWORD ReceiveLength, UINT SendDelay, UINT ReceiveDelay)
{
    HANDLE myOutPipe;
    HANDLE myInPipe;
    PCHAR pVID_PID = "vid_04d8&pid_000c";
    PCHAR pEP = "\\MCHP_EP1";

    myOutPipe = MPUSBOpen(0,pVID_PID, pEP, MP_WRITE,0);
    myInPipe = MPUSBOpen(0,pVID_PID, pEP, MP_READ,0);

    cout << "MyOutPipe : " << myOutPipe << endl;
    cout << "MyInPipe  : " << myInPipe << endl;

    DWORD SentDataLength;
    DWORD ExpectedReceiveLength;
    UINT Result = 100;

    ExpectedReceiveLength = ReceiveLength;
    if ((myOutPipe != INVALID_HANDLE_VALUE) && (myInPipe != INVALID_HANDLE_VALUE))
    {
        if(MPUSBWrite(myOutPipe,SendData,SendLength, &SentDataLength, SendDelay) != 0)
        {
            if(MPUSBRead(myInPipe, ReceiveData, ExpectedReceiveLength, &ReceiveLength, ReceiveDelay) != 0)
            {
                if(ReceiveLength = ExpectedReceiveLength)
                {
                    Result = 1; //erfolgreich
                }
                else
                {
                    Result = 2; //falsche RückgabeLänge
                }
            }
            else
            {
               CheckInvalidHandle(myOutPipe, myInPipe);
            }
        }
        else
        {
            CheckInvalidHandle(myOutPipe, myInPipe);
        }
    }
    return Result;
}



void MainWindow::CheckInvalidHandle(HANDLE myInPipe, HANDLE myOutPipe)
{
    if(GetLastError() == ERROR_INVALID_HANDLE)
    {
        MPUSBClose(myOutPipe);
        MPUSBClose(myInPipe);
        myOutPipe = myInPipe = INVALID_HANDLE_VALUE;
    }
    else
    {
        cout << "Fehler " << GetLastError()<< endl;
    }
}
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top