Jump to content

Writing to an USB port


VicTT
 Share

Recommended Posts

I think I got a solution (that's in mikroe - PIC). :ph34r: the link: http://www.mikroe.com/forum/viewtopic.php?t=7296

It's a program that generates code in VB, Delphi or C++ and have a dll to handle most of the job. Someone would need to translate the code from PASCAL to AutoIt ... :lmao:(mostly the DLL calls and events), I'm not the right person... Where to get the generator program: http://www.mecanique.co.uk/software/EasyHID.zip . I also attached a zip with all the generated Delphi Pascal and the dll help file.

Now, the generated dll declarations

unit cUSBInterface;

interface

uses
   Windows,Messages;

const
   // the plugin manager DLL name...
   DLL_NAME                  = 'mcHID.dll';

// DLL interface functions...
function Connect(pHostWin:HWND):BOOL;stdcall;external DLL_NAME name 'Connect';
function Disconnect:BOOL;stdcall;external DLL_NAME name 'Disconnect';
function GetItem(pIndex:UINT):UINT;stdcall;external DLL_NAME name 'GetItem';
function GetItemCount:UINT;stdcall;external DLL_NAME name 'GetItemCount';
function Read(pHandle:UINT;pData:pointer):BOOL;stdcall;external DLL_NAME name 'Read';
function Write(pHandle:UINT;pData:pointer):BOOL;stdcall;external DLL_NAME name 'Write';
function ReadEx(pVendorID,pProductID:UINT;pData:pointer):BOOL;stdcall;external DLL_NAME name 'ReadEx';
function WriteEx(pVendorID,pProductID:UINT;pData:pointer):BOOL;stdcall;external DLL_NAME name 'WriteEx';

function GetHandle(pVendorID,pProductID:UINT):UINT;stdcall;external DLL_NAME name 'GetHandle';
function GetVendorID(pHandle:UINT):UINT;stdcall;external DLL_NAME name 'GetVendorID';
function GetProductID(pHandle:UINT):UINT;stdcall;external DLL_NAME name 'GetProductID';
function GetVersion(pHandle:UINT):UINT;stdcall;external DLL_NAME name 'GetVersion';
function GetVendorName(pHandle:UINT;pText:LPSTR;pLen:UINT):UINT;stdcall;external DLL_NAME name 'GetVendorName';
function GetProductName(pHandle:UINT;pText:LPSTR;pLen:UINT):UINT;stdcall;external DLL_NAME name 'GetProductName';
function GetSerialNumber(pHandle:UINT;pText:LPSTR;pLen:UINT):UINT;stdcall;external DLL_NAME name 'GetSerialNumber';
function GetInputReportLength(pHandle:UINT):UINT;stdcall;external DLL_NAME name 'GetInputReportLength';
function GetOutputReportLength(pHandle:UINT):UINT;stdcall;external DLL_NAME name 'GetOutputReportLength';

procedure SetReadNotify(pHandle:UINT;pValue:BOOL);stdcall;external DLL_NAME name 'SetReadNotify';
function IsReadNotifyEnabled(pHandle:UINT):BOOL;stdcall;external DLL_NAME name 'IsReadNotifyEnabled';

function IsAvailable(pVendorID,pProductID:UINT):BOOL;stdcall;external DLL_NAME name 'IsAvailable';

implementation

end.

----------------------------------------------

unit cUSBInterfaceTypes;

interface

uses
   Windows, Classes, Messages;

const
   WM_HID_EVENT   = WM_APP + 200;
   NOTIFY_PLUGGED   = $0001;
   NOTIFY_UNPLUGGED  = $0002;
   NOTIFY_CHANGED   = $0003;
   NOTIFY_READ     = $0004;

implementation

end.
Now, the main program (see that funcTForm1.USBEvent is a dll event activated procedure)
unit FormMain;

interface

uses
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

const

   // input and out buffer size constants...
   BufferInSize  = 8;
   BufferOutSize = 8;
type

   // input and output buffers...
   TBufferIn = array[0..BufferInSize] of byte;
   TBufferOut = array[0..BufferOutSize] of byte;

   // main form
   TForm1 = class(TForm)
    Memo1: TMemo;
    Edit1: TEdit;
      procedure FormCreate(Sender: TObject);
      procedure FormDestroy(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
   private
      FBufferIn:TBufferIn;
      FBufferOut:TBufferOut;
      function USBEvent(var Msg: TMessage): Boolean;
   public
   end;

var
  Form1: TForm1;

implementation

uses
   cUSBInterface,
   cUSBInterfaceTypes;

const

   // vendor and product ID constants...
   VENDOR_ID           = $1234;
   PRODUCT_ID         = 0001;

{$R *.DFM}

{
****************************************************************************
* Name  : Create                                                         *
* Purpose : Create the main form                                           *
****************************************************************************
}
procedure TForm1.FormCreate(Sender: TObject);
begin
   Application.HookMainWindow(USBEvent);
   Connect(Application.Handle);
end;
{
****************************************************************************
* Name  : Destroy                                                       *
* Purpose : Free the main form                                           *
****************************************************************************
}
procedure TForm1.FormDestroy(Sender: TObject);
begin
   Application.UnHookMainWindow(USBEvent);
end;
{
****************************************************************************
* Name  : USBEvent                                                     *
* Purpose : DLL message handler hook                                       *
****************************************************************************
}
function TForm1.USBEvent(var Msg: TMessage): Boolean;
var
   DevHandle:cardinal;
//   TextBuffer:array[0..255] of char;
begin
  result := False;
  if Msg.Msg = WM_HID_EVENT then
  begin
     case Msg.WParam of

        // a HID device has been plugged in...
        NOTIFY_PLUGGED :
        begin

           // is it our HID device...
           DevHandle := Msg.LParam; // handle of HID device in this message
           if (GetVendorID(DevHandle) = VENDOR_ID) and (GetProductID(DevHandle) = PRODUCT_ID) then
           begin
           Caption := 'Cool';
              // add your code here, for example...
              // GetProductName(DevHandle, TextBuffer, 256);
              // ALabel.Caption := string(TextBuffer);
           end;
           result := true;
        end;

        // a HID device has been device removed...
        NOTIFY_UNPLUGGED :
        begin

           // is it our HID device...
           DevHandle := Msg.LParam; // handle of HID device in this message
           if (GetVendorID(DevHandle) = VENDOR_ID) and (GetProductID(DevHandle) = PRODUCT_ID) then
           begin
             Caption := 'Oops';
              // add you code here
           end;
           result := true;
        end;

        // a HID device has been attached or removed. This event is fired after
        // either NotifyPlugged or NotifyUnplugged.
        NOTIFY_CHANGED :
        begin
           // get the handle of the device we are interested in
           // and set it's read notification flag to true...
           DevHandle := GetHandle(VENDOR_ID,PRODUCT_ID);
           SetReadNotify(DevHandle,true);
           result := true;
        end;

        // a HID device has sent some data..
        NOTIFY_READ :
        begin
           DevHandle := Msg.LParam; // handle of HID device in this message
           if (GetVendorID(DevHandle) = VENDOR_ID) and (GetProductID(DevHandle) = PRODUCT_ID) then
           begin
               // read the data - remember that first byte is report ID...
               Read(DevHandle,@FBufferIn);
               Memo1.Lines.Text := Memo1.lines.Text + chr(fbufferin[1]);
               // process data here...
           end;
           result := true;
        end;
     end;
  end;
end;

(*
   if you want to write some data to the USB device, then
   try using something like this...

   // first element is report ID, set to 0...
   FBufferOut[0] := $0;

   // fill the buffer with some data...
   for index := 1 to BufferOutSize do
      FBufferOut[index] := index;

   DevHandle := GetHandle(VendorID, ProductID);
   Write(DevHandle,@FBufferOut);
*)

procedure TForm1.Edit1Change(Sender: TObject);
var s: string;
begin
  s := Edit1.Text;
  if s = '' then Exit;
  fbufferout[0] := 0;
  fbufferout[1] := ord(s[Length(s)]);
  Write(GetHandle(Vendor_ID, Product_ID), @fbufferout);
end;

end.
mcHID.zipUSB_easyhid_Pascal.zip

Jose and drdelphi

Edited by joseLB
Link to comment
Share on other sites

How do you write to The USB like a wireless commuator?

What is this?

Just to get this clear - the low level USB drivers make the USB device pretend it is a generic serial port, so any serial port software should technically work as long as you match up the port numbers and speeds - am I correct?

Link to comment
Share on other sites

What is this?

Just to get this clear - the low level USB drivers make the USB device pretend it is a generic serial port, so any serial port software should technically work as long as you match up the port numbers and speeds - am I correct?

Sounds like it should,but you are talking about drivers and that is hinky unto itself.
Link to comment
Share on other sites

  • 1 year later...

interesting read...

anyone know if is possible to intercept HID value from usb pheripericals ?

[like wiimote]

miss source for all best programs to detect wiimote over PC (glovepie and WiinRemote)

maybe is possible to have our wiimote utility (maybe a boost for autoit site)

Sorry for bad english,

m.

Link to comment
Share on other sites

this is old i guess but just wanted to say my 2 cents.

looks to me like your programing a pic via serial port, now you changed that to usb with a bluetooth usb dongle.

i have the same setup with gps, its a serial gps unit that has bluetooth, so i got my bluetooth dongle and used blue solairs to make a virtual serial com port. then i used autoit's com port udf to read and if you want write raw data to the port, just like a terminal connection.

check the forums for AUtoogle tracker, cheers!

Link to comment
Share on other sites

  • 5 years later...

I believe that what you need to do is set up a SPP serial port between the device and the Bluetooth host (the computer) and then, using the COM: port which should be created, follow the isntructions found >here. :sweating:

Sorry for the ancient thread resurrection, but this should answer the OP question.

Link to comment
Share on other sites

  • Moderators

weasel5i2,

But after nearly 8 years do you really think the OP is still interested? :huh:

Please do not necro-post this far back again. :naughty:

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...