项目中需要与com端口进行通信,第三方的DLL里有一个方法,原型如下:
public extern static int OpenComm(ref IntPtr hCom, ref byte com_port);
第一个参数是com的句柄,第二个参数是com端口的名称。如果我想与com1端口进行通信应该如何给两个参数赋值?

解决方案 »

  1.   

    为什么不用.net 自带的串口类 SerialPort命名空间:System.IO.Ports
    程序集:System(在 system.dll 中)
      

  2.   

    public extern static int OpenComm(ref IntPtr hCom, ref byte com_port);
    这是原型?
    第三方的DLL是.net写的?
      

  3.   

    用api, CreateFile可以,它返回的就是句柄
      

  4.   

    public extern static int OpenComm(ref IntPtr hCom, ref byte com_port);如果这个是第三方给出的定义,是有点奇怪,至少com_port是用不着ref修饰符的。
    调用这个函数的话:
    IntPtr hCom;
    byte cport = 1;
    int Result = OpenComm(ref hCom, ref cport);
      

  5.   

    感谢关注。因为这是一个读IC卡的DLL,还有一些其它的功能函数。
      

  6.   

    这个DLL是用C++写的,这是它给的在.net下C#的调用方法。
      

  7.   

    这样写的话h_com没有初始化,是不能通过编译的。
    它的定义是这样的
    class IcDev_ECP
        {
            const string sDllFile = "EPCDemoDll.dll";        //extern "C" __declspec(dllexport) int OpenComm(HANDLE *hCom,char *com_port);
            [DllImport(sDllFile)]
            public extern static int OpenComm(ref IntPtr hCom, ref byte com_port);        //extern "C" __declspec(dllexport) void CloseComm(HANDLE comm);
            [DllImport(sDllFile)]
            public extern static void CloseComm(IntPtr comm);
            ......
    }
    注释掉的部分好像是在c++下调用的方法。
      

  8.   

    用api, CreateFile返回的句柄传给opencomm
    不过 我怀疑你的OpenComm是否与原型一致,你最好贴上C++原型
      

  9.   

    C++的原型定义是:int OpenComm(HANDLE *hCom,char *com_port);
    这样就可以CreateFile得到句柄。
    可以它给的C#的定义是:public extern static int OpenComm(ref IntPtr hCom, ref byte com_port);
    就不知道怎么用了
      

  10.   

    C++的原型定义是:int OpenComm(HANDLE *hCom,char *com_port);
    public extern static int OpenComm(ref IntPtr hCom, StringBuilder com_port);
    StringBuilder strB =  new StringBuilder("COM1"); HANDLE h = CreateFile("COM1",............);
    OpenComm(ref h, strB);
    试试
      

  11.   

    外部引用第三方读卡动态库,一般是传一个端口号和一个波特率,返回的就是这个端口的句柄。
    --reply by CSDN Study V1.0.0.3 (starts_2000)
      

  12.   

    用.net 自带的串口类 SerialPort就很好