private void btnStart_Click(object sender, EventArgs e)
{
try
{
SerialPortWriteAndRead("10 03 EB 00 01 02 00 01");//设置信号源为 RS485 通讯控制
SerialPortWriteAndRead("10 03 EC 00 01 02 00 00");//设置用户控制模式为正常
if (radioButton1.Checked)
{
string positionNum = int.Parse(cbPositionNum.Text).ToString("X4");
SerialPortWriteAndRead("10 03 EF 00 01 02" + positionNum);//执行点位命令
}
else if (radioButton2.Checked)
{
string lengthPush = int.Parse(txbLength.Text).ToString("X4");
string speedPush = int.Parse(txbSpeedPush.Text).ToString("X4");
SerialPortWriteAndRead("10 04 02 00 02 04 " + lengthPush + speedPush);//设置推压段距离
和速度百分比
string position = ((int)(double.Parse(txbPosition.Text.ToString()) *
100)).ToString("X4");
string speed = ((int)(double.Parse(txbSpeed.Text.ToString()) * 100)).ToString("X4");
string strength = int.Parse(txbStrength.Text).ToString("X4");
SerialPortWriteAndRead("10 03 FE 00 02 04" + speed + strength);//写入速度和推力
SerialPortWriteAndRead("10 03 FD 00 01 02" + position);//写入位置
SerialPortWriteAndRead("10 03 EF 00 01 02 00 05");//执行非可存位置
SerialPortWriteAndRead("10 03 ED 00 01 02 00 01");//使能电缸
}
else if (radioButton3.Checked)
{
string position = Math.Abs((int)(double.Parse(txbPosition2.Text.ToString()) *
100)).ToString("X4");
SerialPortWriteAndRead("10 04 00 00 01 02" + position);//写入位置
SerialPortWriteAndRead("10 03 F2 00 01 02" + "03E8");//写入速度
SerialPortWriteAndRead("10 03 F3 00 01 02" + "01F4");//写入推力
SerialPortWriteAndRead("10 03 EF 00 01 02 00 04");//执行相对运动模式
if (double.Parse(txbPosition2.Text) >=0)
{
SerialPortWriteAndRead("10 03 F0 00 01 02 00 01");//触发正向相对运动