/// <summary> /// sをアクティブ状態とし,それ以外を非アクティブ状態として各種処理を実行します. /// </summary> /// <param name="s"></param> private void activateWindow(SerialPortWindow s) { for (int i = 0; i < this.P.MAX_PORT_NUMBER; i++) { this.SerialPorts[i].isFocused = false; if (this.SerialPorts[i].FormTx != null && this.SerialPorts[i].FormTx.Controls.Count != 0) { ((TextBox)(this.SerialPorts[i].FormTx.Controls[0])).BackColor = Color.FromArgb(255, 249, 235); } } s.isFocused = true; if (s.FormTx != null && s.FormTx.Controls.Count != 0) { s.FormTx.Focus(); ((TextBox)(s.FormTx.Controls[0])).BackColor = Color.White ; } updateStatusStrip(s); }
/// <summary> /// シリアルタブのステータスバーに現在の送受信バイト数を表示します。 /// </summary> /// <param name="w">表示するシリアルポート</param> void updateStatusStrip(SerialPortWindow w) { if (w.Port != null) { this.label.Text = w.Port.PortName + "/" + w.Port.BaudRate + "bps 送信 " + w.SendBytes + "bytes / 受信 " + w.ReceiveBytes + "bytes"; } }
/// <summary> /// ポートを開きます。新規にSerialPortWindowインスタンスを生成してSerialPortをセットし、ログファイルは指定ディレクトリに生成されます /// </summary> /// <param name="Port">セットするSerialPortインスタンス。</param> /// <param name="connectError">接続失敗かどうか。</param> /// <param name="TxLog">TXウインドウのログを取るか。</param> /// <param name="RxLog">RXウインドウのログを取るか。</param> /// <param name="baseFileName">Tx,Rxファイルを保存するディレクトリ+拡張子を除く基本ファイル名。</param> private void openPort(SerialPort Port, bool connectError, bool TxLog, bool RxLog, bool BinaryLog, bool TxShow, bool RxShow, bool BinaryShow, string baseFileName) { try { SerialPortWindow window = new SerialPortWindow(); window.LastUpdate = DateTime.Now; window.COM = Port.PortName; window.TxLogEnable = TxLog; window.RxLogEnable = RxLog; window.BinaryLogEnable = BinaryLog; window.FormTxVisible = TxShow; window.FormRxVisible = RxShow; window.FormBinaryVisible = BinaryShow; window.TxBinaryMode = false; if (TxLog) { string TxLogFileName = baseFileName + "_Tx.txt"; try { window.TxFile = new StreamWriter(TxLogFileName, false, Encoding.GetEncoding("Shift_JIS")); window.TxFileName = TxLogFileName; this.LogFiles.Add(TxLogFileName); } #region{StreamWriter用例外処理ブロック} catch (UnauthorizedAccessException) { MessageBox.Show("ログファイルの生成に失敗しました。設定されたパスにアクセスする権限があるか確認して下さい。\n\n"+ TxLogFileName, "ファイル新規作成時エラー"); window.TxFile = null; window.TxLogEnable = false; } catch (ArgumentException) { MessageBox.Show("ログファイルの生成に失敗しました。ファイル名が空の文字列でないか確認して下さい。\n\n" + TxLogFileName, "ファイル新規作成時エラー"); window.TxFile = null; window.TxLogEnable = false; } catch (PathTooLongException) { MessageBox.Show("ログファイルの生成に失敗しました。指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。\n\n" + TxLogFileName, "ファイル新規作成時エラー"); window.TxFile = null; window.TxLogEnable = false; } catch (IOException) { MessageBox.Show("ログファイルの生成に失敗しました。ファイル名、ディレクトリ名、またはボリューム ラベルの不正な構文または無効な構文が含まれています。\n\n" + TxLogFileName, "ファイル新規作成時エラー"); window.TxFile = null; window.TxLogEnable = false; } #endregion } if (RxLog) { string RxLogFileName = baseFileName + "_Rx.txt"; try { window.RxFile = new StreamWriter(RxLogFileName, false, Encoding.GetEncoding("Shift_JIS")); window.RxFileName = RxLogFileName; this.LogFiles.Add(RxLogFileName); } #region{StreamWriter用例外処理ブロック} catch (UnauthorizedAccessException) { MessageBox.Show("ログファイルの生成に失敗しました。設定されたパスにアクセスする権限があるか確認して下さい。\n\n" + RxLogFileName, "ファイル新規作成時エラー"); window.RxFile = null; window.RxLogEnable = false; } catch (ArgumentException) { MessageBox.Show("ログファイルの生成に失敗しました。ファイル名が空の文字列でないか確認して下さい。\n\n" + RxLogFileName, "ファイル新規作成時エラー"); window.RxFile = null; window.RxLogEnable = false; } catch (PathTooLongException) { MessageBox.Show("ログファイルの生成に失敗しました。指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。\n\n" + RxLogFileName, "ファイル新規作成時エラー"); window.RxFile = null; window.RxLogEnable = false; } catch (IOException) { MessageBox.Show("ログファイルの生成に失敗しました。ファイル名、ディレクトリ名、またはボリューム ラベルの不正な構文または無効な構文が含まれています。\n\n" + RxLogFileName, "ファイル新規作成時エラー"); window.RxFile = null; window.RxLogEnable = false; } #endregion } if (BinaryLog) { string BinaryLogFileName = baseFileName + "_Bin.txt"; try { window.BinaryFile = new StreamWriter(BinaryLogFileName, false, Encoding.GetEncoding("Shift_JIS")); window.BinaryFileName = BinaryLogFileName; this.LogFiles.Add(BinaryLogFileName); } #region{StreamWriter用例外処理ブロック} catch (UnauthorizedAccessException) { MessageBox.Show("ログファイルの生成に失敗しました。設定されたパスにアクセスする権限があるか確認して下さい。\n\n" + BinaryLogFileName, "ファイル新規作成時エラー"); window.BinaryFile = null; window.BinaryLogEnable = false; } catch (ArgumentException) { MessageBox.Show("ログファイルの生成に失敗しました。ファイル名が空の文字列でないか確認して下さい。\n\n" + BinaryLogFileName, "ファイル新規作成時エラー"); window.BinaryFile = null; window.BinaryLogEnable = false; } catch (PathTooLongException) { MessageBox.Show("ログファイルの生成に失敗しました。指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。\n\n" + BinaryLogFileName, "ファイル新規作成時エラー"); window.BinaryFile = null; window.BinaryLogEnable = false; } catch (IOException) { MessageBox.Show("ログファイルの生成に失敗しました。ファイル名、ディレクトリ名、またはボリューム ラベルの不正な構文または無効な構文が含まれています。\n\n" + BinaryLogFileName, "ファイル新規作成時エラー"); window.BinaryFile = null; window.BinaryLogEnable = false; } #endregion } window.BinaryLogIndex = 0; window.RxLogIndex = 0; window.TxLogIndex = 0; window.ReceiveBytes = 0; window.SendBytes = 0; window.isConnected = !connectError; window.TxLastTimeStamp = DateTime.Now; window.RxLastTimeStamp = DateTime.Now; for (int i = 0; i < P.MAX_PORT_NUMBER; i++) { this.SerialPorts[i].isFocused = false; } window.isFocused = true; window.Port = Port; window.DrawedIndex = 0; if (portNumber(Port.PortName) < P.MAX_PORT_NUMBER) { this.SerialPorts[portNumber(Port.PortName)] = window; } openWindow(Port.PortName, Port, ref window, connectError); //this.SerialPortsList.Add(window); resizeWindows(); //ウィンドウ作成が終わってから受信開始 Port.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived); Port.PinChanged += new SerialPinChangedEventHandler(this.serialPort1_PinChanged); } catch (Exception) { } }
private void openWindow(string port, SerialPort Port, ref SerialPortWindow serialPorts, bool connectError) { try { #region{ウインドウ接続} //窓サイズをシリアルポート数で分割 this.windowWidth = (int)(this.tabPage1.Width / (1 + getActiveSerialPortWindows())); #region{Tx} if (serialPorts.FormTxVisible && (serialPorts.FormTx == null || serialPorts.FormTx.Visible == false)) { Form FormTx = new Form(); TextBox Tx = new TextBox(); FormTx.Icon = this.Icon; FormTx.TopLevel = false ; FormTx.Width = this.windowWidth; FormTx.Height = this.windowHeight; FormTx.Text = getFormTitle(port, FormType.Tx, serialPorts.TxFileName, Port.BaudRate, connectError, Port.IsOpen, Tx); FormTx.MaximizeBox = false; FormTx.MinimizeBox = false; FormTx.AutoScroll = true; FormTx.Left = 0; FormTx.Top = 0; FormTx.Tag = portNumber(port); FormTx.FormClosing += new FormClosingEventHandler(serialWindow_FormClosing); this.toolTip1.SetToolTip(FormTx, FormTx.Text); Tx.Font = this.BaseFont; Tx.Tag = portNumber(port); Tx.KeyDown += new KeyEventHandler(Tx_KeyDown); Tx.KeyPress += new KeyPressEventHandler(Tx_KeyPress); Tx.Multiline = true; FormTx.Controls.Add(Tx); Tx.Dock = DockStyle.Fill; Tx.MaxLength = 0; Tx.ScrollBars = ScrollBars.Vertical; Tx.MouseDown += new MouseEventHandler(Tx_MouseDown); Tx.Click += new EventHandler(Port_OnClick); Tx.DragDrop += new DragEventHandler(Port_DragDrop); Tx.DragEnter += new DragEventHandler(Port_DragEnter); Tx.AllowDrop = true; this.tabPage1.Controls.Add(FormTx); FormTx.Show(); FormTx.BringToFront(); serialPorts.FormTx = FormTx; serialPorts.TxLogIndex = 0; } #endregion #region{Rx} if (serialPorts.FormRxVisible && (serialPorts.FormRx == null || serialPorts.FormRx.Visible == false)) { Form FormRx = new Form(); TextBox Rx = new TextBox(); FormRx.Icon = this.Icon; FormRx.TopLevel = false; FormRx.Width = this.windowWidth; FormRx.Height = this.windowHeight; FormRx.Tag = portNumber(port); FormRx.FormClosing += new FormClosingEventHandler(serialWindow_FormClosing); FormRx.Text = getFormTitle(port, FormType.Rx, serialPorts.RxFileName, Port.BaudRate, connectError, Port.IsOpen, Rx); FormRx.MaximizeBox = false; FormRx.MinimizeBox = false; FormRx.AutoScroll = true; FormRx.Left = 0; FormRx.Top = this.windowHeight; this.toolTip1.SetToolTip(FormRx, FormRx.Text); Rx.Font = this.BaseFont; Rx.Tag = portNumber(port); Rx.Multiline = true; Rx.ReadOnly = true; FormRx.Controls.Add(Rx); Rx.Dock = DockStyle.Fill; Rx.MaxLength = 0; Rx.ScrollBars = ScrollBars.Vertical; Rx.MouseDown += new MouseEventHandler(Rx_MouseDown); Rx.Click += new EventHandler(Port_OnClick); Rx.DragDrop += new DragEventHandler(Port_DragDrop); Rx.DragEnter += new DragEventHandler(Port_DragEnter); Rx.AllowDrop = true; this.tabPage1.Controls.Add(FormRx); FormRx.Show(); FormRx.BringToFront(); serialPorts.FormRx = FormRx; serialPorts.RxLogIndex = 0; serialPorts.DrawedIndex = 0; } #endregion #region{Rx Binary} if (serialPorts.FormBinaryVisible && (serialPorts.FormBinary == null || serialPorts.FormBinary.Visible == false)) { Form FormBinary = new Form(); TextBox Binary = new TextBox(); FormBinary.Icon = this.Icon; FormBinary.TopLevel = false; FormBinary.Width = this.windowWidth; FormBinary.Height = this.windowHeight; FormBinary.Tag = portNumber(port); FormBinary.FormClosing += new FormClosingEventHandler(serialWindow_FormClosing); FormBinary.Text = getFormTitle(port, FormType.Binary, serialPorts.BinaryFileName, Port.BaudRate, connectError, Port.IsOpen, Binary); FormBinary.MaximizeBox = false; FormBinary.MinimizeBox = false; FormBinary.AutoScroll = true; FormBinary.Left = 0; FormBinary.Top = this.windowHeight; this.toolTip1.SetToolTip(FormBinary, FormBinary.Text); Binary.Font = this.BaseFont; Binary.Tag = portNumber(port); Binary.Multiline = true; Binary.ReadOnly = true; FormBinary.Controls.Add(Binary); Binary.Dock = DockStyle.Fill; Binary.MaxLength = 0; Binary.ScrollBars = ScrollBars.Vertical; Binary.MouseDown += new MouseEventHandler(Rx_MouseDown); Binary.Click += new EventHandler(Port_OnClick); Binary.DragDrop += new DragEventHandler(Port_DragDrop); Binary.DragEnter += new DragEventHandler(Port_DragEnter); Binary.AllowDrop = true; this.tabPage1.Controls.Add(FormBinary); FormBinary.Show(); FormBinary.BringToFront(); serialPorts.FormBinary = FormBinary; serialPorts.BinaryLogIndex = 0; } #endregion #endregion this.SerialPorts[portNumber(serialPorts.COM)].FormTx.Controls[0].Focus(); activateWindow(this.SerialPorts[portNumber(serialPorts.COM)]); } catch (Exception ex) { Console.WriteLine("error at openWindow:" + ex.Message); } }
private void changeBaudRate(SerialPortWindow Com, int BaudRate) { try { Com.Port.BaudRate = BaudRate; if (Com.FormTx != null) { Com.FormTx.Text = getFormTitle(Com.COM, FormType.Tx, Com.TxFileName, Com.Port.BaudRate, !Com.isConnected, Com.Port.IsOpen, Com.FormTx.Controls[0]); } if (Com.FormRx != null) { Com.FormRx.Text = getFormTitle(Com.COM, FormType.Rx, Com.RxFileName, Com.Port.BaudRate, !Com.isConnected, Com.Port.IsOpen, Com.FormRx.Controls[0]); } if (Com.FormBinary != null) { Com.FormBinary.Text = getFormTitle(Com.COM, FormType.Binary, Com.BinaryFileName, Com.Port.BaudRate, !Com.isConnected, Com.Port.IsOpen, Com.FormBinary.Controls[0]); } } catch (Exception ex) { Console.WriteLine("changeBaudRateError:" + ex.Message); } }