Beispiel #1
0
 /// <summary>
 /// 生成主机发送命令(功能码为03H(只读)时,生成全部命令;功能码为10H(写)时,生成部分命令(未生成写入的数据);功能码为01H时,生成全部命令)
 /// </summary>
 /// <param name="address">设备地址</param>
 /// <param name="functionCode">功能码</param>
 /// <param name="start">起始地址(寄存器地址)</param>
 /// <param name="registers">数据长度(寄存器数量)</param>
 /// <param name="message">存放生成的主机发送命令</param>
 public static void BuildMessage(byte address, byte functionCode, ushort start, ushort registers, ref byte[] message)
 {
     //用于存放生成的CRC码
     //  byte[] CRC = new byte[2];
     byte[] CRC1 = new byte[2];
     byte[] tst  = new byte[2];
     //生成主机发送命令
     message[0] = address;
     message[1] = functionCode;
     message[2] = (byte)(start >> 8);
     message[3] = (byte)start;
     message[4] = (byte)(registers >> 8);
     message[5] = (byte)registers;
     //根据命令生成CRC码
     //   GetCRC(message, ref CRC);
     TimerAction.crc16(message, Convert.ToUInt32(message.Length - 2), ref CRC1);
     //  message[message.Length - 2] = CRC[0];//低8位
     //  message[message.Length - 1] = CRC[1];//高8位
     message[message.Length - 2] = CRC1[0]; //CRC高8位,电脑采用小端模式存数据
     message[message.Length - 1] = CRC1[1]; //CRC低8位,Modbus采用大端模式
 }
Beispiel #2
0
 private void button5_Click(object sender, EventArgs e)
 {
     if (serialPort1.IsOpen)
     {
         if (button5.Text == "开始发送")
         {
             if (checkBox1.Checked == true)
             {
                 type            = 1;
                 timer1.Interval = Convert.ToInt32(numericUpDown1.Value) * 1000;
                 timer1.Enabled  = true;
                 button1.Enabled = false;
             }
             else
             {
                 TimerAction.timerAction(this, type);
             }
         }
         else if (button5.Text == "停止发送")
         {
             timer1.Enabled    = false;
             button1.Enabled   = true;
             textBox1.Text     = "";
             groupBox2.Enabled = true;
             button5.Text      = "开始发送";
         }
     }
     else
     {
         timer1.Enabled    = false;
         groupBox2.Enabled = true;
         textBox1.Text     = "";
         button5.Text      = "开始发送";
         MessageBox.Show("请先打开串口 再发送数据");
     }
 }
Beispiel #3
0
 private void timer1_Tick(object sender, EventArgs e)
 {
     TimerAction.timerAction(this, type);
 }