jccsantos
Junior Member level 2
mpusbapi.dll
hello , i have a aplication that only needs the data in, how can i modify this code to read only data-in (continued) and remove the timer??????
the program will make only a job off a logger with data incoming.
The schematic is based in PIC18F2550 with USB and uses mpusbapi.dll.
The pic will send data (1 byte) every second, and do not receive any.
This is the code:
Thanks in advance.
hello , i have a aplication that only needs the data in, how can i modify this code to read only data-in (continued) and remove the timer??????
the program will make only a job off a logger with data incoming.
The schematic is based in PIC18F2550 with USB and uses mpusbapi.dll.
The pic will send data (1 byte) every second, and do not receive any.
This is the code:
Code:
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls, ExtCtrls, Menus, A3nalogGauge, Buttons, rxtimerlst;
type
TForm1 = class(TForm)
StatusBar1: TStatusBar;
Button1: TButton;
Edit1: TEdit;
Label2: TLabel;
Timer1: TTimer;
BtnTemp: TSpeedButton;
EditMin: TLabeledEdit;
EditMax: TLabeledEdit;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Label2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure BtnTempClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
end;
Const
MAXSIZE=64;
MPUSB_FAIL=0;
MPUSB_SUCCESS=1;
MP_WRITE:DWORD=0;
MP_READ:DWORD=1;
MAX_NUM_MPUSB_DEV=127;
type
DWORD = LongInt;
PCHAR8 = array[0..MAXSIZE] of char;
PBYTE = array[0..MAXSIZE] of BYTE;
PDWORD = array[0..MAXSIZE] of DWORD;
PVOID = Pointer;
UINT = Cardinal;
function _MPUSBGetDLLVersion():DWORD; stdcall;external 'mpusbapi.dll';
function _MPUSBGetDeviceCount(pVID_PID:PCHAR8):DWORD; stdcall;external 'mpusbapi.dll';
function _MPUSBOpen(instance:DWORD;pVID_PID:PCHAR8;pEP:PCHAR8;dwDir:DWORD;dwReserved:DWORD):THANDLE; stdcall;external 'mpusbapi.dll';
function _MPUSBRead(handle:THANDLE;var pData:PBYTE;dwLen:DWORD;var pLength:DWORD;dwMilliseconds:DWORD):DWORD; stdcall;external 'mpusbapi.dll';
function _MPUSBReadInt(handle:THANDLE;var pData:PBYTE;dwLen:DWORD;var pLength:PDWORD;dwMilliseconds:DWORD):DWORD; stdcall;external 'mpusbapi.dll';
function _MPUSBWrite(handle:THANDLE;pData:PBYTE;dwLen:DWORD;var pLength:DWORD;dwMilliseconds:DWORD):DWORD; stdcall;external 'mpusbapi.dll';
function _MPUSBClose(handle:THANDLE):DWORD; stdcall;external 'mpusbapi.dll';
var
Form1: TForm1;
vid_pid:PCHAR8='vid_04d8&pid_00c8';
out_pipe:PCHAR8='\MCHP_EP1';
in_pipe:PCHAR8='\MCHP_EP1';
myOutPipe:THANDLE;
myInPipe:THANDLE;
isConnected:boolean;
implementation
uses ShellAPI;
{$R *.DFM}
procedure CheckInvalidHandle();
begin
if(GetLastError=ERROR_INVALID_HANDLE) then
begin
_MPUSBClose(myOutPipe);
_MPUSBClose(myInPipe);
myInPipe:=INVALID_HANDLE_VALUE;
myOutPipe:=INVALID_HANDLE_VALUE;
end
else
ShowMessage('Error Code :'+inttostr(GetLastError()));
end;
function SendReceivePacket(SendData:PBYTE;SendLength:DWORD;var ReceiveData:PBYTE;
var ReceiveLength:DWORD;SendDelay:UINT;ReceiveDelay:UINT):DWORD; stdcall;
var
SentDataLength:DWORD ;
ExpectedReceiveLength:DWORD;
begin
ExpectedReceiveLength:= ReceiveLength;
if((myOutPipe <> INVALID_HANDLE_VALUE) and (myInPipe <> INVALID_HANDLE_VALUE)) then
begin
if(_MPUSBWrite(myOutPipe,SendData,SendLength,SentDataLength,SendDelay)<>0) then
if(_MPUSBRead(myInPipe,ReceiveData,ExpectedReceiveLength,ReceiveLength,ReceiveDelay)<>0) then
begin
if(ReceiveLength = ExpectedReceiveLength) then
begin
Result:=1; // OK
exit;
end
else
if(ReceiveLength < ExpectedReceiveLength) then
begin
Result:=2; // failed, incorrect receive length
exit;
end
end
else
CheckInvalidHandle()
else
CheckInvalidHandle()
end
else
begin
Result:=0; // Error
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if (myOutPipe <> INVALID_HANDLE_VALUE) then _MPUSBClose(myOutPipe);
if (myInPipe <> INVALID_HANDLE_VALUE) then _MPUSBClose(myInPipe);
myInPipe:= INVALID_HANDLE_VALUE;
myOutPipe:=INVALID_HANDLE_VALUE;
end;
procedure TForm1.Label2Click(Sender: TObject);
var
AnUrl: array[0..255] of char;
begin
StrPCopy(AnUrl, 'http://XXXXXXXXXXXXX/');
ShellExecute(Application.Handle, 'open', AnUrl, nil, nil, SW_NORMAL);
end;
//Ligar dispositivo usb
procedure TForm1.Button1Click(Sender: TObject);
var
selection:DWORD;
send_buf:PBYTE;
receive_buf:PBYTE;
RecvLength:DWORD;
begin
selection:=0; // USB device that have PID & VID as we configed.
if (_MPUSBGetDeviceCount(vid_pid)=0) then
begin
StatusBar1.Panels[1].text:='Estado : dispositivo não encontrado';
StatusBar1.Panels[0].text:='Versão Firmware : ????';
exit;
end
else
begin
StatusBar1.Panels[1].text:='Estado : Dispositivo Ligado';
StatusBar1.Panels[0].text:='Versão Firmware : 0.1';
end;
myOutPipe:= _MPUSBOpen(selection,vid_pid,out_pipe,MP_WRITE,0);
myInPipe:= _MPUSBOpen(selection,vid_pid,out_pipe,MP_READ,0);
if ((myOutPipe = INVALID_HANDLE_VALUE) or (myInPipe = INVALID_HANDLE_VALUE)) then
begin
StatusBar1.Panels[1].text:='Estado : USB Error';
exit;
end;
_MPUSBClose(myOutPipe);
_MPUSBClose(myInPipe);
myInPipe:= INVALID_HANDLE_VALUE;
myOutPipe:=INVALID_HANDLE_VALUE;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
myOutPipe:=INVALID_HANDLE_VALUE;
myInPipe:= INVALID_HANDLE_VALUE;
timer1.Enabled := false;
end;
procedure TForm1.BtnTempClick(Sender: TObject);
begin
timer1.Enabled := BtnTemp.Down ;
if BtnTemp.Down then BtnTemp.Caption := 'Parar'
else
BtnTemp.Caption := 'Temporizar';
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
selection:DWORD;
send_buf:PBYTE;
receive_buf:PBYTE;
RecvLength:DWORD;
begin
selection:=0; // USB device that have PID & VID as we configed.
myOutPipe:= _MPUSBOpen(selection,vid_pid,out_pipe,MP_WRITE,0);
myInPipe:= _MPUSBOpen(selection,vid_pid,out_pipe,MP_READ,0);
if ((myOutPipe = INVALID_HANDLE_VALUE) or (myInPipe = INVALID_HANDLE_VALUE)) then
begin
StatusBar1.Panels[1].text:='Estado : USB Error';
exit;
end;
send_buf[0]:= $00; // command
send_buf[1]:=1; // Expected length of the result
RecvLength:=1;
if(SendReceivePacket(send_buf,1,receive_buf,RecvLength,1000,1000) = 1) then
begin
if receive_buf[1] =0 then
begin
edit1.Text := IntToStr(Receive_buf[0]) ;
end;
end
else
begin
StatusBar1.Panels[1].Text:='Estado : Erro Comunicação!';
end;
_MPUSBClose(myOutPipe);
_MPUSBClose(myInPipe);
myInPipe:= INVALID_HANDLE_VALUE;
myOutPipe:=INVALID_HANDLE_VALUE;
end;
end.
Thanks in advance.