Reuse virtual com port

cardmaker

Member
Joined
Jan 12, 2019
Messages
21
Programming Experience
Beginner
Hello, im trying to reuse the usb serial virtual at second time but the aplication hang, at first use it works all ok, but when i close port or close aplication i cannot use again virtual com, i must unplug usb cable and reconnect to work.

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;

namespace vcp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button2.Enabled = false;
            label2.Visible = false;
            SerialPort.GetPortNames();
            string[] ports = SerialPort.GetPortNames();
            comboBox2.Items.AddRange(ports);
            comboBox2.SelectedIndex = 0;
            
        }

        System.IO.Ports.SerialPort sp = new System.IO.Ports.SerialPort();
      
    
        
        private void button1_Click(object sender, EventArgs e)
        {
            sp.PortName = comboBox2.Text;
            sp.BaudRate = 9600;
            sp.Parity = Parity.Even;// None;
            sp.DataBits = 8;
            sp.StopBits = StopBits.One;
            button2.Enabled = true;
            button1.Enabled = false;
            label2.Text = comboBox2.Text + " OPEN:";
            label2.ForeColor = System.Drawing.Color.Green;
            label2.Visible = true;

           byte[] received = new byte[256];
            
           try
           {
               sp.Open();

               sp.RtsEnable = false;
               System.Threading.Thread.Sleep(500);
            
               int bytesRead = sp.Read(received, 0, received.Length);
               if (bytesRead != 0)
               {
                   string rec = BitConverter.ToString(received);
                   textBox1.Text = rec;
                  
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            sp.Close();
            sp.Dispose();
            button2.Enabled = false;
            button1.Enabled = true;
            textBox1.Clear();
            label2.Visible = false;
            label2.Text = comboBox2.Text + " CLOSED:";
      
        }

  


    }
}

anyone have an sugestion?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,465
Location
Chesapeake, VA
Programming Experience
10+
Try using different hardware/driver combination.

Based on a quick scan of your code above, the code looks right with regards to closing and disposing resources. Even more significant is the fact that even closing the application was not releasing the port. From the Windows point of view, when an application is killed, it releases any resources it may have acquired.
 
Top Bottom