0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cscの作法 その638

0
Last updated at Posted at 2026-10-03

概要

cscの作法、調べてみた。
comポート番号の取得、見つけたので、やってみた。

参考にしたページ

写真

image.png

using System;
using System.IO.Ports;
using System.Threading;
using System.Management;
using System.Text.RegularExpressions;

namespace Com_display
{
	public partial class Com_checker: Form {
		private System.Windows.Forms.Timer timer1;
		private ListBox listBox1;
		private ListBox listBox2;
		string[] com_num_text = new string[100];
		public Com_checker() {
			Text = "Com_checker";
			Size = new Size(600, 400);
			listBox1 = new ListBox();
			listBox1.Location = new Point(100, 64);
			listBox1.Size = new Size(198, 186);
			listBox1.TabIndex = 3;
			Controls.Add(listBox1);
			listBox2 = new ListBox();
			listBox2.Location = new Point(400, 64);
			listBox2.Size = new Size(108, 186);
			listBox2.TabIndex = 3;
			Controls.Add(listBox2);
			timer1 = new System.Windows.Forms.Timer();
			timer1.Interval = 10000; 
			timer1.Tick += new EventHandler(timer1_Tick);
			timer1.Start();
		}
		private void Form1_Load(object sender, EventArgs e) {
			timer1.Enabled = true;
		}
		public void update() {
			string[] ports = GetDeviceNames(ref com_num_text);
			listBox1.Items.Clear();
			listBox2.Items.Clear();
			int index = 0;
			foreach (string port in ports)
			{
				listBox1.Items.Add(port);
				listBox2.Items.Add(com_num_text[index]);
				index++;
			}
			if (listBox1.Items.Count > 0) 
				listBox1.SelectedIndex = 0;
		}
		public void update_serial() {
			listBox1.Items.Clear ();
			string[] ports = SerialPort.GetPortNames();
			foreach (string port in ports)
			{
				listBox1.Items.Add(port);
			}
			if (listBox1.Items.Count > 0) 
				listBox1.SelectedIndex = 0;
		}
		public static string[] GetDeviceNames(ref string[] com_num_text_array) {
			var deviceNameList = new System.Collections.ArrayList();
			var check = new Regex("(COM[1-9][0-9]?[0-9]?)");
			ManagementClass mcPnPEntity = new ManagementClass("Win32_PnPEntity");
			ManagementObjectCollection manageObjCol = mcPnPEntity.GetInstances();
			//全てのPnPデバイスを探索しシリアル通信が行われるデバイスを随時追加する
			foreach (ManagementObject manageObj in manageObjCol)
			{
				//Nameプロパティを取得
				var namePropertyValue = manageObj.GetPropertyValue("Name");
				if (namePropertyValue == null)
				{
					continue;
				}
				//Nameプロパティ文字列の一部が"(COM1)~(COM999)"と一致するときリストに追加"
				string name = namePropertyValue.ToString();
				if (check.IsMatch(name))
				{
					deviceNameList.Add(name);
				}
			}
			//戻り値作成
			if (deviceNameList.Count > 0)
			{
				string[] deviceNames = new string[deviceNameList.Count];
				int index = 0;
				foreach (var name in deviceNameList)
				{
					var com_num = Regex.Match(name.ToString(), @"(COM[1-9][0-9]?[0-9]?)");
					com_num_text_array[index] = com_num.ToString();
					deviceNames[index++] =  name.ToString();
				}
				return deviceNames;
			}
			else
			{
				return null;
			}
		}
		private void timer1_Tick(object sender, EventArgs e) {
			update();
		}
		[STAThread]
		public static void Main() {
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new Com_checker());
		}
	}
}




以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?