| |

VerySource

 Forgot password?
 Register
Search
View: 815|Reply: 5

The problem of dynamic library call (vb example to delphi)

[Copy link]

4

Threads

12

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 United States

Post time: 2020-3-8 16:30:01
| Show all posts |Read mode
Attendance machine dynamic library description (example about vb):
Explanation: All functions only accept two parameters
Parameter one: Params structure
Parameter 2: A value indicating the type of hardware, value [30, 60, 2000, 2100, 2200, 2300, 2600]

Params structure definition
Public Type Params
    Port As String * 3 // Serial port number, [1 ~ 255]
    CtrlID As String * 3 // device number, [1 ~ 127]
    Params As String * 1024 // Input: The parameter table passed to the device for processing. Multiple parameters are separated by semicolons and terminated with semicolons.
                           // Output: Interface return value
End Type

Interface return value definition
100 // Call interface failed
101 // This type of device is not supported
102 // This device does not support this interface
103 // device is not responding
104 // Parameter error
105 // The call was successful
106 // The serial port is not open
107 // Open file error
108 // Communication parameter error, resend
109 // Return value data check error
110 // Create thread error
111 // device is busy

I. IT2100
1. Open the serial port
statement
Public Declare Function OpenComm Lib "CM60.dll" (ByRef lParam As Params, ByVal sType As Integer) As Integer
Call example
dim p as Params
dim sType as Integer
dim ret as Long

sType = 2100
p.Port = "001" ‘Open serial port one
p.CtrlID = "001" ‘device number is 1

ret = OpenComm (p, sType)
select case ret
case 100‘open failed
case 105 ‘Successful opening of serial port
case else
    end select

2. Close the serial port
statement
Public Declare Function CloseComm Lib "CM60.dll" (ByRef lParam As Params, ByVal sType As Integer) As Integer
Call example
Dim p as Params
Dim sType as Integer
Dim ret as Long

sType = 2100
p.Port = ”001” ‘Close the serial port
p.CtrlID = ”” ‘device number ignored

ret = CloseComm (p, sType)
select case ret
case 100 ‘Failed to close the serial port
case 105 ‘Successful closing of the serial port
case else
end select


I am now testing under DELPHI:
unit Ukq;

interface

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

