Skip to content

Commit

Permalink
Merge pull request #3 from stop-pattern/feature/serial
Browse files Browse the repository at this point in the history
設定画面表示とある程度のBIDS互換
  • Loading branch information
stop-pattern authored Jan 26, 2025
2 parents de30c4f + 2e9873d commit 7eb874a
Show file tree
Hide file tree
Showing 20 changed files with 2,424 additions and 30 deletions.
8 changes: 8 additions & 0 deletions CommEx/CommEx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<Deterministic>false</Deterministic>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
Expand All @@ -18,4 +19,11 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xaml" />
<Reference Include="WindowsBase" />
</ItemGroup>
</Project>
439 changes: 439 additions & 0 deletions CommEx/Serial/Bids/Serial.cs

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions CommEx/Serial/Common/BoolToColorConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;

namespace CommEx.Serial.Common
{
/// <summary>
/// bool 型を Color に変換するコンバータ
/// </summary>
public class BoolToColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool isOpen)
{
return isOpen ? Brushes.Green : Brushes.Red;
}

return Brushes.Gray; // デフォルト色
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Brush brush)
{
if (brush == Brushes.Green)
{
return true;
}
else if (brush == Brushes.Red)
{
return false;
}
else if (brush == Brushes.Gray)
{
return false;
}
}

throw new InvalidOperationException("Unsupported conversion");
}
}
}
124 changes: 124 additions & 0 deletions CommEx/Serial/Common/EnumConverters.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace CommEx.Serial.Common
{
#region StringConverter

/// <inheritdoc/>
/// <summary>
/// Enum の Value と String を変換するコンバータ
/// String はそのまま Enum の Value として扱われる
/// </summary>
public class EnumToStringConverter : EnumConverter
{
public EnumToStringConverter(Type type) : base(type)
{
if (!type.IsEnum)
{
throw new ArgumentException("Type must be an Enum.");
}
}
/// <summary>
/// Enum の Value から String を取得
/// Enum.Value -> string
/// </summary>
/// <param name="value">Enum.value</param>
/// <param name="destinationType">string</param>
/// <returns>string</returns>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value != null)
{
return value.ToString();
}
return base.ConvertTo(context, culture, value, destinationType);
}

/// <summary>
/// String から Enum の Value を取得
/// string -> Enum.Value
/// </summary>
/// <param name="value">string</param>
/// <returns>Enum.value</returns>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string stringValue)
{
return Enum.Parse(EnumType, stringValue);
}
return base.ConvertFrom(context, culture, value);
}
}

#endregion

#region DescriptionConverter

/// <inheritdoc/>
/// <summary>
/// Enum の Value とその Description を変換するコンバータ
/// </summary>
public class EnumToDescriptionConverter : EnumConverter
{
public EnumToDescriptionConverter(Type type) : base(type)
{
if (!type.IsEnum)
{
throw new ArgumentException("Type must be an Enum.");
}
}

/// <summary>
/// Enum の Value から DescriptionAttribute を取得
/// Enum.Value -> string
/// </summary>
/// <param name="value">Enum.value</param>
/// <param name="destinationType">string</param>
/// <returns>string</returns>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string) && value != null)
{
var fieldInfo = value.GetType().GetField(value.ToString());
var descriptionAttribute = fieldInfo?.GetCustomAttribute<DescriptionAttribute>();
if (descriptionAttribute != null)
{
return descriptionAttribute.Description;
}
}
return base.ConvertTo(context, culture, value, destinationType);
}

/// <summary>
/// Enum の DescriptionAttribute から Value を取得
/// string -> Enum.Value
/// </summary>
/// <param name="value">string</param>
/// <returns>Enum.value</returns>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string stringValue)
{
foreach (var field in EnumType.GetFields(BindingFlags.Public | BindingFlags.Static))
{
var descriptionAttribute = field.GetCustomAttribute<DescriptionAttribute>();
if (descriptionAttribute != null && descriptionAttribute.Description == stringValue)
{
return Enum.Parse(EnumType, field.Name);
}
}
}
return base.ConvertFrom(context, culture, value);
}
}

