Tools
ツールズ

抵抗計算ソフト 


回路を設計していると ちょうど良い抵抗が手持ちになかったり 今付いているものにパラに入れて調整したかったりと、なにかと パラ(並列)計算をする機会が多いものです。 そこで、昔から ベーシックソフトで書いたものを N88Basicsシミュレータ を用いて使っていたのですが、C# を使って書き換えたので 公開することにしました。

下図のようなUIを持つソフトです
 

赤の箱に数値をいれて、計算スタートボタンを
押すだけの簡単なソフトですが、下図のように
2本の抵抗値から、並列値も 計算できます。



File の HELP を開くと下図のような使い方
を示したフォームが開きます。


 
さてこのソフトの作り方ですが、
Microsoft Visual Stdio の
C#で作ります。

プロジェクト(デザイン)画面


下図に form1 のソースを示す。
なんかの役に立てば ・ ・ 幸い

// Ver. 2.0R6 : 2018.10.06.

namespace 抵抗計算123
{
   public partial class Form1 : Form
   {
      int e_sel = 12, B;
      double r_value=0;
      double rp1 = 1, rp2 = 1,rx1,rpp;    //並列抵抗1,2
      double rs1 = 1, rs2 = 1,rx2,rss;    //直列抵抗1,2
      double[,] R;
      int i_step = 2;
      int gousei = 1;

      public Form1()
      {
          double B;
          InitializeComponent();
          R = newdouble[11, 25];
          B = 1;
          for (int i = 1; i < 10; i++)
          {
              R[i, 1 ]= 1.0 * B;   //E12
              R[i, 2 ]= 1.1 * B;
              R[i, 3 ]= 1.2 * B;   //E12
              R[i, 4 ]= 1.3 * B;
              R[i, 5 ]= 1.5 * B;   //E12
              R[i, 6 ]= 1.6 * B;
              R[i, 7 ]= 1.8 * B;   //E12
              R[i, 8 ]= 2.0 * B;
              R[i, 9 ]= 2.2 * B;   //E12
              R[i, 10 ]= 2.4 * B;
              R[i, 11 ]= 2.7 * B;   //E12
              R[i, 12 ]= 3.0 * B;
              R[i, 13 ]= 3.3 * B;   //E12
              R[i, 14 ]= 3.6 * B;
              R[i, 15 ]= 3.9 * B;   //E12
              R[i, 16 ]= 4.3 * B;
              R[i, 17 ]= 4.7 * B;   //E12
              R[i, 18 ]= 5.1 * B;
              R[i, 19 ]= 5.6 * B;   //E12
              R[i, 20 ]= 6.2 * B;
              R[i, 21 ]= 6.8 * B;   //E12
              R[i, 22 ]= 7.5 * B;
              R[i, 23 ]= 8.2 * B;   //E12
              R[i, 24 ]= 9.1 * B;
              B = B * 10;
          }
      }

       privatevoid Form1_Load(object sender, EventArgs e)
      {
           this.ActiveControl = this.textBox5;
          //this.textBox5.Focus();
      }

       privatevoid Form1_Shown(object sender, EventArgs e)
      {
          textBox5.Focus();

      }

