Skip to content
This repository was archived by the owner on Jan 2, 2026. It is now read-only.

Commit 7eb874a

Browse files
authored
Merge pull request #3 from stop-pattern/feature/serial
設定画面表示とある程度のBIDS互換
2 parents de30c4f + 2e9873d commit 7eb874a

20 files changed

Lines changed: 2424 additions & 30 deletions

CommEx/CommEx.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
88
<Deterministic>false</Deterministic>
99
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
10+
<UseWPF>true</UseWPF>
1011
</PropertyGroup>
1112

1213
<ItemGroup>
@@ -18,4 +19,11 @@
1819
</PackageReference>
1920
</ItemGroup>
2021

22+
<ItemGroup>
23+
<Reference Include="PresentationCore" />
24+
<Reference Include="PresentationFramework" />
25+
<Reference Include="System.Windows.Forms" />
26+
<Reference Include="System.Xaml" />
27+
<Reference Include="WindowsBase" />
28+
</ItemGroup>
2129
</Project>

CommEx/Serial/Bids/Serial.cs

Lines changed: 439 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows.Data;
8+
using System.Windows.Media;
9+
10+
namespace CommEx.Serial.Common
11+
{
12+
/// <summary>
13+
/// bool 型を Color に変換するコンバータ
14+
/// </summary>
15+
public class BoolToColorConverter : IValueConverter
16+
{
17+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
18+
{
19+
if (value is bool isOpen)
20+
{
21+
return isOpen ? Brushes.Green : Brushes.Red;
22+
}
23+
24+
return Brushes.Gray; // デフォルト色
25+
}
26+
27+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
28+
{
29+
if (value is Brush brush)
30+
{
31+
if (brush == Brushes.Green)
32+
{
33+
return true;
34+
}
35+
else if (brush == Brushes.Red)
36+
{
37+
return false;
38+
}
39+
else if (brush == Brushes.Gray)
40+
{
41+
return false;
42+
}
43+
}
44+
45+
throw new InvalidOperationException("Unsupported conversion");
46+
}
47+
}
48+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Diagnostics;
5+
using System.Globalization;
6+
using System.Linq;
7+
using System.Reflection;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
11+
namespace CommEx.Serial.Common
12+
{
13+
#region StringConverter
14+
15+
/// <inheritdoc/>
16+
/// <summary>
17+
/// Enum の Value と String を変換するコンバータ
18+
/// String はそのまま Enum の Value として扱われる
19+
/// </summary>
20+
public class EnumToStringConverter : EnumConverter
21+
{
22+
public EnumToStringConverter(Type type) : base(type)
23+
{
24+
if (!type.IsEnum)
25+
{
26+
throw new ArgumentException("Type must be an Enum.");
27+
}
28+
}
29+
/// <summary>
30+
/// Enum の Value から String を取得
31+
/// Enum.Value -> string
32+
/// </summary>
33+
/// <param name="value">Enum.value</param>
34+
/// <param name="destinationType">string</param>
35+
/// <returns>string</returns>
36+
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
37+
{
38+
if (destinationType == typeof(string) && value != null)
39+
{
40+
return value.ToString();
41+
}
42+
return base.ConvertTo(context, culture, value, destinationType);
43+
}
44+
45+
/// <summary>
46+
/// String から Enum の Value を取得
47+
/// string -> Enum.Value
48+
/// </summary>
49+
/// <param name="value">string</param>
50+
/// <returns>Enum.value</returns>
51+
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
52+
{
53+
if (value is string stringValue)
54+
{
55+
return Enum.Parse(EnumType, stringValue);
56+
}
57+
return base.ConvertFrom(context, culture, value);
58+
}
59+
}
60+
61+
#endregion
62+
63+
#region DescriptionConverter
64+
65+
/// <inheritdoc/>
66+
/// <summary>
67+
/// Enum の Value とその Description を変換するコンバータ
68+
/// </summary>
69+
public class EnumToDescriptionConverter : EnumConverter
70+
{
71+
public EnumToDescriptionConverter(Type type) : base(type)
72+
{
73+
if (!type.IsEnum)
74+
{
75+
throw new ArgumentException("Type must be an Enum.");
76+
}
77+
}
78+
79+
/// <summary>
80+
/// Enum の Value から DescriptionAttribute を取得
81+
/// Enum.Value -> string
82+
/// </summary>
83+
/// <param name="value">Enum.value</param>
84+
/// <param name="destinationType">string</param>
85+
/// <returns>string</returns>
86+
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
87+
{
88+
if (destinationType == typeof(string) && value != null)
89+
{
90+
var fieldInfo = value.GetType().GetField(value.ToString());
91+
var descriptionAttribute = fieldInfo?.GetCustomAttribute<DescriptionAttribute>();
92+
if (descriptionAttribute != null)
93+
{
94+
return descriptionAttribute.Description;
95+
}
96+
}
97+
return base.ConvertTo(context, culture, value, destinationType);
98+
}
99+
100+
/// <summary>
101+
/// Enum の DescriptionAttribute から Value を取得
102+
/// string -> Enum.Value
103+
/// </summary>
104+
/// <param name="value">string</param>
105+
/// <returns>Enum.value</returns>
106+
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
107+
{
108+
if (value is string stringValue)
109+
{
110+
foreach (var field in EnumType.GetFields(BindingFlags.Public | BindingFlags.Static))
111+
{
112+
var descriptionAttribute = field.GetCustomAttribute<DescriptionAttribute>();
113+
if (descriptionAttribute != null && descriptionAttribute.Description == stringValue)
114+
{
115+
return Enum.Parse(EnumType, field.Name);
116+
}
117+
}
118+
}
119+
return base.ConvertFrom(context, culture, value);
120+
}
121+
}
122+
123+
#endregion
124+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
using BveEx.PluginHost;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO.Ports;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace CommEx.Serial.Common
10+
{
11+
public interface ISerialControl
12+
{
13+
/// <summary>
14+
/// ポートを開ける前に呼ばれる
15+
/// </summary>
16+
/// <param name="serialPort"><see cref="SerialPort"/></param>
17+
void PortOpen(SerialPort serialPort);
18+
19+
/// <summary>
20+
/// ポートを閉じた後に呼ばれる
21+
/// </summary>
22+
/// <param name="serialPort"><see cref="SerialPort"/></param>
23+
void PortClose(SerialPort serialPort);
24+
25+
/// <summary>
26+
/// シリアルポートの受信時に呼ばれる
27+
/// </summary>
28+
/// <param name="sender"><see cref="SerialPort"/></param>
29+
/// <param name="e"></param>
30+
//void DataReceived(object sender, SerialDataReceivedEventArgs e);
31+
}
32+
33+
interface IBveEx
34+
{
35+
/// <summary>
36+
/// 全ての BveEx 拡張機能が読み込まれ、BveEx.PluginHost.Plugins.Extensions プロパティが取得可能になると発生
37+
/// </summary>
38+
/// <param name="sender"></param>
39+
/// <param name="e"></param>
40+
void AllExtensionsLoaded(object sender, EventArgs e);
41+
42+
/// <summary>
43+
/// シナリオ読み込み
44+
/// </summary>
45+
/// <param name="e"></param>
46+
void OnScenarioCreated(ScenarioCreatedEventArgs e);
47+
48+
/// <summary>
49+
/// シナリオ読み込み中に毎フレーム呼び出される
50+
/// </summary>
51+
/// <param name="elapsed"></param>
52+
void Tick(TimeSpan elapsed);
53+
54+
/// <summary>
55+
/// シナリオ終了
56+
/// </summary>
57+
/// <param name="e"></param>
58+
void ScenarioClosed(EventArgs e);
59+
}
60+
61+
/// <summary>
62+
/// 入力されたキーを送信する
63+
/// </summary>
64+
/// <param name="nInputs"></param>
65+
/// <param name="pInputs"></param>
66+
/// <param name="cbsize"></param>
67+
//[DllImport("user32.dll", SetLastError = true)]
68+
//public extern static void SendInput(int nInputs, Input[] pInputs, int cbsize);
69+
70+
/// <summary>
71+
/// BveHacker と Native の初期化
72+
/// </summary>
73+
/// <param name="bveHacker"></param>
74+
/// <param name="native"></param>
75+
/// <exception cref="ArgumentNullException">引数がnullの時に投げる例外</exception>
76+
//public static void Load(IBveHacker bveHacker, INative native)
77+
//{
78+
// native = native ?? throw new ArgumentNullException(nameof(native));
79+
// bveHacker = bveHacker ?? throw new ArgumentNullException(nameof(bveHacker));
80+
//}
81+
//public static void Load(IBveHacker bveHacker)
82+
//{
83+
// bveHacker = bveHacker ?? throw new ArgumentNullException(nameof(bveHacker));
84+
//}
85+
86+
}

CommEx/Serial/Common/Loopback.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using BveEx.Diagnostics;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO.Ports;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace CommEx.Serial.Common
10+
{
11+
/// <summary>
12+
/// シリアルループバック
13+
/// </summary>
14+
internal class Loopback : ISerialControl
15+
{
16+
/// <inheritdoc/>
17+
public void PortOpen(SerialPort serialPort)
18+
{
19+
serialPort.DataReceived += DataReceived;
20+
}
21+
22+
/// <summary>
23+
/// シリアル受信時のイベントハンドラ
24+
/// 送られてきた情報をそっくりそのまま返す
25+
/// </summary>
26+
/// <param name="sender">受信したポートの <see cref="SerialPort"/></param>
27+
/// <param name="e">イベントデータ</param>
28+
private void DataReceived(object sender, SerialDataReceivedEventArgs e)
29+
{
30+
try
31+
{
32+
SerialPort serialPort = (SerialPort)sender;
33+
string str = serialPort.ReadLine();
34+
serialPort.WriteLine(str);
35+
}
36+
catch (Exception ex)
37+
{
38+
#if DEBUG
39+
ErrorDialog.Show(new ErrorDialogInfo("通信エラー", ex.Source, ex.Message));
40+
#endif
41+
}
42+
}
43+
44+
/// <inheritdoc/>
45+
public void PortClose(SerialPort serialPort)
46+
{
47+
serialPort.DataReceived -= DataReceived;
48+
}
49+
}
50+
}

CommEx/Serial/Common/NewLines.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Globalization;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
using System.Drawing;
10+
11+
namespace CommEx.Serial.Common
12+
{
13+
/// <summary>
14+
/// 改行文字
15+
/// </summary>
16+
[TypeConverter(typeof(EnumToStringConverter))]
17+
public enum NewLines
18+
{
19+
[Description("\n")]
20+
LF,
21+
[Description("\r\n")]
22+
CRLF,
23+
[Description("\r")]
24+
CR
25+
}
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Settings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
3+
<PortSettings>
4+
<Port PortName="COM0">
5+
<BaudRate>115200</BaudRate>
6+
<DataBits>8</DataBits>
7+
<DtrEnable>false</DtrEnable>
8+
<Handshake>None</Handshake>
9+
<NewLine>CRLF</NewLine>
10+
<Parity>None</Parity>
11+
<StopBits>One</StopBits>
12+
<IsAutoConnent>true</IsAutoConnent>
13+
<Controller>Loopback</Controller>
14+
</Port>
15+
</PortSettings>
16+
</Settings>

0 commit comments

Comments
 (0)