Recent

Author Topic: how to use external dll  (Read 12676 times)

Velocity_rus

  • Newbie
  • Posts: 3
how to use external dll
« on: January 27, 2012, 10:01:35 am »
I'm novice in Lazarus and try to use Microchip SimpleIO.dll for communication with MCP2200 chip (USB to COM converter)
Microchip SimpleIO.dll written in C and all examples written for C language and Basic. I try to find any examples for Lazarus, but unsuccessfully.

dll API looks like
Code: [Select]
Simple IO API
Summary:
void __stdcall SimpleIOClass::InitMCP2200(unsigned int VendorID, unsigned int ProductID)
bool __stdcall SimpleIOClass::IsConnected()
bool __stdcall SimpleIOClass::ConfigureMCP2200(unsigned char IOMap, unsigned long BaudRateParam, unsigned int RxLEDMode, unsigned int TxLEDMode, bool FLOW, bool ULOAD, bool SSPND)
bool __stdcall SimpleIOClass::WritePort(unsigned int portValue)
bool __stdcall SimpleIOClass::ReadPort(unsigned int *returnvalue)

How to correctly declare and use this dll?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: how to use external dll
« Reply #1 on: January 27, 2012, 10:12:10 am »
That looks like C++, if you have the C wrapper (.h) then it would be possible to use. Usually hardware nowadays have wrapper for Delphi as well, if you could search for it, then 95-100% it could be used instead.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: how to use external dll
« Reply #2 on: January 27, 2012, 10:13:39 am »
That doesn't look like C, but C++ (it has classes and :: in it.

If that weren't the case, it would probably (omitting the SimpleIOCLass: bits):

procedure InitMCP2200(VendorID : cint;  ProductID : cint); stdcall; external 'dllname.dll' name 'InitMCP2200';

Velocity_rus

  • Newbie
  • Posts: 3
Re: how to use external dll
« Reply #3 on: January 27, 2012, 10:33:14 am »
That doesn't look like C, but C++ (it has classes and :: in it.

If that weren't the case, it would probably (omitting the SimpleIOCLass: bits):

procedure InitMCP2200(VendorID : cint;  ProductID : cint); stdcall; external 'dllname.dll' name 'InitMCP2200';

When I try this I get:
- Error: Identifier not found "cint"
- Error: Fields cannot appear after a method or property definition, start a new visibility section first
- Fatal: Syntax error, ":" expected but "const string" found


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: how to use external dll
« Reply #4 on: January 27, 2012, 10:37:25 am »

Quote

procedure InitMCP2200(VendorID : cint;  ProductID : cint); stdcall; external 'dllname.dll' name 'InitMCP2200';

When I try this I get:

Before trying you first need to find a solution to the fact that you don't have an ordinarily calling DLL, but seem to have something C++.

Quote
- Error: Identifier not found "cint"

Add ctypes to the "uses" clause

Quote
- Error: Fields cannot appear after a method or property definition, start a new visibility section first

This is not a class declaration. Don't put it inside a class scope.
 

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: how to use external dll
« Reply #5 on: January 27, 2012, 10:45:58 am »
Quote
all examples written for C language and Basic
Basic is probably using a COM interface. That can be used from lazarus too and is probably the easiest solution. Can you post a small Basic example?

Velocity_rus

  • Newbie
  • Posts: 3
Re: how to use external dll
« Reply #6 on: January 27, 2012, 10:53:34 am »
Quote
all examples written for C language and Basic
Basic is probably using a COM interface. That can be used from lazarus too and is probably the easiest solution. Can you post a small Basic example?

Basic code looks like

Code: [Select]
Public Class Form1
    Const mcp2200_vid As UInteger = &H4D8
    Const mcp2200_pid As UInteger = &HDF
    Dim bConnected As Boolean = False
    Dim ucIoMapValue As UShort


    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        SimpleIO.SimpleIOClass.InitMCP2200(mcp2200_vid, mcp2200_pid)
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        bConnected = SimpleIO.SimpleIOClass.IsConnected()
        If (bConnected = False) Then
            ToolStripStatusLabel1.Text = "Not Connected"
            .....
        Else
            ToolStripStatusLabel1.Text = "MCP2200 Connected"
            .....
            uiPortValue = SimpleIO.SimpleIOClass.ReadPortValue()
            TextBoxReadPortValue.Text = Hex(uiPortValue)
        End If
    End Sub


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: how to use external dll
« Reply #7 on: January 27, 2012, 11:05:59 am »
I once used Microchip based USB devices with Delphi and looked stuff up.  I however used 18F2550's that have USB built-in, not an external device.

From what I can see simpleIO is a _managed_ assembly. So this is for .NET languages C# and VB.NET ONLY.  Quoting C and VB doesn't help there.

I looked up what I used, and I got the info from this url:

http://www.sixca.com/delphi/article/microchip_usb.html

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: how to use external dll
« Reply #8 on: January 31, 2012, 08:56:23 am »
For conversion tips you can take a look at this message:
http://www.lazarus.freepascal.org/index.php/topic,12763.msg66330.html#msg66330

Generally, if you already have VB headers for this DLL then it shouldn't be much work.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: how to use external dll
« Reply #9 on: January 31, 2012, 11:07:44 am »
Generally, if you already have VB headers for this DLL then it shouldn't be much work.

He said Basic, not VB. And from what i see on the net it is VB.NET.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: how to use external dll
« Reply #10 on: February 01, 2012, 02:39:24 pm »
Generally, if you already have VB headers for this DLL then it shouldn't be much work.

He said Basic, not VB. And from what i see on the net it is VB.NET.

You are right:
http://www.microchip.com/forums/m532882.aspx

Anyway, reading MCP2200 datasheet (http://ww1.microchip.com/downloads/en/DeviceDoc/93066A.pdf) I can see that this chip is a composite USB device - both CDC and HID class compatible. CDC means that it creates a virtual COM port, which is easy to use since from a programming side it is the same as any other COM port. HID means that it can be accessed the same way as joysticks and other similar peripherals. "The HID interface of the MCP2200 is used for the additional functionalities of the devices, such as GPIO, EEPROM or special features manipulation." If you really need these features accessable only through HID, then there are many ways to access HID from Delphi. You can try translation yourself. One is JvHID in JVCL, and the other 3 are here: http://www.intel.com/intelpress/usb/examples/zipfiles/delphi_hid.htm here: http://www.lvr.com/hidpage.htm and here: http://www.soft-gems.net/supplement/download.php?ID=37.

Cross platform USB access can be done in Lazarus with libusb:
http://wiki.freepascal.org/Hardware_Access#USB
http://www.lazarus.freepascal.org/index.php/topic,15455.msg82984.html#msg82984


FTDI chip seams to me easier to use then MCP2200. If 4800bps is enough for your purpose, you can even use this software only AVR USB CDC implementation:
http://www.recursion.jp/avrcdc
« Last Edit: February 01, 2012, 03:23:48 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

 

TinyPortal © 2005-2018