       privatevoid r_value_calc()
       {
           double a, b;
           double rs = r_value;
           double rp_temp = 9999999;        //近い値の暫定値Para
           double rs_temp = 9999999;        //近い値の暫定値Srias
           if (gousei == 1)                 //合成値から抵抗値を求める
           {
               for (int i1 = 1; i1 < 9; i1++)
               {
                   for (int j1 = 1; j1 < 24; j1 += i_step)
                   {
                       for (int i2 = 1; i2 < 9; i2++)
                       {
                           for (int j2 = 1; j2 < 24; j2 += i_step)
                           {
                               a = R[i1, j1];
                               b = R[i2, j2];

                               rx1 = a * b /(a + b);                  //並列合成値
                               if (Math.Sqrt((rx1 - rs )*(rx1 - rs ))< rp_temp)
                               {
                                   rp_temp = Math.Sqrt((rx1 - rs )*(rx1 - rs));
                                   rp1 = a;
                                   rp2 = b;
                                   rpp = rx1;
                               }

                               rx2 = a + b;                           //直列合成値
                               if (Math.Sqrt((rx2 - rs )*(rx2 - rs ))< rs_temp)   
                               {
                                   rs_temp = Math.Sqrt((rx2 - rs )*(rx2 - rs));
                                   rs1 = a;
                                   rs2 = b;
                                   rss = rx2;
                               }
                           }
                       }
                   }
               }
               textBox1.Text = rp1.ToString("n0");                 //n=0 小数点0桁
               if (this.textBox1.TextLength == 1)
               {
                   textBox1.Text = rp1.ToString("n1");             //n=1 小数点1桁
               }
               if (Math.Sqrt((rp1 - rs )*(rp1 - rs ))> 0.1)
               {    //1本で抵抗値となる?
                   textBox2.Text = rp2.ToString("n0");             //小数点0桁
               }
               else
               {
                   textBox2.Text = " ";              //1本で抵抗値となる場合は空欄
               }
               textBox3.Text = rpp.ToString("f2");                 //小数点2桁
               textBox4.Text =((rpp - rs )/ rs).ToString("p");    //パーセント
               textBox7.Text = rs1.ToString("n1");
               textBox8.Text = rs2.ToString("n1");
               textBox9.Text = rss.ToString("f2");
               textBox10.Text =((rss - rs )/ rs).ToString("p");
               Refresh();
           }
           else //抵抗値から合成値を求める
           {
               rx1 = Convert.ToInt32(textBox1.Text);
               rx2 = Convert.ToInt32(textBox2.Text);
               if ((rx1 * rx2 )< 1)
               {
                   Console.Beep();
                   //
               }
               else
               {
                   rpp =(rx1 * rx2 )/(rx1 + rx2);
                   textBox5.Text = rpp.ToString("f2");
                   textBox4.Text = " ";
                   textBox3.Text = rpp.ToString("f2");
                   //textBox3.Text = " ";
                   textBox7.Text = " " ;
                   textBox8.Text = " " ;
                   textBox9.Text = " " ;
                   textBox10.Text = " " ;
                   Refresh();
               }
           }
       }

       //TextBox(*)のKeyPressイベントハンドラ
        privatevoid TextBox5_KeyPress(object sender,System.Windows.Forms.KeyPressEventArgs e)
       {
           if ((e.KeyChar < '0' || '9' < e.KeyChar))
           {
               if (e.KeyChar ==(char)0x0d)
               {
                   textBox1.Text = "";
                   textBox2.Text = "";
                   Refresh();
                   if (textBox5.Text != "") r_value = double.Parse(textBox5.Text);
                   if (r_value >= 1) r_value_calc();
               }
           }

       }
        privatevoid TextBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
       {
           if (e.KeyChar < '0' || '9' < e.KeyChar)
           {
               //押されたキーが 0~9でない場合は、イベントをキャンセルする
               e.Handled = true;
           }
       }
        privatevoid TextBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
       {
           if (e.KeyChar < '0' || '9' < e.KeyChar)
           {
               //押されたキーが 0~9でない場合は、イベントをキャンセルする
              e.Handled = true;
           }
       }

        privatevoid radioButton3_CheckedChanged(object sender, EventArgs e)
       {
           e_sel = 12;
           radioButton3.Font = new Font(radioButton3.Font, FontStyle.Bold);
           radioButton4.Font = new Font(radioButton4.Font, FontStyle.Regular);
           i_step = 2;
           if (gousei == 1)
           {
               textBox5.SelectionStart = 0;
               textBox5.SelectionLength = 0;
               textBox5.Focus();
           }
           else
           {
               textBox1.SelectionStart = 0;
               textBox1.SelectionLength = 0;
               textBox1.Focus();
           }
       }

        privatevoid radioButton4_CheckedChanged(object sender, EventArgs e)
       {
           e_sel = 24;
           radioButton3.Font = new Font(radioButton3.Font, FontStyle.Regular);
           radioButton4.Font = new Font(radioButton4.Font, FontStyle.Bold);
           i_step = 1;
           if (gousei == 1)
            {
               textBox5.SelectionStart = 0;
               textBox5.SelectionLength = 0;
               textBox5.Focus();
           }
           else
           {
               textBox1.SelectionStart = 0;
               textBox1.SelectionLength = 0;
               textBox1.Focus();
           }
       }

