看看有没有帮助 //测试调用chdcomm.dll public class CHDComm { //打开串口 [DllImport("CHDComm.dll", EntryPoint="OpenCom", CharSet=CharSet.Auto)] public static extern int OpenCom1(string ComPort,int BaundRate); //关闭串口 [DllImport("CHDComm.dll", EntryPoint="CloseCom", CharSet=CharSet.Auto)] public static extern int CloseCom(); }
在程序中创建一个这个引用的实例,
就可以调用他的方法。
然后创建(new) 实例。
//测试调用chdcomm.dll
public class CHDComm
{ //打开串口
[DllImport("CHDComm.dll", EntryPoint="OpenCom", CharSet=CharSet.Auto)]
public static extern int OpenCom1(string ComPort,int BaundRate);
//关闭串口
[DllImport("CHDComm.dll", EntryPoint="CloseCom", CharSet=CharSet.Auto)]
public static extern int CloseCom(); }
private void Button1_Click(object sender, System.EventArgs e)
{
int ret1,ret2;
//打开串口
ret1 =CHDComm.OpenCom1("COM1",9600);
TextBox1.Text = ret1.ToString();
//关闭串口
ret2 = CHDComm.CloseCom();
TextBox2.Text = ret2.ToString(); }