二维码扫描枪中文开发指导

2024-11-03 23:20:28

1、新建 C# 项目引入 dll库文件 使用下列代码:[DllImport("VirtualSendKey.dll", EntryPoint = 媪青怍牙"SendMsg", CallingConvention = CallingConvention.Cdecl)] public static extern void SendMsg(string msg);

2、新建默认串口变量SerialPort ComPort = new SerialPort();

3、初始化ComPort波特率为115200(默认)数据位 8校验位1其余为默认设置并初始化 假设 ComPort.PortName=”com3”

4、打开串口ComPort.Open()在这个位置可使用try catch 函数捕获错误信息

5、为DataReceived生成事件 下面是代码private void ComPort_Data伊怕锱鳏Received(object sender, SerialDataReceivedEventArgs e) { / ****此处省略***/。 this.Invoke((EventHandler)(delegate { / ****此处省略***/ SendMsg(str); //调用dll中函数将数据发送到任意可编辑窗口 })); }

6、当winform 窗体关闭时 调用 ComPort.Close(); 释放资源

猜你喜欢