using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace RscTest1 { public partial class Form1 : Form { //フィールド(クラス内で使う変数宣言) private byte[] sendData; //送信データの配列を宣言 //コンストラクタ(起動時処理) public Form1() { InitializeComponent(); //デザイナで作ったところ serialPort1.PortName = "COM1"; //使うポートをCOM1に設定 serialPort1.BaudRate = 9600; //通信速度を9600bpsに設定 serialPort1.Open(); //シリアルポートをオープン } //マウスクリックイベント private void runButton_Click(object sender, EventArgs e) { sendData = new byte[7]; //配列を実体化 sendData[0] = 255; //シンクロバイトをセット sendData[1] = Convert.ToByte(id.Text); //IDをセット sendData[2] = 4; //命令長をセット sendData[3] = 2; //命令をセット sendData[4] = Convert.ToByte(servoNum.Text);//サーボ番号をセット sendData[5] = Convert.ToByte(servoPos.Text);//サーボ位置をセット sendData[6] = Convert.ToByte(servoSpd.Text);//サーボ速度をセット serialPort1.Write(sendData, 0, 7); //配列0番目から7個送信 } } }