type
  TForm1 = class (TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    procedure Button1Click (Sender: TObject);
  
  private
    {Private declarations}
  public
    {Public declarations}
  end;

type
  TParams = Record
    Port: String [3];
    CtrlID: String [3]; // device number, [1 ~ 127]
    Params: string [250];
  end;
 pparams = ^ TParams;


var
  Form1: TForm1;


implementation

{$ R * .dfm}
function OpenComm (var lParam: pParams; sType: Integer): Integer; stdcall; external 'CM60.dll';
function CloseComm (var lParam: pParams; sType: Integer): Integer; stdcall; external 'CM60.dll';

procedure TForm1.Button1Click (Sender: TObject);
var
  p: pparams;
  sType, ret: Integer;
begin
  sType: = 2100;

  new (p);
  p ^ .Port: = edit1.Text;
  p ^ .CtrlID: = edit2.Text;

  ret: = OpenComm (p, sType);
   showmessage (inttostr (ret));


 ret: = CloseComm (p, sType);
 p ^ .CtrlID: = '';
  showmessage (inttostr (ret));
  dispose (p);
 
end;
end.
The result of each test returns 100 (the serial port fails to open and close), which is the case on multiple machines. And the terminal management of the time attendance machine can open the serial port every time.

I don't know what is wrong with my code?
Reply

Use magic Report

4

Threads

12

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 Japan

 Author| Post time: 2020-6-2 11:30:03
| Show all posts
Come on, Comet attendance machine
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 United States

Post time: 2020-8-20 02:15:01
| Show all posts
Attention, I am also doing something similar to this.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-20 02:30:01
| Show all posts
I feel it is a parameter definition problem, because I have never used VB, I don’t know the following definition
Public Type Params
    Port As String * 3//Serial port number, [1~255]
    CtrlID As String * 3//device number, [1~127]
    Params As String * 1024//input: the parameter table passed to the device for processing, multiple parameters are separated by a semicolon and end with a semicolon
                           //Output: interface return value
End Type
Is it to define a string or an array

Put
type
  TParams = Record
    Port: String[3];
    CtrlID: String[3]; //Device number, [1~127]
    Params: string[250];
  end;
To
type
  TParams = Record
    Port: array[0..2] of cahr;
    CtrlID: array[0..2] of cahr; //device number, [1~127]
    Params: array[0..1023] of cahr;
  end;
Try it
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-22 16:00:01
| Show all posts
My code can open the serial port, but cannot search for the device
unit UIDLL_VAR;

interface
Type Params = record
Port:array[0..2] of char;
CtrlID:array[0..2] of char;
Param:array[0..1023] of char;
IPPort:array[0..4] of char;
IPAddr:array[0..15] of char;
End;
Function SearchCtrl( var lParam:Params; sType:integer):integer; stdcall;external'cm60.dll';
Function OpenComm (var lParam:Params; sType:integer):integer; stdcall;external'cm60.dll';
Function CloseComm( var lParam:Params; sType:integer):integer; stdcall;external'cm60.dll';
Function CollectAll( var lParam:Params; sType:integer):integer; stdcall;external'cm60.dll';
var
  userid,user_name:string;

implementation

end.
////////////////////
unit URRMTEST;

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

type
  TFRMTEST = class(TForm)
    Button1: TButton;
    PB1: TProgressBar;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    GroupBox1: TGroupBox;
    RB1: TRadioButton;
    RB2: TRadioButton;
    Button2: TButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    {Private declarations}
  public
    {Public declarations}
  end;

var
  FRMTEST: TFRMTEST;

implementation
uses
    uidll_var,ufunction;

{$R *.dfm}

procedure TFRMTEST.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    action:=cafree;
end;

procedure TFRMTEST.Button1Click(Sender: TObject);
var
   j,t,b,k,h :integer;
   p:Params;
   s:string;
begin
     if rb1.Checked then
        begin

           p.Port[0]:='0';
           p.Port[1]:='0';
           p.Port[2]:='1';

           //p.Port:='001';
           P.CtrlID[0]:='0';
           P.CtrlID[1]:='0';
           P.CtrlID[2]:='1';

           p.Param[0]:='9';
           p.Param[1]:='6';
           p.Param[2]:='0';
           p.Param[3]:='0';
           //p.Param[4]:='0';
           //p.Param:='9600';
           j:=2100;
        end
     else
        begin
           p.Port[0]:='0';
           p.Port[1]:='0';
           p.Port[2]:='1';

           P.CtrlID[0]:='0';
           P.CtrlID[1]:='0';
           P.CtrlID[2]:='1';

           p.Param[0]:='1';
           p.Param[1]:='9';
           p.Param[2]:='2';
           p.Param[3]:='0';
           p.Param[4]:='0';
           j:=2201;
        end;
     b:=openComm(p,j);
     showmessage(inttostr(b)+' OK');
     if b=105 then
        begin
           showmessage('Serial port opened successfully!!');
           k:=1;
           PB1.Max:=strtoint(edit2.Text);
           pb1.Step:= 1;
           while k<= strtoint(edit2.Text) do
              begin
                case k of
                   1:
                      begin
                          P.CtrlID[0]:='0';
                          P.CtrlID[1]:='0';
                          P.CtrlID[2]:='1';
                      end;
                   2: begin
                          P.CtrlID[0]:='0';
                          P.CtrlID[1]:='0';
                          P.CtrlID[2]:='2';
                      end;
                   3: begin
                          P.CtrlID[0]:='0';
                          P.CtrlID[1]:='0';
                          P.CtrlID[2]:='3';
                      end;
                   4: begin
                          P.CtrlID[0]:='0';
                          P.CtrlID[1]:='0';
                          P.CtrlID[2]:='4';
                      end;
                   5: begin
                          P.CtrlID[0]:='0';
                          P.CtrlID[1]:='0';
                          P.CtrlID[2]:='5';
                      end;
                   6: begin
                          P.CtrlID[0]:='0';
                          P.CtrlID[1]:='0';
                          P.CtrlID[2]:='6';
                      end;
                   7: begin
                          P.CtrlID[0]:='0';
                          P.CtrlID[1]:='0';
                          P.CtrlID[2]:='7';
                      end;
                   8: begin
                          P.CtrlID[0]:='0';
                          P.CtrlID[1]:='0';
                          P.CtrlID[2]:='8';
                      end;
                end;
                s:= inttostr(k);
                //showmessage(p.Port+''+p.CtrlID);
                p.Param:='';
                b:=SearchCtrl(p,j);
                //showmessage(inttostr(b));
                if b=105 then
                   begin
                      showmessage('Successfully searched for the device!!'+s[1]);
                      Break;
                   end
                else
                   begin
                       showmessage('The device is not found successfully'+s[1]);
                   end;
                pb1.StepIt;
                //GB2.Caption:= fla(k/strtoint(edit2.Text))+'%';
                k:=k+1;
          end;
          //end;
        end
  else
      showmessage('Serial port opening is not successful');
     //b:=SearchCtrl(p,j);
end;

procedure TFRMTEST.Button2Click(Sender: TObject);
var
s,l:string;
i:integer;
begin
   {The following two methods can get the directory where the current application is located}
    getdir(0,s);
    showmessage(s);
    s:=extractfilepath(paramstr(0));
    showmessage(s);
   //**************************************//
   l:=DateTimeToStr(Now);
   showmessage(l);
   showmessage(inttostr(length(l)));
   i:=length(l);
   case i of
       17:
          begin
             s:='data'+copy(l,1,4)+copy('00'+copy(l,6,1),2,2)+copy('00'+copy(l,8,1) ,2,2)+copy(l,10,2)+copy(l,13,2)+copy(l,15,2);
             showmessage(s);
          end;
       18:
          begin
             s:=copy(l,11,2)+copy(l,14,2)+copy(l,17,2);
             if copy(l,) = then

             showmessage(s);
          end;
       19:
          begin
             s:='data'+copy(l,1,4)+copy('00'+copy(l,6,1),2,2)+copy('00'+copy(l,8,1) ,2,2)+copy(l,10,2)+copy(l,13,2)+copy(l,15,2);
             showmessage(s);
          end;
   end;
   //showmessage(inttostr(year()));
  //showmessage(FormatDateTime('"The meeting is on "dddd, mmmm d, yyyy," at "hh:mm AM/PM', Now + 0.125));


end;

end.
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-22 16:15:02
| Show all posts
I don't know if it is the problem of Comet's development library or I didn't get it right. If you modify it, you can find the device.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list