Skip to content

Commit

Permalink
device name
Browse files Browse the repository at this point in the history
  • Loading branch information
771-8bit committed Jan 7, 2024
1 parent 50ec16b commit ac84a4b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 71 additions & 6 deletions Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Management;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
Expand All @@ -16,6 +17,9 @@
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Header;

//https://kana-soft.com/tech/sample_0007.htm
namespace SerialMonitorEssential
Expand Down Expand Up @@ -53,6 +57,8 @@ private void Form1_Load(object sender, EventArgs e)

reload_COM_Click(sender, e);

cmbPortName.DropDownStyle = ComboBoxStyle.DropDownList;

initSettings();

AcceptButton = btnSend;
Expand Down Expand Up @@ -370,18 +376,77 @@ private void timerReadSerial_Tick(object sender, EventArgs e)
MessageBox.Show(ex.Message);
}

}
}
public class ItemSet
{
// DisplayMemberとValueMemberにはプロパティで指定する仕組み
public String ItemDisp { get; set; }
public String ItemValue { get; set; }

// プロパティをコンストラクタでセット
public ItemSet(String v, String s)
{
ItemDisp = s;
ItemValue = v;
}
}

List<ItemSet> src = new List<ItemSet>();
private void reload_COM_Click(object sender, EventArgs e)
{
string[] PortList = SerialPort.GetPortNames();
var deviceNameList = new System.Collections.ArrayList();

int int_Max = 0;
src.Clear();

var check = new System.Text.RegularExpressions.Regex("(COM[1-9][0-9]?[0-9]?)");

cmbPortName.Items.Clear();
//https://truthfullscore.hatenablog.com/entry/2014/01/10/180608
ManagementClass mcPnPEntity = new ManagementClass("Win32_PnPEntity");
ManagementObjectCollection manageObjCol = mcPnPEntity.GetInstances();

foreach (string PortName in PortList)
//全てのPnPデバイスを探索しシリアル通信が行われるデバイスを随時追加する
foreach (ManagementObject manageObj in manageObjCol)
{
cmbPortName.Items.Add(PortName);
//Nameプロパティを取得
var namePropertyValue = manageObj.GetPropertyValue("Name");
if (namePropertyValue == null)
{
continue;
}

//Nameプロパティ文字列の一部が"(COM1)~(COM999)"と一致するときリストに追加"
string display = namePropertyValue.ToString();
if (check.IsMatch(display))
{
// https://guminote.hatenablog.jp/entry/2015/08/13/193216
var ExtractPortNum = new System.Text.RegularExpressions.Regex(".*(COM[1-9][0-9]?[0-9]?).*");
string name = ExtractPortNum.Replace(display, "$1");

var ExtractName = new Regex(@"(.*?)\s*\(COM\d+\)");

if(name.Length == 4)
{
display = name + " " + ExtractName.Match(display).Groups[1].Value;
}
else
{
display = name + " " + ExtractName.Match(display).Groups[1].Value;
}
src.Add(new ItemSet(name, display));
int_Max = Math.Max(int_Max, display.Length);
}
}

//https://www.ampita.jp/2023/05/dropdownlist/
cmbPortName.DropDownWidth = (int_Max + 1) * 14;

// https://qiita.com/KnowledgeU/items/3388f4c461076b05f6e5
cmbPortName.DataSource = null;
cmbPortName.DataSource = src;
cmbPortName.DisplayMember = "ItemDisp";
cmbPortName.ValueMember = "ItemValue";

if (cmbPortName.Items.Count > 0)
{
cmbPortName.SelectedIndex = 0;
Expand Down Expand Up @@ -424,7 +489,7 @@ private void check_timestamp_CheckedChanged(object sender, EventArgs e)

private void connectSerial(bool message=false)
{
serialPort1.PortName = cmbPortName.SelectedItem.ToString();
serialPort1.PortName = cmbPortName.SelectedValue.ToString();

serialPort1.BaudRate = Convert.ToInt32(cmbBaudRate.SelectedItem);

Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// すべての値を指定するか、次を使用してビルド番号とリビジョン番号を既定に設定できます
// 既定値にすることができます:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("7.0.0.0")]
[assembly: AssemblyFileVersion("7.0.0.0")]
[assembly: AssemblyVersion("9.0.0.0")]
[assembly: AssemblyFileVersion("9.0.0.0")]
1 change: 1 addition & 0 deletions SerialMonitorEssential.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand Down

0 comments on commit ac84a4b

Please sign in to comment.