Example #1
0
		private void ButtonAddVirtualComPort_Click(object sender, EventArgs e)
		{
			VirtualComPort newPort;
			try
			{
				newPort = new VirtualComPort(virtualComPortRootKey, virtualComPortDll,
					virtualComPortFriendlyName);
				if (! newPort.DriverLoaded)
				{
					MessageBox.Show("Failed to load driver for new virtual COM port. There may not be a free COM port number.",
						"Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
					newPort = null;
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show("Failed to create new virtual COM port object: " + ex.Message, "Error",
					MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
				newPort = null;
			}
			if (newPort != null)
			{
				// Successfully loaded the driver for a new virtual COM port. Add it to the array.
				VirtualComPort[] newPorts = new VirtualComPort[virtualComPorts.Length + 1];
				virtualComPorts.CopyTo(newPorts, 0);
				newPorts[virtualComPorts.Length] = newPort;
				virtualComPorts = newPorts;

				// Update the saved configuration.
				UpdateVirtualComPortsConfiguration();

				// Update the user interface.
				RepopulateVirtualComPortsDialog();
			}
		}
Example #2
0
		private void ButtonRemoveVirtualComPort_Click(object sender, EventArgs e)
		{
			int i = virtualComPortsDialog.VirtualComPortListBox.SelectedIndex;
			if (i >= 0)
			{
				// Dispose of the COM port.
				if (virtualComPorts[i] != null)
					virtualComPorts[i].Dispose();

				// Remove it from the array.
				VirtualComPort[] newPorts = new VirtualComPort[virtualComPorts.Length - 1];
				for (int j = 0; j < i; j++) 
					newPorts[j] = virtualComPorts[j];
				for (int j = i + 1; j < virtualComPorts.Length; j++)
					newPorts[j - 1] = virtualComPorts[j];
				virtualComPorts = newPorts;

				// Update the saved configuration.
				UpdateVirtualComPortsConfiguration();

				// Update the user interface.
				RepopulateVirtualComPortsDialog();
			}
		}
Example #3
0
		private void LoadVirtualComPortDrivers()
		{
			// First clear out the virtual COM port key.
			string[] subKeyNames = virtualComPortRootKey.GetSubKeyNames();
			foreach (string keyName in subKeyNames)
				virtualComPortRootKey.DeleteSubKeyTree(keyName);

			// Load the list of virtual COM ports to be created.
			string[] virtualComPortNames;
			string virtualComPortSetting = configSettings.GetString("Virtual COM Ports", "").Trim();
			if (virtualComPortSetting == "")
			{
				virtualComPortNames = new string[0];
			}
			else
			{
				virtualComPortNames = System.Text.RegularExpressions.Regex.Split(virtualComPortSetting,
					@"\s*,\s*", System.Text.RegularExpressions.RegexOptions.None);
			}

			// Create the virtual COM ports.
			int i = 0;
			virtualComPorts = new VirtualComPort[virtualComPortNames.Length];
			foreach (string portName in virtualComPortNames)
			{
				try
				{
					virtualComPorts[i] = new VirtualComPort(virtualComPortRootKey,
						virtualComPortDll, virtualComPortFriendlyName, portName);
					if (! virtualComPorts[i].DriverLoaded)
						MessageBox.Show("Error loading virtual COM port driver for " + portName,
							"Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
					i++;
				}
				catch (Exception e)
				{
					virtualComPorts[i] = null;
					MessageBox.Show("Error creating virtual COM port object for " + portName + ": " +
						e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand,
						MessageBoxDefaultButton.Button1);
				}
			}

			// Resize the virtual COM port array if we had any catastrophic failures.
			if (i < virtualComPortNames.Length)
			{
				VirtualComPort[] newPorts = new VirtualComPort[i];
				for (int j = 0; j < i; j++)
					newPorts[j] = virtualComPorts[j];
				virtualComPorts = newPorts;
			}
		}