Automatically create virtual serial port

This is implemented in combination with the referenced VSPD7.2 DLL. If you are interested in the details, please refer to the VSPD user help manual to help you write the interface class.

Create virtual serial port interface class code

[DllImport("vspdctl.dll")]//引用VSPD的DLL
        public static extern bool CreatePair(string comName1, string comName2);

Mybox--the name of the ComboBox that stores the serial port name

Myport--serial port control name

Automatically create a virtual serial port method 2022.9.29------New method, optimized

public void ConnetedCreatePort(SerialPort Myport, ComboBox Mybox)//连接时自动创建虚拟串口并打开
{
            string Buffer="";//存储打开的串口1
            string Buffer2="";//存储打开的串口2
            int index = 1;//串口号
            while(true)
            {
                Buffer = "COM" + index.ToString();
                Buffer2 = "COM" + (index + 1).ToString();//串联串口命名
                if (SerialPort.GetPortNames().Contains(Buffer) || SerialPort.GetPortNames().Contains(Buffer2))//判断名字是否存在
                    index++;
                else
                    break;//没有目前要创建的名字则跳出
            }
            bool isSuccessed = CreatePair(Buffer, Buffer2);//等待创建串口
            if(isSuccessed)//成功则加入框
            {
                Mybox.Items.Add(Buffer);//添加进下拉框
                Mybox.Items.Add(Buffer2);//添加进下拉框
                Mybox.Text = Buffer;
                Myport.PortName = Mybox.Text;
            }
            else
            {
                MessageBox.Show("创建虚拟串口发生未知错误。", "提示");
            }
}

Automatically create a virtual serial port method.2022.7.28--------Old method↓ (an error will occur if the Combbox is not updated)

public void ConnetedCreatePort(SerialPort Myport, ComboBox Mybox)//连接时自动创建虚拟串口并打开
        {
            string Buffer;//存储打开的串口1
            string Buffer2;//存储打开的串口2
            for (int i = 1; i < Mybox.Items.Count+1; i++)//以下拉框获取的串口数来作为循环,+1是为了创建串口
            {
                try
                {
                    Buffer = "COM" + i.ToString();
                    Buffer2 = "COM" + (i + 1).ToString();//串联串口命名
                    if (Mybox.FindStringExact("COM" + i.ToString())==-1)//假如检索到没有此串口存在则去创建串口
                    {
                        bool isSuccessed = CreatePair(Buffer, Buffer2);//如果创建串口不成功,则会catch,并重新循环

                        if (isSuccessed)//如果创建成功则打开
                        {
                            //MessageBox.Show("程序已帮你自动生成虚拟串口,后面需要修改请打开设置。", "提示");
                            Thread.Sleep(1000);//暂停一下,使系统能够识别串口
                            Myport.PortName = Buffer;
                            Myport.Open();//如果尝试打开成功,创建成功
                            break;//串口打开跳出循环
                        }
                    }   
                }
                catch
                {

                }
            }
            Mybox.Text = Myport.PortName;//将串口Combox显示为创建的串口
        }

After successful creation, it can be recognized by the system.

Guess you like

Origin blog.csdn.net/aa989111337/article/details/126050217