C# Modbus Rtu

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace 测试Modbus_Rtu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    bool a;
    bool start;
    byte[] bb1;
    bool stop;
    byte[] bt2;

  // modbus Rtu校验;
    public static ushort CRC16(byte[] bytes)
    {
        ushort value;
        ushort newLoad = 0xffff, In_value;
        int count = 0;
        for (int i = 0; i < bytes.Length; i++)
        {
            value = (ushort)bytes[i];
            newLoad = (ushort)(Convert.ToInt32(value) ^ Convert.ToInt32(newLoad));
            In_value = 0xA001;
            while (count < 8)
            {
                if (Convert.ToInt32(newLoad) % 2 == 1)//判断最低位是否为1
                {
                    newLoad -= 0x00001;
                    newLoad = (ushort)(Convert.ToInt32(newLoad) / 2);//右移一位
                    count++;//计数器加一
                    newLoad = (ushort)(Convert.ToInt32(newLoad) ^ Convert.ToInt32(In_value));//异或操作
                }
                else
                {
                    newLoad = (ushort)(Convert.ToInt32(newLoad) / 2);//右移一位
                    count++;//计数器加一
                }
            }
            count = 0;
        }
        return newLoad;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Control.CheckForIllegalCrossThreadCalls = false;
        serialPort1.PortName = "COM4";
        serialPort1.BaudRate = 9600;
        serialPort1.Parity = Parity.None;
        serialPort1.DataBits = 8;
        serialPort1.StopBits = StopBits.One;
        serialPort1.ReceivedBytesThreshold = 1;
        serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
        serialPort1.Open();
        textBox1.AppendText("串口成功打开");
    }

    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        Thread.Sleep(100);

        if (serialPort1.BytesToRead == 0)
        {
            return;
        }
        else
        {
            byte[] bb = new byte[1024];
            int c = serialPort1.BytesToRead;
            serialPort1.Read(bb, 0, bb.Length);
            if (a)
            {
                byte[] bn = new byte[4];
                bn[0] = bb[6];
                bn[1] = bb[5];
                bn[2] = bb[4];
                bn[3] = bb[3];
                textBox2.Text = BitConverter.ToInt32(bn, 0).ToString();
                a = false;
                textBox1.AppendText("\r\n数据返回成功");
            }
            //Q0.0 启动回传判断;
            if (start & c==8)
            {
                if (bb1[0] == bb[6] & bb1[1] == bb[7])
                {
                    button1.BackColor = Color.Red;
                    button1.Text = "已启动";
                    start = false;
                    textBox1.AppendText("\r\n %q0.0启动成功");
                }
            }
            // Q0.0 停止回传判断;

            if (stop & c == 8 ) 
            {                                  
                    if (bt2[0] == bb[6] & bt2[1] == bb[7])
                    {
                        button1.BackColor = Color.Lime;
                        button1.Text = "未启动";
                        stop = false;
                        textBox1.AppendText("\r\n %q0.0已关闭");
                    }                                 
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        byte[] bb = { 0x01, 0x05, 0x00, 0x00,0xFF, 0x00 };
        bb1 =BitConverter.GetBytes( CRC16(bb)) ;
        List Ls = bb.ToList();
        Ls.Add(bb1[0]);
        Ls.Add(bb1[1]);
        bb = Ls.ToArray();
        serialPort1.Write(bb,0,bb.Length);
        start = true;

    }

    private void button2_Click(object sender, EventArgs e)
    {
        byte[] bb = { 0x01, 0x05, 0x00, 0x00, 0x00, 0x00 };
        bt2 = BitConverter.GetBytes(CRC16(bb));
        List Ls = bb.ToList();
        Ls.Add(bt2[0]);
        Ls.Add(bt2[1]);
        bb = Ls.ToArray();
        serialPort1.Write(bb, 0, bb.Length);
        stop = true;
    }

    private void button3_Click(object sender, EventArgs e)
    {
        byte[] bb = { 0x01, 0x03, 0x00, 0x00, 0x00, 0x02 };
        byte[] bb1 = BitConverter.GetBytes(CRC16(bb));
        List Ls = bb.ToList();
        Ls.Add(bb1[0]);
        Ls.Add(bb1[1]);
        bb = Ls.ToArray();
        serialPort1.Write(bb, 0, bb.Length);
        a = true;
    }
}

}

你可能感兴趣的:(c#,microsoft,开发语言)