1.Button1 按下为True,Button2按下为false
// M102打开
private void butt1_Click_1(object sender, EventArgs e)
{
string _Addr = "102";
Int16 _value = Int16.Parse("1");
bool[] _Send = new bool[1];
if (_value > 0)
{
_Send[0] = true;
}
else
{
_Send[0] = false;
}
mform1.P_mPLC.DB_WriteByte(UInt16.Parse(_Addr), _Send); // 发送数据到PLC
}
//M102关闭
private void butt2_Click(object sender, EventArgs e)
{
string _Addr = "102";
Int16 _Value = Int16.Parse("0");
bool[] _Send = new bool[1];
if (_Value > 0)
{
_Send[0] = true;
}
else
{
_Send[0] = false;
}
mform1.P_mPLC.DB_WriteByte(UInt16.Parse(_Addr), _Send); // 发送数据到PLC
PS: 注意在事件属性中要选择对应的事件
2.Button1 按下为True,松开为false ,点动
//单按钮切换,点动
private void button10_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
string _Addr = "104";
Int16 _Value = Int16.Parse("1");
bool[] _Send = new bool[1];
_Send[0] = true; // 如果值大于0,设置为true,否则为false
mform1.P_mPLC.DB_WriteByte(UInt16.Parse(_Addr), _Send); // 发送数据到PLC
}
}
private void button10_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
string _Addr = "104";
Int16 _Value = Int16.Parse("0");
bool[] _Send = new bool[1];
_Send[0] = false; // 如果值大于0,设置为true,否则为false
mform1.P_mPLC.DB_WriteByte(UInt16.Parse(_Addr), _Send); // 发送数据到PLC
}
}
3. 循环点动,取反点动
// 单击切换状态自动手动
private bool leftButtonDown5 = false;
private void button10_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// 切换leftButtonDown的状态
leftButtonDown5 = !leftButtonDown5;
string _Addr = "105";
Int16 _Value = leftButtonDown5 ? Int16.Parse("1") : Int16.Parse("0");
bool[] _Send = new bool[1];
_Send[0] = leftButtonDown5;
//_Send[0] = true; // 设置_Send数组的值根据leftButtonDown的状态
mform1.P_mPLC.DB_WriteByte(UInt16.Parse(_Addr), _Send); // 发送数据到PLC
if (leftButtonDown5 == true)
{
button10.BackColor = Color.Green;
button10.Text = "手动模式";
}
else
{
button10.BackColor = Color.Red;
button10.Text = "自动模式";
}
}
}