gpt4 book ai didi

delphi - delphi中如何使用MetroCom.dll启动com端口

转载 作者:行者123 更新时间:2023-12-03 18:32:06 27 4
gpt4 key购买 nike

我在 Delphi 中编写了一个代码来启动连接到我的设备的 com 端口,但出现错误 access violation at address 0x0000我写的代码是:

unit Test_Cam;

interface

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

type
TByteArr = array of byte;
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
ComboBox1: TComboBox;



procedure Button1Click(Sender: TObject);
procedure Doconnect;
procedure CamOpen;
Function StrToByte(const Value: String): TByteArr;
private
{ Private declarations }


public
published
property ObjectMenuItem;
{ Public declarations }
end;
TconfigPort = record
BaudRate:Integer; // baud rate
ByteSize:Integer; // number of bits/byte, 4-8
Parity:Integer; // 0-4=no,odd,even,mark,space
StopBits:Integer; // 0,1,2 = 1, 1.5, 2
fOutxCtsFlow:Integer; // CTS Flow Control
end;

TPROGRESS_FUNC = procedure (blocks_total,blocks_done:Integer) of Object;

var
Form1: TForm1;

Function MetrocomEnterImageUpl(nport:Integer):Boolean; external 'metrocom.dll';
procedure MetrocomAcquireFullFrame(nport:Integer); external 'metrocom.dll';


Function MetrocomUploadJpeg(nPort:Integer; PROGRESS_FUNC:Tprogress_func; szFileName:string; nSubsampling:integer):Boolean; external 'metrocom.dll';
Function MetrocomExitImageUpl(nPort:Integer):Boolean; external 'metrocom.dll';
Function MetrocomInitCommunication(nPort:Integer; COMMPORTCONFIG:TconfigPort):Boolean; external 'metrocom.dll';
Function MetrocomEndCommunication(nPort:Integer):Boolean; external 'metrocom.dll';
Function camSetImageCompression(cam_handle:Integer; quality:Integer; method:Integer):Integer; external 'Camlib.dll';
Function camSetCenteredWOI(cam_handle:Integer;img_width:Integer;img_height:Integer):Integer; external 'Camlib.dll';
Function camInit(dev_interface:Integer):Integer; external 'Camlib.dll';
Function camOpenEx2(device_name:TByteArr;ctl_bus_name:Byte;Var p_dev_type:Integer; ctl_bus_type:integer; camera_type:integer;dev_bus_type:Integer):Integer; external 'Camlib.dll';
Function camClose(cam_handle:Integer):Integer; external 'Camlib.dll';
Function camFree:Integer; external 'Camlib.dll';
Function camGetOperatingMode(cam_handle:Integer; var mode:Integer):Integer; external 'Camlib.dll';

var
Config_port: TconfigPort;
m_bConnect:Boolean = false;
m_nPort,m_nInit,nPort,i,j,m_camHandle:integer;
nDeviceType:Integer = 0;
device_name:string;
szDeviceName:TByteArr;

implementation

{$R *.dfm}

function TForm1.StrToByte(const Value: String): TByteArr;
var
I: integer;
begin
SetLength(Result, Length(Value));
for I := 0 to Length(Value) - 1 do
Result[I] := ord(Value[I + 1]) ;
end;

procedure Tform1.DoConnect;
begin
m_nPort := StrToInt(ComboBox1.Text) - 1;
Config_port.BaudRate := 9600;
Config_port.ByteSize := 8;
Config_port.Parity := 0;
Config_port.StopBits := 0;
m_bConnect := MetrocomInitCommunication(m_nPort , Config_port);
end;

procedure TForm1.CamOpen;
begin
m_nInit := camInit(0);
if m_nInit < 0 then
ShowMessage('Fail to camInit.');
nPort := m_nPort + 1;
device_name := 'COM' + IntToStr(nPort) + ' baud=' + IntToStr(Config_port.BaudRate) + ' parity=' + IntToStr(Config_port.Parity) + ' data=' + IntToStr(Config_port.ByteSize) + ' stop=' + IntToStr(Config_port.StopBits);
szDeviceName := strtobyte(device_name);
for i := 0 to 4 do
begin
for j := 0 to 4 do
begin
m_camHandle := camOpenEx2(szDeviceName, 0 , nDeviceType, 0, 1, 0);
if m_camHandle < 0 then break;
end;
if m_camHandle < 0 then break;
end;

end;


procedure TForm1.Button1Click(Sender: TObject);
begin
DoConnect;
CamOpen;
camSetImageCompression(m_camHandle, 30, 0);
camSetCenteredWOI(m_camHandle, 500, 220);

end;

end.

DLL 文件的文档是:
MetrocomInitCommunication
Purpose:
Open the port for communication and set up the ports parameters. The communication parameters only
matter when a real COM port is used for either USB or Bluetooth virtual COM ports these settings are
ignored.
C/C++:
BOOL MetrocomInitCommunication(int i_port, COMMPORTCONFIG * p_config);
Visual Basic:
Declare Function MetrocomInitCommunication Lib "metrocom.dll" _
(ByVal i_port As Long, ByRef config As COMMPORTCONFIG) As Long
Parameters:
i_port - [in] Communication port number (0 for COM1, 1 for COM2, etc.)
p_config - [in] Port configuration structure this parameter sets baud rate and other port settings to be
used during normal communication (not during image upload)
Return value:
1 if the parameters were set successfully and the port was opened, 0 otherwise.

最佳答案

导入函数的调用约定不正确。您没有指定调用约定,因此使用默认寄存器约定。请查阅文档以了解使用的调用约定。您提供的 VB6 声明使用 stdcall,所以这就是我的钱所在。所有 VB6 导入都是 stdcall。翻译标题时,始终确保正确指定调用约定

修复后,互操作将出现更多问题。对象的使用显然是错误的。 C++ 库不会实现它。同样错误的是使用 Delphi 字符串。字符串可能会作为 C 字符串、指向以空字符结尾的字符数组的指针传递。并且结构可能会通过引用而不是值传递。

对于有问题的函数,C++ 声明清楚地表明该结构是通过引用传递的。

该函数应该像这样声明:

function MetrocomInitCommunication(
nPort: Integer;
const config: TConfigPort
): BOOL; stdcall; external 'metrocom.dll';

关于delphi - delphi中如何使用MetroCom.dll启动com端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18967525/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com