#endregion
}
86 changes: 86 additions & 0 deletions CommEx/Serial/Common/ISerialControl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using BveEx.PluginHost;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommEx.Serial.Common
{
public interface ISerialControl
{
/// <summary>
/// ポートを開ける前に呼ばれる
/// </summary>
/// <param name="serialPort"><see cref="SerialPort"/></param>
void PortOpen(SerialPort serialPort);

/// <summary>
/// ポートを閉じた後に呼ばれる
/// </summary>
/// <param name="serialPort"><see cref="SerialPort"/></param>
void PortClose(SerialPort serialPort);

/// <summary>
/// シリアルポートの受信時に呼ばれる
/// </summary>
/// <param name="sender"><see cref="SerialPort"/></param>
/// <param name="e"></param>
//void DataReceived(object sender, SerialDataReceivedEventArgs e);
}

interface IBveEx
{
/// <summary>
/// 全ての BveEx 拡張機能が読み込まれ、BveEx.PluginHost.Plugins.Extensions プロパティが取得可能になると発生
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void AllExtensionsLoaded(object sender, EventArgs e);

/// <summary>
/// シナリオ読み込み
/// </summary>
/// <param name="e"></param>
void OnScenarioCreated(ScenarioCreatedEventArgs e);

/// <summary>
/// シナリオ読み込み中に毎フレーム呼び出される
/// </summary>
/// <param name="elapsed"></param>
void Tick(TimeSpan elapsed);

/// <summary>
/// シナリオ終了
/// </summary>
/// <param name="e"></param>
void ScenarioClosed(EventArgs e);
}

/// <summary>
/// 入力されたキーを送信する
/// </summary>
/// <param name="nInputs"></param>
/// <param name="pInputs"></param>
/// <param name="cbsize"></param>
//[DllImport("user32.dll", SetLastError = true)]
//public extern static void SendInput(int nInputs, Input[] pInputs, int cbsize);

/// <summary>
/// BveHacker と Native の初期化
/// </summary>
/// <param name="bveHacker"></param>
/// <param name="native"></param>
/// <exception cref="ArgumentNullException">引数がnullの時に投げる例外</exception>
//public static void Load(IBveHacker bveHacker, INative native)
//{
// native = native ?? throw new ArgumentNullException(nameof(native));
// bveHacker = bveHacker ?? throw new ArgumentNullException(nameof(bveHacker));
//}
//public static void Load(IBveHacker bveHacker)
//{
// bveHacker = bveHacker ?? throw new ArgumentNullException(nameof(bveHacker));
//}

}
50 changes: 50 additions & 0 deletions CommEx/Serial/Common/Loopback.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using BveEx.Diagnostics;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommEx.Serial.Common
{
/// <summary>
/// シリアルループバック
/// </summary>
internal class Loopback : ISerialControl
{
/// <inheritdoc/>
public void PortOpen(SerialPort serialPort)
{
serialPort.DataReceived += DataReceived;
}

/// <summary>
/// シリアル受信時のイベントハンドラ
/// 送られてきた情報をそっくりそのまま返す
/// </summary>
/// <param name="sender">受信したポートの <see cref="SerialPort"/></param>
/// <param name="e">イベントデータ</param>
private void DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
SerialPort serialPort = (SerialPort)sender;
string str = serialPort.ReadLine();
serialPort.WriteLine(str);
}
catch (Exception ex)

Check warning on line 36 in CommEx/Serial/Common/Loopback.cs

View workflow job for this annotation

GitHub Actions / MSBuild

The variable 'ex' is declared but never used

Check warning on line 36 in CommEx/Serial/Common/Loopback.cs

View workflow job for this annotation

GitHub Actions / MSBuild

The variable 'ex' is declared but never used
{
#if DEBUG
ErrorDialog.Show(new ErrorDialogInfo("通信エラー", ex.Source, ex.Message));
#endif
}
}

/// <inheritdoc/>
public void PortClose(SerialPort serialPort)
{
serialPort.DataReceived -= DataReceived;
}
}
}
26 changes: 26 additions & 0 deletions CommEx/Serial/Common/NewLines.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Drawing;

namespace CommEx.Serial.Common
{
/// <summary>
/// 改行文字
/// </summary>
[TypeConverter(typeof(EnumToStringConverter))]
public enum NewLines
{
[Description("\n")]
LF,
[Description("\r\n")]
CRLF,
[Description("\r")]
CR
}
}
16 changes: 16 additions & 0 deletions CommEx/Serial/Common/Sample.Setting.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<Settings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<PortSettings>
<Port PortName="COM0">
<BaudRate>115200</BaudRate>
<DataBits>8</DataBits>
<DtrEnable>false</DtrEnable>
<Handshake>None</Handshake>
<NewLine>CRLF</NewLine>
<Parity>None</Parity>
<StopBits>One</StopBits>
<IsAutoConnent>true</IsAutoConnent>
<Controller>Loopback</Controller>
</Port>
</PortSettings>
</Settings>
Loading

0 comments on commit 7eb874a

Please sign in to comment.