public static void timerAction(FrmMain fm, int type)
        {
            if (fm.radioButton1.Checked == true)
            {
                if (fm.textBox2.Text.Trim() == "")
                {
                    if (type == 1)
                    {
                        fm.timer1.Enabled    = false;
                        fm.button1.Enabled   = false;
                        fm.groupBox2.Enabled = true;
                        fm.button5.Text      = "开始发送";
                    }
                    MessageBox.Show("如果要发送单字符串 请填写字符");
                    fm.textBox2.Focus();
                }
                else
                {
                    if (type == 1)
                    {
                        fm.button5.Text      = "停止发送";
                        fm.groupBox2.Enabled = false;
                    }
                    if (fm.radioButton4.Checked == true)
                    {
                        SendCommand.action(fm.textBox2.Text.Trim(), fm);
                    }
                    else if (fm.radioButton5.Checked == true)
                    {
                        fm.serialPort1.Write(fm.textBox2.Text.Trim());
                    }
                }
            }

            else if (fm.radioButton2.Checked == true)
            {
                int num = 0;
                if (fm.textBox3.Text.Length != 0)
                {
                    if (fm.radioButton4.Checked == true)
                    {
                        SendCommand.action(fm.textBox3.Text.Trim(), fm);
                        System.Threading.Thread.Sleep(50);
                    }
                    else if (fm.radioButton5.Checked == true)
                    {
                        fm.serialPort1.Write(fm.textBox3.Text.Trim());
                        System.Threading.Thread.Sleep(50);
                    }
                }
                else
                {
                    num++;
                }
                if (fm.textBox4.Text.Length != 0)
                {
                    if (fm.radioButton4.Checked == true)
                    {
                        SendCommand.action(fm.textBox4.Text.Trim(), fm);
                        System.Threading.Thread.Sleep(50);
                    }
                    else if (fm.radioButton5.Checked == true)
                    {
                        fm.serialPort1.Write(fm.textBox4.Text.Trim());
                        System.Threading.Thread.Sleep(50);
                    }
                }
                else
                {
                    num++;
                }
                if (fm.textBox5.Text.Length != 0)
                {
                    if (fm.radioButton4.Checked == true)
                    {
                        SendCommand.action(fm.textBox5.Text.Trim(), fm);
                        System.Threading.Thread.Sleep(50);
                    }
                    else if (fm.radioButton5.Checked == true)
                    {
                        fm.serialPort1.Write(fm.textBox5.Text.Trim());
                        System.Threading.Thread.Sleep(50);
                    }
                }
                else
                {
                    num++;
                }
                if (fm.textBox6.Text.Length != 0)
                {
                    if (fm.radioButton4.Checked == true)
                    {
                        SendCommand.action(fm.textBox6.Text.Trim(), fm);
                        System.Threading.Thread.Sleep(50);
                    }
                    else if (fm.radioButton5.Checked == true)
                    {
                        fm.serialPort1.Write(fm.textBox6.Text.Trim());
                        System.Threading.Thread.Sleep(50);
                    }
                }
                else
                {
                    num++;
                }
                if (num == 4)
                {
                    if (type == 1)
                    {
                        fm.timer1.Enabled    = false;
                        fm.groupBox2.Enabled = true;
                        fm.button1.Enabled   = false;
                        fm.button5.Text      = "开始发送";
                    }
                    MessageBox.Show("如果要多字符串发送 请把命令填写上");
                }
                else
                {
                    if (type == 1)
                    {
                        fm.button5.Text      = "停止发送";
                        fm.groupBox2.Enabled = false;
                    }
                }
            }

            else if (fm.radioButton3.Checked == true)
            {
                if (fm.commmand.Count == 0)
                {
                    fm.timer1.Enabled = false;
                    MessageBox.Show("请先导入命令文件");
                    return;
                }

                if (type == 1)
                {
                    fm.button5.Text      = "停止发送";
                    fm.groupBox2.Enabled = false;
                }

                if (fm.radioButton4.Checked == true)
                {
                    for (int counter = 0; counter < fm.commmand.Count; counter++)
                    {
                        SendCommand.action(fm.commmand[counter].ToString(), fm);
                        System.Threading.Thread.Sleep(50);
                    }
                }
                else if (fm.radioButton5.Checked == true)
                {
                    for (int counter = 0; counter < fm.commmand.Count; counter++)
                    {
                        fm.serialPort1.Write(fm.textBox6.Text.Trim());
                        System.Threading.Thread.Sleep(50);
                    }
                }
            }
        }
Exemple #2
0
 /// <summary>
 /// Modbus写保持寄存器发送主机代码10H
 /// </summary>
 public static void timer2Action_SendFc10(byte address, ushort start, ushort registers, ushort[] values, FrmMain fm)
 {
     if (fm.button1.Text == "关闭串口")
     {//此处使用动态内存,当使用帧延时发送时会导致每次都算一遍数据
         byte[] message = new byte[9 + 2 * registers];
         message[6] = (byte)(registers * 2);
         //字节计数,寄存器数量*2
         message[6] = (byte)(registers * 2);
         //保存的数据(需写入的数据)
         for (int i = 0; i < registers; i++)
         {
             message[7 + 2 * i] = (byte)(values[i] >> 8);
             message[8 + 2 * i] = (byte)(values[i]);
         }
         ModbusRTU.BuildMessage(address, 0x10, start, registers, ref message);
         ReadLenth = 8;
         fm.serialPort1.ReceivedBytesThreshold = ReadLenth;
         if (SendCount == 0)
         {
             if (frameDelayType == false)
             {
                 SendCount = 1;
             }
             else
             {
                 SendCount = 0;
             }
         }
         if (ModbusTimeOut == 0)
         {
             if (frameDelayType == false)
             {
                 SendCommand.SendBytes(message, message.Length, fm);
                 ModbusTimeOut     = 1;
                 fm.timer1.Enabled = true;
                 SendCount         = 0;
             }
             else
             {
                 //  byte[] Txtest = new byte[1];
                 Txtest[0] = message[SendCount++];
                 SendCommand.SendOneByte(Txtest, fm);
                 if (SendCount >= message.Length)
                 {//定时器1用来做帧延时,定时器2做Modbus超时
                     ModbusTimeOut     = 1;
                     fm.timer1.Enabled = true;
                     SendCount         = 0;
                 }
             }
         }
         if (SerialRecOk == 1)
         {
             ModbusTimeOut     = 0;
             SerialRecOk       = 0;
             SendCount         = 0;
             fm.timer1.Enabled = false;
         }
     }
     else
     {
         fm.timer2.Enabled = false;
         fm.timer1.Enabled = false;
     }
 }
Exemple #3
0
 public static void SendOneByte(byte[] TByte, FrmMain fm)
 {
     SendCommand.SendBytes(TByte, 1, fm);
 }