全ソースコード
作成したシステムのソースコードです。
デバッグするために、ところどころにメッセージを入れる
コードが入っています。
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 mnuGUI
{
public partial class mainForm : Form
{
bool gPflag;
static private int grip_max = 200 ;
static private int grip_min = 100 ;
static private int wrist_max = 200 ;
static private int wrist_min = 100 ;
static private int arm_max = 200 ;
static private int arm_min = 100 ;
static private int elbow_max = 200 ;
static private int elbow_min = 100 ;
static private int waist_max = 200 ;
static private int waist_min = 100 ;
static private int dvalue = 150;
static private int LAST = 5 ;
private decimal [] now ;
private decimal [] prev;
private decimal number;
bool dflag ;
static private int key_up_val = 1;
static private int key_down_val = -1;
static private int obj_grip = 0;
static private int obj_wrist = 1;
static private int obj_arm = 2;
static private int obj_elbow = 3;
static private int obj_waist = 4;
public mainForm()
{
InitializeComponent();
}
// btnExit handler
private void btnExit_Click(object sender, EventArgs e)
{
// judge
if (sciDTB.IsOpen) { sciDTB.Close(); }
// End
Application.Exit();
}
// btnOpen handler
private void btnOpen_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();
// update button states
gPflag = true;
btnOpen.Enabled = false;
btnClose.Enabled = true;
}
catch (System.IO.IOException myIOe)
{
MessageBox.Show(myIOe.ToString(), "ERROR:COM port not exists!");
gPflag = false;
btnOpen.Enabled = true;
btnClose.Enabled = false;
}
}
// btnClose handler
private void btnClose_Click(object sender, EventArgs e)
{
// judge
if (!gPflag) return;
// change flag state
gPflag = false;
btnOpen.Enabled = true;
btnClose.Enabled = false;
// Close Port
sciDTB.Close();
}
private void mainForm_Load(object sender, EventArgs e)
{
// initialize serial port
{
sciDTB.BaudRate = 9600;
sciDTB.RtsEnable = false;
sciDTB.DtrEnable = false;
sciDTB.ReadBufferSize = 8192;
sciDTB.PortName = "COM1";
sciDTB.Encoding = System.Text.Encoding.GetEncoding("shift_jis");
}
// button control
gPflag = false;
dflag = false;
btnOpen.Enabled = true;
btnClose.Enabled = false;
// set range
{
// max
nudGrip.Maximum = grip_max;
nudWrist.Maximum = wrist_max;
nudArm.Maximum = arm_max;
nudElbow.Maximum = elbow_max;
nudWaist.Maximum = waist_max;
// min
nudGrip.Minimum = grip_min;
nudWrist.Minimum = wrist_min;
nudArm.Minimum = arm_min;
nudElbow.Minimum = elbow_min;
nudWaist.Minimum = waist_min;
}
// set default
{
set_default_value() ;
// machin number
nudNumber.Value = 0 ;
}
// reserve memory
{
now = new decimal[5];
prev = new decimal[5];
for (int i = 0; i < LAST; i++)
{
now[i] = dvalue ;
prev[i] = dvalue ;
}
}
// enable timer
timHandler.Enabled = true;
}
// btnInit handler
private void btnInit_Click(object sender, EventArgs e)
{
// default
set_default_value() ;
// set default value to internal memory
for (int i = 0; i < LAST; i++)
{
now[i] = dvalue;
prev[i] = dvalue;
}
}
// btnSend handler
private void btnSend_Click(object sender, EventArgs e)
{
// get values
now[0] = nudGrip.Value ;
now[1] = nudWrist.Value ;
now[2] = nudArm.Value ;
now[3] = nudElbow.Value ;
now[4] = nudWaist.Value ;
number = nudNumber.Value;
// looping
dflag = true;
}
// btnClear handler
private void btnClear_Click(object sender, EventArgs e)
{
edtText.Text = "" ;
}
// timer handler
private void timHandler_Tick(object sender, EventArgs e)
{
int loop ;
int cnt ;
String stmp ;
// get machine number
number = nudNumber.Value;
// judge
if ( dflag == true ) {
stmp = "";
for (loop = 0; loop < LAST; loop++)
{
// judge
if (now[loop] == prev[loop]) continue;
// update
if (now[loop] > prev[loop])
{
prev[loop]++;
}
else
{
prev[loop]--;
}
// generate command
stmp = stmp + "A" + number.ToString();
stmp = stmp + loop.ToString() + prev[loop].ToString();
stmp = stmp + '\r';
}
// send
if (sciDTB.IsOpen)
{
sciDTB.Write(stmp);
}
// show
edtText.Text = edtText.Text + stmp + '\n';
// count up differences
cnt = 0;
for (loop = 0; loop < LAST; loop++)
{
if (now[loop] != prev[loop])
{
cnt++;
}
}
// judge
if (cnt == 0)
{
for (loop = 0; loop < LAST; loop++)
{
prev[loop] = now[loop];
}
dflag = false;
}
}
}
// defualt
private void set_default_value()
{
nudGrip.Value = dvalue ;
nudWrist.Value = dvalue ;
nudArm.Value = dvalue ;
nudElbow.Value = dvalue ;
nudWaist.Value = dvalue ;
}
private void nud_key_handler(int x,int y)
{
decimal max;
decimal min;
decimal tmp;
// get now values
switch (x)
{
case 1 :
max = nudWrist.Maximum;
min = nudWrist.Minimum;
tmp = nudWrist.Value;
break;
case 2 :
max = nudArm.Maximum;
min = nudArm.Minimum;
tmp = nudArm.Value;
break;
case 3 :
max = nudElbow.Maximum;
min = nudElbow.Minimum;
tmp = nudElbow.Value;
break;
case 4 :
max = nudWaist.Maximum;
min = nudWaist.Minimum;
tmp = nudWaist.Value;
break;
default :
max = nudGrip.Maximum;
min = nudGrip.Minimum;
tmp = nudGrip.Value;
break;
}
// up
if ( y == key_up_val )
{
if (max > tmp)
{
tmp++;
}
}
// down
if ( y == key_down_val )
{
if (min < tmp)
{
tmp--;
}
}
// update
switch (x)
{
case 1 : nudWrist.Value = tmp ; break;
case 2 : nudArm.Value = tmp ; break;
case 3 : nudElbow.Value = tmp ; break;
case 4 : nudWaist.Value = tmp ; break;
default: nudGrip.Value = tmp ; break;
}
}
private void nudGrip_KeyDown(object sender, KeyEventArgs e)
{
// up
if (e.KeyValue == 38)
{
nud_key_handler( obj_grip, key_up_val );
}
// down
if (e.KeyValue == 40)
{
nud_key_handler( obj_grip , key_down_val);
}
}
private void nudWrist_KeyDown(object sender, KeyEventArgs e)
{
// up
if (e.KeyValue == 38)
{
nud_key_handler( obj_wrist , key_up_val);
}
// down
if (e.KeyValue == 40)
{
nud_key_handler( obj_wrist , key_down_val );
}
}
private void nudArm_KeyDown(object sender, KeyEventArgs e)
{
// up
if (e.KeyValue == 38)
{
nud_key_handler( obj_arm , key_up_val );
}
// down
if (e.KeyValue == 40)
{
nud_key_handler( obj_arm , key_down_val );
}
}
private void nudElbow_KeyDown(object sender, KeyEventArgs e)
{
// up
if (e.KeyValue == 38)
{
nud_key_handler( obj_elbow , key_up_val );
}
// down
if (e.KeyValue == 40)
{
nud_key_handler( obj_elbow , key_down_val );
}
}
private void nudWaist_KeyDown(object sender, KeyEventArgs e)
{
// up
if (e.KeyValue == 38)
{
nud_key_handler( obj_waist , key_up_val);
}
// down
if (e.KeyValue == 40)
{
nud_key_handler( obj_waist , -1);
}
}
}
}
目次
前