diff --git a/ISCamRecorder/ISCamRecorder.csproj b/ISCamRecorder/ISCamRecorder.csproj index 7034dd2..2018da5 100644 --- a/ISCamRecorder/ISCamRecorder.csproj +++ b/ISCamRecorder/ISCamRecorder.csproj @@ -100,6 +100,7 @@ + MainForm.cs diff --git a/ISCamRecorder/MainForm.Designer.cs b/ISCamRecorder/MainForm.Designer.cs index 1a47b16..e76d20a 100644 --- a/ISCamRecorder/MainForm.Designer.cs +++ b/ISCamRecorder/MainForm.Designer.cs @@ -23,7 +23,6 @@ /// コード エディターで変更しないでください。 /// private void InitializeComponent() { - this.components = new System.ComponentModel.Container(); System.Windows.Forms.DataVisualization.Charting.ChartArea chartArea1 = new System.Windows.Forms.DataVisualization.Charting.ChartArea(); System.Windows.Forms.DataVisualization.Charting.Series series1 = new System.Windows.Forms.DataVisualization.Charting.Series(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); @@ -34,6 +33,8 @@ this.splitContainer3 = new System.Windows.Forms.SplitContainer(); this.TxtTop = new System.Windows.Forms.TextBox(); this.icTop = new TIS.Imaging.ICImagingControl(); + this.TxtSensor = new System.Windows.Forms.TextBox(); + this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart(); this.splitContainer4 = new System.Windows.Forms.SplitContainer(); this.TxtLeft = new System.Windows.Forms.TextBox(); this.icLeft = new TIS.Imaging.ICImagingControl(); @@ -58,8 +59,6 @@ this.label6 = new System.Windows.Forms.Label(); this.label7 = new System.Windows.Forms.Label(); this.CboImageType = new System.Windows.Forms.ComboBox(); - this.serialPort1 = new System.IO.Ports.SerialPort(this.components); - this.chart1 = new System.Windows.Forms.DataVisualization.Charting.Chart(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).BeginInit(); this.splitContainer1.Panel1.SuspendLayout(); this.splitContainer1.Panel2.SuspendLayout(); @@ -72,6 +71,7 @@ this.splitContainer3.Panel2.SuspendLayout(); this.splitContainer3.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.icTop)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer4)).BeginInit(); this.splitContainer4.Panel1.SuspendLayout(); this.splitContainer4.Panel2.SuspendLayout(); @@ -83,7 +83,6 @@ this.splitContainer5.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.icFront)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.icRight)).BeginInit(); - ((System.ComponentModel.ISupportInitialize)(this.chart1)).BeginInit(); this.SuspendLayout(); // // BtnSetProperty @@ -153,6 +152,7 @@ // // splitContainer3.Panel2 // + this.splitContainer3.Panel2.Controls.Add(this.TxtSensor); this.splitContainer3.Panel2.Controls.Add(this.chart1); this.splitContainer3.Size = new System.Drawing.Size(592, 248); this.splitContainer3.SplitterDistance = 296; @@ -184,6 +184,38 @@ this.icTop.Size = new System.Drawing.Size(290, 218); this.icTop.TabIndex = 1; // + // TxtSensor + // + this.TxtSensor.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.TxtSensor.BackColor = System.Drawing.Color.Aquamarine; + this.TxtSensor.Location = new System.Drawing.Point(3, 3); + this.TxtSensor.Name = "TxtSensor"; + this.TxtSensor.ReadOnly = true; + this.TxtSensor.Size = new System.Drawing.Size(288, 19); + this.TxtSensor.TabIndex = 3; + this.TxtSensor.Text = "逆血センサー"; + // + // chart1 + // + this.chart1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) + | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + chartArea1.AxisX.LabelStyle.Format = "hh:mm:ss"; + chartArea1.Name = "ChartArea1"; + this.chart1.ChartAreas.Add(chartArea1); + this.chart1.Location = new System.Drawing.Point(3, 28); + this.chart1.Name = "chart1"; + series1.ChartArea = "ChartArea1"; + series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine; + series1.Name = "Series1"; + series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Time; + series1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32; + this.chart1.Series.Add(series1); + this.chart1.Size = new System.Drawing.Size(286, 220); + this.chart1.TabIndex = 0; + this.chart1.Text = "chart1"; + // // splitContainer4 // this.splitContainer4.Dock = System.Windows.Forms.DockStyle.Fill; @@ -307,7 +339,7 @@ this.TxtRecodingDulation.TabIndex = 8; this.TxtRecodingDulation.Text = "3"; this.TxtRecodingDulation.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; - this.TxtRecodingDulation.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TxtRecodingDulation_KeyPress); + this.TxtRecodingDulation.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.NumberKeyOnly); // // label1 // @@ -380,6 +412,7 @@ this.TxtTriggerFPS.TabIndex = 16; this.TxtTriggerFPS.Text = "40.0"; this.TxtTriggerFPS.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; + this.TxtTriggerFPS.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.NumberKeyOnly); // // label4 // @@ -416,6 +449,7 @@ this.TxtMovieRate.TabIndex = 20; this.TxtMovieRate.Text = "3000"; this.TxtMovieRate.TextAlign = System.Windows.Forms.HorizontalAlignment.Right; + this.TxtMovieRate.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.NumberKeyOnly); // // label6 // @@ -448,26 +482,6 @@ this.CboImageType.TabIndex = 21; this.CboImageType.Text = "JPG"; // - // chart1 - // - this.chart1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) - | System.Windows.Forms.AnchorStyles.Left) - | System.Windows.Forms.AnchorStyles.Right))); - chartArea1.AxisX.LabelStyle.Format = "hh:mm:ss"; - chartArea1.Name = "ChartArea1"; - this.chart1.ChartAreas.Add(chartArea1); - this.chart1.Location = new System.Drawing.Point(3, 3); - this.chart1.Name = "chart1"; - series1.ChartArea = "ChartArea1"; - series1.ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.FastLine; - series1.Name = "Series1"; - series1.XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Time; - series1.YValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Int32; - this.chart1.Series.Add(series1); - this.chart1.Size = new System.Drawing.Size(286, 245); - this.chart1.TabIndex = 0; - this.chart1.Text = "chart1"; - // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); @@ -498,6 +512,7 @@ this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing); this.Load += new System.EventHandler(this.MainForm_Load); this.SizeChanged += new System.EventHandler(this.MainForm_SizeChanged); + this.Paint += new System.Windows.Forms.PaintEventHandler(this.MainForm_Paint); this.splitContainer1.Panel1.ResumeLayout(false); this.splitContainer1.Panel2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit(); @@ -508,9 +523,11 @@ this.splitContainer3.Panel1.ResumeLayout(false); this.splitContainer3.Panel1.PerformLayout(); this.splitContainer3.Panel2.ResumeLayout(false); + this.splitContainer3.Panel2.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.splitContainer3)).EndInit(); this.splitContainer3.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.icTop)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit(); this.splitContainer4.Panel1.ResumeLayout(false); this.splitContainer4.Panel1.PerformLayout(); this.splitContainer4.Panel2.ResumeLayout(false); @@ -525,7 +542,6 @@ this.splitContainer5.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.icFront)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.icRight)).EndInit(); - ((System.ComponentModel.ISupportInitialize)(this.chart1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); @@ -563,8 +579,8 @@ private System.Windows.Forms.Label label6; private System.Windows.Forms.Label label7; private System.Windows.Forms.ComboBox CboImageType; - private System.IO.Ports.SerialPort serialPort1; private System.Windows.Forms.DataVisualization.Charting.Chart chart1; + private System.Windows.Forms.TextBox TxtSensor; } } diff --git a/ISCamRecorder/MainForm.cs b/ISCamRecorder/MainForm.cs index fa96034..d54c1be 100644 --- a/ISCamRecorder/MainForm.cs +++ b/ISCamRecorder/MainForm.cs @@ -11,13 +11,13 @@ using System.Reflection; using System.IO; using NAudio.MediaFoundation; -using System.Management; -using System.IO.Ports; using System.Windows.Forms.DataVisualization.Charting; +using System.Threading; namespace ISCamRecorder { public partial class MainForm : Form { - readonly string SERIAL_PORT_NAME = "USB シリアル"; + + readonly int PLOT_LENGTH = 200; private System.Threading.Timer _UITimer; // UI更新タイマー List _Cameras = new List (); // カメラオブジェクト @@ -26,8 +26,8 @@ Task _SerialThread; // シリアル通信スレッド float _TriggerFrameRate = 30.0F; private ulong _availablePhysicalMemory; //合計物理メモリ - Queue _SensorValues = new Queue(); - Queue _SensorTime = new Queue(); + SensorData _Sensor = new SensorData(); + DateTime _LastUpdate; /// /// コンストラクタ @@ -52,20 +52,11 @@ _Cameras.Add(new ISCamera(icRight, "9220025")); _Cameras.ForEach(c => c.Connect()); - // グラフ初期化 - chart1.Series[0].Points.Clear(); - //chart1.ChartAreas.Clear(); - //chart1.Series.Clear(); - //var chartArea = new ChartArea("chartArea"); - //chart1.ChartAreas.Add(chartArea); - //var series = new Series(); - //series.ChartType = SeriesChartType.Line; - //chart1.Series.Add(series); - // タイマー起動 - _UITimer = new System.Threading.Timer(UITimerCB, this, 0, 2000); + _UITimer = new System.Threading.Timer(UITimerCB, this, 0, 100); _TriggerThread = Task.Run(TriggerThread); _SerialThread = Task.Run(SerialThread); + _LastUpdate = DateTime.Now; } /// @@ -89,90 +80,46 @@ /// シリアル通信スレッド /// private void SerialThread() { - // シリアルポート接続 - var portname = GetSerialPort(); - if (portname.Length < 1) { - MessageBox.Show("シリアルデバイスに接続できません", "Error", - MessageBoxButtons.OK, MessageBoxIcon.Warning); - return; - } - serialPort1.BaudRate = 9600; - serialPort1.Parity = Parity.None; - serialPort1.DataBits = 8; - serialPort1.StopBits = StopBits.One; - serialPort1.Handshake = Handshake.None; - serialPort1.RtsEnable = true; - serialPort1.PortName = portname; - serialPort1.Open(); - Debug.WriteLine($"Serial Connect {portname}"); - while (!_ExitSignal) { - var str = serialPort1.ReadLine(); - if (_ExitSignal) break; - var val = int.Parse(str); - var dt = DateTime.Now; - _SensorValues.Enqueue(val); - _SensorTime.Enqueue(dt); - if (_SensorValues.Count > 100) { - _SensorValues.Dequeue(); - _SensorTime.Dequeue(); - } - if (!_ExitSignal) Plot(); - } + if (!_Sensor.Connect(PLOT_LENGTH)) return; + _Sensor.Loop(); } /// /// グラフをプロット /// private void Plot() { - if (!this.IsDisposed && this.InvokeRequired) { - this.Invoke((MethodInvoker)delegate { Plot(); }); - return; - } chart1.Series[0].Points.Clear(); - for (var i = 0; i < _SensorValues.Count; i++) { - chart1.Series[0].Points.AddXY( - _SensorTime.ElementAt(i), _SensorValues.ElementAt(i)); - } + var times = _Sensor.Times.ToList(); + var values = _Sensor.Values.ToList(); + chart1.Series[0].Points.DataBindXY(times, values); } - /// - /// シリアルポート取得 - /// - /// - private string GetSerialPort() { - var portname = ""; - var mcW32SerPort = new ManagementClass("Win32_SerialPort"); - foreach (var port in mcW32SerPort.GetInstances()) { - if (port.GetPropertyValue("Caption").ToString().Contains(SERIAL_PORT_NAME)) { - portname = port.GetPropertyValue("DeviceID").ToString(); - } - } - return portname; - } /// /// タイマーイベント /// /// static void UITimerCB(object obj) { - ((MainForm)obj).UpdateForm(); + ((MainForm)obj).Invalidate(); } /// /// トリガー発生 /// public void UpdateForm() { - if (this.InvokeRequired) { - this.Invoke((MethodInvoker)delegate { UpdateForm(); }); - return; + var now = DateTime.Now; + var elapsed = now - _LastUpdate; + if (elapsed.TotalSeconds > 2) { + _LastUpdate = now; + TxtTop.Text = $"上方カメラ {_Cameras[0].CameraInfo()}"; + TxtLeft.Text = $"左方カメラ {_Cameras[1].CameraInfo()}"; + TxtFront.Text = $"前方カメラ {_Cameras[2].CameraInfo()}"; + TxtRight.Text = $"右方カメラ {_Cameras[3].CameraInfo()}"; + TotalPhysicalMemory(); + MemoryToUse(); + _TriggerFrameRate = float.Parse(TxtTriggerFPS.Text); } - TxtTop.Text = $"上方カメラ {_Cameras[0].CameraInfo()}"; - TxtLeft.Text = $"左方カメラ {_Cameras[1].CameraInfo()}"; - TxtFront.Text = $"前方カメラ {_Cameras[2].CameraInfo()}"; - TxtRight.Text = $"右方カメラ {_Cameras[3].CameraInfo()}"; - TotalPhysicalMemory(); - MemoryToUse(); - _TriggerFrameRate = float.Parse(TxtTriggerFPS.Text); + Plot(); } /// @@ -204,11 +151,11 @@ /// /// private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { - serialPort1.Close(); - _ExitSignal = true; _UITimer.Dispose(); + _Sensor.Stop(); + _ExitSignal = true; _TriggerThread.Wait(); - _SerialThread.Wait(1000); + _SerialThread.Wait(); } /// @@ -257,7 +204,7 @@ /// /// /// - private void TxtRecodingDulation_KeyPress(object sender, KeyPressEventArgs e) { + private void NumberKeyOnly(object sender, KeyPressEventArgs e) { //バックスペースが押された時は有効(Deleteキーも有効) if (e.KeyChar == '\b' || e.KeyChar == '.') { return; @@ -320,5 +267,14 @@ BtnSetProperty.Enabled = true; } } + + /// + /// フォーム再描画 + /// + /// + /// + private void MainForm_Paint(object sender, PaintEventArgs e) { + UpdateForm(); + } } } diff --git a/ISCamRecorder/MainForm.resx b/ISCamRecorder/MainForm.resx index 1b87d9c..8d6a7ab 100644 --- a/ISCamRecorder/MainForm.resx +++ b/ISCamRecorder/MainForm.resx @@ -117,9 +117,6 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - 17, 17 - diff --git a/ISCamRecorder/SensorData.cs b/ISCamRecorder/SensorData.cs new file mode 100644 index 0000000..6b03439 --- /dev/null +++ b/ISCamRecorder/SensorData.cs @@ -0,0 +1,95 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO.Ports; +using System.Management; +using System.Diagnostics; + +namespace ISCamRecorder { + internal class SensorData { + readonly string SERIAL_PORT_NAME = "USB シリアル"; + + public Queue Values { get; private set; } = new Queue(); + public Queue Times { get; private set; } = new Queue(); + SerialPort _Serial = null; + bool _ExitSignal = false; // 終了シグナル + int _QueueLength = 100; + + /// + /// 接続 + /// + /// + public bool Connect(int queueLength) { + _QueueLength = queueLength; + + // シリアルポート接続 + var portname = GetSerialPort(); + if (portname.Length < 1) { + //MessageBox.Show("シリアルデバイスに接続できません", "Error", + // MessageBoxButtons.OK, MessageBoxIcon.Warning); + return false; + } + _Serial = new SerialPort { + PortName = portname, + BaudRate = 9600, + DataBits = 8, + Parity = Parity.None, + StopBits = StopBits.One, + Handshake = Handshake.None, + Encoding = Encoding.ASCII, + WriteTimeout = 1000, + ReadTimeout = 1000, + RtsEnable = true, + }; + try { + _Serial.Open(); + } catch (Exception ex) { + Debug.WriteLine(ex.Message); + return false; + } + Debug.WriteLine($"Serial Connect {portname}"); + return true; + } + + /// + /// 受信ループ + /// + public void Loop() { + while (!_ExitSignal) { + var str = _Serial.ReadLine(); + var val = int.Parse(str); + var dt = DateTime.Now; + Values.Enqueue(val); + Times.Enqueue(dt); + if (Values.Count > _QueueLength) { + Values.Dequeue(); + Times.Dequeue(); + } + } + + _Serial?.Close(); + _Serial = null; + } + + public void Stop() { + _ExitSignal = true; + } + + /// + /// シリアルポート取得 + /// + /// + private string GetSerialPort() { + var portname = ""; + var mcW32SerPort = new ManagementClass("Win32_SerialPort"); + foreach (var port in mcW32SerPort.GetInstances()) { + if (port.GetPropertyValue("Caption").ToString().Contains(SERIAL_PORT_NAME)) { + portname = port.GetPropertyValue("DeviceID").ToString(); + } + } + return portname; + } + } +}