        privatevoid button1_Click(object sender, EventArgs e)
       {
           if (gousei == 1)
           {
               textBox1.Text = "";
               textBox2.Text = "";
               Refresh();
               if (textBox5.Text != "") r_value = double.Parse(textBox5.Text);
               if (r_value >= 1) r_value_calc();

           }
           else
           {
               textBox5.Text = "";
               r_value_calc();
               Refresh();
           }
       }

        privatevoid textBox5_KeyDown(object sender, KeyEventArgs e)
       {
           try
           {
               if (e.KeyCode == Keys.Enter)
               {
                   if (textBox5.Text != "") r_value = double.Parse(textBox5.Text);
                   if (r_value >= 1) r_value_calc();
               }
           }

           catch (System.Exception ex)
           {
               //すべての例外をキャッチする
               //なにもしない
           }
       }

        privatevoid radioButton1_CheckedChanged(object sender, EventArgs e)
       {
           gousei = 1;
           radioButton1.Font = new Font(radioButton1.Font, FontStyle.Bold);
           radioButton2.Font = new Font(radioButton2.Font, FontStyle.Regular);
           textBox5.Text = "";
           textBox1.Text = "";
           textBox2.Text = "";

           textBox4.Text = " ";
           textBox3.Text = " ";
           textBox7.Text = " ";
           textBox8.Text = " ";
           textBox9.Text = " ";
           textBox10.Text = " ";

           this.textBox5.BackColor = Color.Pink;
           this.textBox1.BackColor = Color.LightCyan;
           this.textBox2.BackColor = Color.LightCyan;
           this.textBox3.BackColor = Color.WhiteSmoke;
           this.textBox4.BackColor = Color.WhiteSmoke;
           this.textBox7.BackColor = Color.LightCyan;
           this.textBox8.BackColor = Color.LightCyan;
           this.textBox9.BackColor = Color.WhiteSmoke;
           this.textBox10.BackColor = Color.WhiteSmoke;

           textBox5.SelectionStart = 0;
           textBox5.SelectionLength = 0;
           textBox5.Focus();
           Refresh();
       }

        privatevoid radioButton2_CheckedChanged(object sender, EventArgs e)
       {
           gousei = 0;
           radioButton2.Font = new Font(radioButton2.Font, FontStyle.Bold);
           radioButton1.Font = new Font(radioButton1.Font, FontStyle.Regular);
           textBox5.Text = "";
           textBox1.Text = "";
           textBox2.Text = "";

           textBox4.Text = " ";
           textBox3.Text = " ";
           textBox7.Text = " ";
           textBox8.Text = " ";
           textBox9.Text = " ";
           textBox10.Text = " ";

           this.textBox5.BackColor = Color.LightCyan;
           this.textBox1.BackColor = Color.Pink;
           this.textBox2.BackColor = Color.Pink;
           this.textBox4.BackColor = Color.DimGray;
           this.textBox7.BackColor = Color.DimGray;
           this.textBox8.BackColor = Color.DimGray;
           this.textBox9.BackColor = Color.DimGray;
           this.textBox10.BackColor = Color.DimGray;
           this.textBox3.BackColor = Color.LightCyan;

           textBox1.SelectionStart = 0;
           textBox1.SelectionLength = 0;
           textBox1.Focus();
           Refresh();
       }

        privatevoid HELPToolStripMenuItem_Click(object sender, EventArgs e)
       {
           //this.Size = new Size(400, 300);
           Help.ShowHelp(this, "抵抗計算V2.chm");
       }

        privatevoid バージョン情報ToolStripMenuItem_Click(object sender, EventArgs e)
       {
           VersionForm ver = new VersionForm();
           ver.ShowDialog();
       }

     }
}

 
 
実際の実行ファイルを 下記に置きますので
ダウンロードして お使いください。

実行ファイルは こちら



実行ファイルとヘルプファイル2つの構成です。
ZIPファイルを解凍してそのまま使用できます。






< home >



(c)2018 mi-take