目次

全ソースコード

 作成したシステムのソースコードです。
 デバッグするために、ところどころにメッセージを入れる
 コードが入っています。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace scst
{
  public partial class mainForm : Form
  {
    bool gPflag;
    string gStartDirectory;
    string gFName;
    //string sCmd = "R\n";
    string sCmd = "C\r";

    // field
    MyTextBox rMyTextBox;

    public mainForm()
    {
      InitializeComponent();
      // get file directory
      gStartDirectory = Path.GetDirectoryName(Application.ExecutablePath) ;
      int idx ;
      idx = gStartDirectory.IndexOf("\\scst");
      textDir.Text = gStartDirectory.Substring(0, idx + 5);
      gFName = "";
      // initialize serial port
      sciDTB.BaudRate = 4800 ;
      //sciDTB.BaudRate = 38400 ;
      sciDTB.RtsEnable = false;
      sciDTB.DtrEnable = false ;
      sciDTB.ReadBufferSize = 8192 ;
      sciDTB.PortName = "COM1";
      sciDTB.Encoding = System.Text.Encoding.GetEncoding("shift_jis");
      // button control
      gPflag = false;
      btnOpenPort.Enabled = true;
      btnClosePort.Enabled = false;
      // add TextBox 
      rMyTextBox = new MyTextBox();
      this.rMyTextBox.Location = new System.Drawing.Point(14, 140);
      this.rMyTextBox.Name = "rMyTextBox";
      this.rMyTextBox.Size = new System.Drawing.Size(104, 331);
      this.rMyTextBox.TabStop = false ;
      this.rMyTextBox.Multiline = true;
      this.rMyTextBox.ScrollBars = ScrollBars.Both ;
      this.rMyTextBox.WordWrap = true ;
      this.rMyTextBox.Width = 314;
      this.rMyTextBox.Height = 140;
      this.rMyTextBox.Text = "";
      this.Controls.Add(rMyTextBox);
    }

    /* Send Command */
    private void btnSend_Click(object sender, EventArgs e)
    {
      if (sciDTB.IsOpen)
      {
        sciDTB.Write(sCmd);
        textCmd.Text = sCmd;
      }
      else
      {
        MessageBox.Show("Not valid port");
      }
    }

    // Open Port
    private void btnOpenPort_Click(object sender, EventArgs e)
    {
      // judge
      if ( gPflag == true ) return;
      //
      try
      {
        // get COM port number
        string com_str;
        com_str = "COM" + nudPort.Value;
        // set Port Name
        sciDTB.PortName = com_str;
        //MessageBox.Show(sciDTB.PortName);
        // Open Port
        sciDTB.Open();
        //
        gPflag = true;
        btnOpenPort.Enabled = false;
        btnClosePort.Enabled = true;
      }
      catch (System.IO.IOException myIOe)
      {
        MessageBox.Show(myIOe.ToString(), "ERROR:COM port not exists!");
        gPflag = false;
        btnOpenPort.Enabled = true;
        btnClosePort.Enabled = false;
      }
    }

    // Close Port
    private void btnClosePort_Click(object sender, EventArgs e)
    {
      // judge
      if ( !gPflag ) return ;
      // change flag state
      gPflag = false ;
      btnOpenPort.Enabled = true ;
      btnClosePort.Enabled = false ;
      // Close Port
      sciDTB.Close();
    }

    // delegate for clear data
    delegate void ClrText();

    // Clear Text Area
    private void btnClear_Click(object sender, EventArgs e)
    {
      //MessageBox.Show("Clear Text Area"); 
      Invoke(new ClrText(rMyTextBox.ClrText));
    }

    // Exit
    private void btnExit_Click(object sender, EventArgs e)
    {
      // judge
      if ( sciDTB.IsOpen ) { sciDTB.Close(); }
      // End
      //this.Dispose();
      Application.Exit();
    }

    // About
    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
      /* local string */
      string str ;
      /* store string to variables */
      str = "Serial Transfer\r\n" ;
      str += "Copy Right : Kensuke Ooyu\r\n" ;
      str += "2010/04/02" ;
      /* show */
      MessageBox.Show(str);
    }

    // data set delegate
    delegate void TextSet(string stxt);

    //
    private void sciDTB_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
      try
      {
        int length;
        string result;
        // clear result
        result = "";
        // get buffer size
        length = sciDTB.ReadBufferSize;
        if (length > 0)
        {
          // malloc
          char[] rcvData = new char[length + 1];
          // get context from receive buffer
          sciDTB.Read(rcvData, 0, length + 1);
          // copy
          for (int i = 0; i < length; i++)
          {
            result += rcvData[i].ToString();
          }
          // MessageBox.Show(result); 
          Invoke(new TextSet(rMyTextBox.SetText), new object[] { result });
        }
      }
      catch (System.IO.IOException myIOe)
      {
        MessageBox.Show(myIOe.ToString(), "ERROR:IOException");
      }
      catch (Exception myEx)
      {
        MessageBox.Show(myEx.ToString(), "ERROR:OTHER ERROR");
      }
    }

    // Receive thread error
    private void sciDTB_ErrorReceived(object sender, System.IO.Ports.SerialErrorReceivedEventArgs e)
    {
      MessageBox.Show("COM port error !");
    }

    // data save delegate
    delegate void SaveText(string text);

    // save to file
    private void btnSave_Click(object sender, EventArgs e)
    {
      // judge
      if (textFileName.Text.Length > 0)
      {
        // generate file name with path
        gFName = textDir.Text + "\\" + textFileName.Text;
        //MessageBox.Show(gFName);
        Invoke(new SaveText(rMyTextBox.SaveText), new object[] { gFName });
      }
      else
      {
        MessageBox.Show("No file name !");
      }
    }
  }

    #region text box class (has some method)
    public class MyTextBox : TextBox
    {
      public MyTextBox() { }

      // append text
      public void SetText(string stxt)
      {
        this.Text = this.Text + stxt;
      }

      // clear text
      public void ClrText()
      {
        this.Text = "";
      }

      // save context of text area
      public void SaveText(string stxt)
      {
        if (this.Text.Length > 0)
        {
          StreamWriter sw ;
          // generate and open Wirte File Stream
          sw = new StreamWriter(new FileStream(stxt,FileMode.Create,FileAccess.Write));
          // save context
          sw.Write(this.Text);
          // close 
          sw.Close();
        }
        else
        {
          MessageBox.Show("No data!");
        }
      }
  }
  #endregion
}

目次

inserted by FC2 system