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

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_DataReceived(object sender, SerialDataReceivedEventArgs e)

        {

            / ****此处省略***/。 

            this.Invoke((EventHandler)(delegate

            {     

              / ****此处省略***/               

                 SendMsg(str);  //调用dll中函数将数据发送到任意可编辑窗口

            }));                       

        }

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

发布了31 篇原创文章 · 获赞 5 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/Pei_hua100/article/details/104540259