generated from stop-pattern/BveExCsTemplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from stop-pattern/feature/serial
設定画面表示とある程度のBIDS互換
- Loading branch information
Showing
20 changed files
with
2,424 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
//} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / MSBuild
|
||
{ | ||
#if DEBUG | ||
ErrorDialog.Show(new ErrorDialogInfo("通信エラー", ex.Source, ex.Message)); | ||
#endif | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void PortClose(SerialPort serialPort) | ||
{ | ||
serialPort.DataReceived -= DataReceived; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.