| |

VerySource

 Forgot password?
 Register
Search
View: 893|Reply: 8

Urgent: Serial port call failed?

[Copy link]

4

Threads

12

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-2-20 23:30:02
| Show all posts |Read mode
Attendance machine dynamic library description:
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 // Failed to call the interface
101 // This type of device is not supported
102 // The device does not support this interface
103 // The device is not responding
104 // parameter error
105 // The call was successful
106 // The serial port is not open
107 // Error opening file
108 // Communication parameter error, request resend
109 // Return value data check error
110 // Create thread error
111 // The device is busy

I. IT2100
Open 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




I tested:
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;

var
  Form1: TForm1;


implementation

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

procedure TForm1.Button1Click (Sender: TObject);
var
  p: TParams;
  sType, ret: Integer;
begin
  sType: = 2100;
  p.Port: = edit1.text;
  p.CtrlID: = edit2.Text;
  ret: = OpenComm (p, sType);
    showmessage (inttostr (ret));


  ret: = CloseComm (p, sType);
   p.CtrlID: = '';
    showmessage (inttostr (ret));
 
end;
end.


Opening and closing the serial port both returned 100 (failed to call the serial port), and using the terminal management of the time attendance machine can open the first and second serial ports, I do n’t know if there is something wrong with my test program
Reply

Use magic Report

0

Threads

53

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-5-3 08:45:02
| Show all posts
Open the serial port, are you sure that no other program is occupying the serial port you want to open,
Reply

Use magic Report

4

Threads

12

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

 Author| Post time: 2020-5-6 19:00:01
| Show all posts
No, I can use the terminal management of the attendance machine to open the first and second serial ports, which can be closed
Reply

Use magic Report

0

Threads

22

Posts

23.00

Credits

Newbie

Rank: 1

Credits
23.00

 China

Post time: 2020-5-7 07:30:01
| Show all posts
PParams = ^ TParams;
var p: PParams; OpenComm (p, sType);
Or p: TParams; OpenComm (^ p, sType);
The first parameter of OpenComm is the address
Reply

Use magic Report

4

Threads

12

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

 Author| Post time: 2020-5-8 12:30:01
| Show all posts
PParams = ^ TParams;
var p: PParams; OpenComm (p, sType);
Or p: TParams; OpenComm (^ p, sType);
The first parameter of OpenComm is the address

After changes, the editor prompts an error: types of actual and formal var parametes must be identical
Reply

Use magic Report

0

Threads

53

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-5-10 01:45:02
| Show all posts
It is a reference by address, but var is also used in the declaration of delphi, which already corresponds to the byref of VB.
The problem is not here, should your serial device parameters be set in the Params character array in the TPams record?
Reply

Use magic Report

4

Threads

12

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

 Author| Post time: 2020-5-13 15:45:02
| Show all posts
Modified to the following code:

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;
  p.Port: = edit1.text;
  p.CtrlID: = edit2.Text;

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


  ret: = CloseComm (p, sType);
   p.CtrlID: = '';
    showmessage (inttostr (ret));
 
end;

end.

The edit was successful, and when running, access violation at address 0040211 ... address error
Reply

Use magic Report

4

Threads

12

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

 Author| Post time: 2020-5-14 09:30:01
| Show all posts
00402A11,0042C590
Reply

Use magic Report

4

Threads

12

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

 Author| Post time: 2020-5-16 15:15:01
| Show all posts
Is in p.Port: = edit1.text;
  p.CtrlID: = edit2.Text;
There is an address error in these two places, it is the same to change to p.port: = '001'
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