Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在delphi中使用MetroCom.dll启动com端口_Delphi_Winapi_Port - Fatal编程技术网

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

如何在delphi中使用MetroCom.dll启动com端口,delphi,winapi,port,Delphi,Winapi,Port,我在Delphi中编写代码来启动连接到我的设备的com端口,在地址0x0000处出现错误访问冲突我编写的代码是: unit Test_Cam; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TByteArr = array of byte; TForm1 = class(TForm) B

我在Delphi中编写代码来启动连接到我的设备的com端口,在地址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.   

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

修复后,互操作将出现更多问题。使用宾语显然是错误的。C++库将不实现此功能。使用Delphi字符串也是错误的。字符串可能会作为C字符串传递,C字符串是指向以null结尾的字符数组的指针。结构可能是通过引用而不是值传递的

对于所讨论的函数,C++声明明确表明结构是按引用传递的。 函数的声明应如下所示:

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

不要让我们猜测哪一行抛出错误。错误发生在何时执行此函数MetroCoMinitCommunications此处是发生的错误:过程Tform1.DoConnect;开始m_nPort:=stroint(ComboBox1.Text)-1;配置端口波特率:=9600;Config_port.ByteSize:=8;配置端口奇偶性:=0;Config_port.StopBits:=0;m_b连接:=MetrocomInitCommunication(m_n端口、配置端口);结束;在执行函数后,正好在这一行中:MetrocomInitCommunication(m_nPort,Config_port)感谢您的帮助,我更改了all函数并更新了代码,但在编译后,我在这行中遇到了另一个问题,我在地址0x0000中遇到了访问冲突。您能帮助我吗?请参阅:tt:=camSetImageCompression(m_camHandle,30,2)中的错误行;camSetCenteredWOI(m_camHandle,500220);编译好函数后,我得到了错误我在答案中解释了函数,我从函数中得到了值0,它是成功的,但在执行函数后出现了错误。我没有回答你问的问题吗?你现在问的是另一个API调用吗?hanks向你寻求帮助,但当我做更改时,我得到了相同的错误,但现在在不同的代码位置
function MetrocomInitCommunication(
    nPort: Integer; 
    const config: TConfigPort
): BOOL; stdcall; external 'metrocom.dll';