幽浮404

基于PLC编程口的通信
基于三菱PLC RS422编程口的通信程序Demo
扫描右侧二维码阅读全文
01
2017/03

基于PLC编程口的通信

基于三菱PLC RS422编程口的通信程序Demo

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

namespace PLC通信
{
    public partial class Form1 : Form
    {
        SerialPort sp = new SerialPort("COM18", 9600, Parity.Even, 7, StopBits.One);
        List<byte> l_b = new List<byte>();
        List<char> l_c = new List<char>();
        DataTable dtm = new DataTable();
        int i_c = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //string str = "123456";
            //byte[] bytes = new byte[str.Length / 2];
            //for (int i = 0; i < bytes.Length; i++)
            //{
            //    bytes[i] = Convert.ToByte(str.Substring(i * 2, 2), 16);
            //}
            dtm.Columns.Add("寄存器");
            dtm.Columns.Add("数据");
            gridControl1.DataSource = dtm;
        }

        

        private byte[] fun_转化(string s)
        {
            List<byte> l_b_zh = new List<byte>();
            int ic1 = 3;
            char icc1 = Convert.ToChar(ic1);
            string ru = "";
            MatchCollection vMatchs = Regex.Matches(s, @"(\d+)");
            int i = int.Parse(vMatchs[0].Value);
            string resul = Convert.ToString(i * 2 + 4096, 16).ToUpper();
            ru = ru + "0";
            ru = ru + resul;
            ru = ru + "0";
            ru = ru + "4";
            ru = ru + icc1.ToString();
            int sum = 0;
            byte[] arrByte = System.Text.Encoding.Default.GetBytes(ru);
            for (int mm = 0; mm < arrByte.Count(); mm++)
            {
                sum += arrByte[mm];
            }
            string jyh = Convert.ToString(sum, 16).ToUpper();
            string jyh_rus = jyh.Substring(jyh.Length - 2);
            byte sumh = Convert.ToByte(jyh_rus[0]);
            byte suait = Convert.ToByte(jyh_rus[1]);
            l_b_zh.Add(2);
            for (int mm = 0; mm < arrByte.Count(); mm++)
            {
                l_b_zh.Add(arrByte[mm]);
            }
            l_b_zh.Add(sumh);
            l_b_zh.Add(suait);
            byte[] byte_rul = new byte[l_b_zh.Count()];
            int ip = 0;
            foreach (byte b in l_b_zh)
            {
                byte_rul[ip] = b;
                ip++;
            }
            return byte_rul;
        }

        void fun_发送(byte[] byt)
        {
            

            if (!sp.IsOpen)
            {
                sp.Open();
            }
            sp.Write(byt, 0, byt.Count());
        
        }

        private string fun_接收()
        {
            l_b.Clear();
            while (sp.BytesToRead > 0)
            {
                int d = sp.ReadByte();
                l_b.Add(Convert.ToByte(d));
            }
            byte[] ss = new byte[l_b.Count];
            int cc1 = 0;
            foreach (byte b in l_b)
            {
                ss[cc1] = b;
                cc1++;
            }
            string s1 = Encoding.ASCII.GetString(ss);
            int zj1 = 0;
            if (s1.Length > 0)
            {
                string s2 = s1.Substring(1, 8);
                string[] result = Regex.Split(s2, "(?<=\\G.{2})");
                string s3 = result[3] + result[2] + result[1] + result[0];
                zj1 = Convert.ToInt32(s3, 16);
            }   
            return zj1.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        //public static string ByteToString(byte[] InBytes)
        //{
        //    string StringOut = "";
        //    foreach (byte InByte in InBytes)
        //    {
        //        StringOut = StringOut + String.Format("{0:X2} ", InByte);
        //    }
        //    return StringOut;
        //}   

        public static byte[] StringToByte(string InString)
        {
            string[] ByteStrings;
            ByteStrings = InString.Split(" ".ToCharArray());
            byte[] ByteOut;
            ByteOut = new byte[ByteStrings.Length - 1];
            for (int i = 0; i < ByteStrings.Length - 1; i++)
            {
                ByteOut[i] = Convert.ToByte(("0x" + ByteStrings[i]));
            }
            return ByteOut;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Stop();
            byte[] byt1 = fun_转化(textBox1.Text);
            fun_发送(byt1);
            string str = fun_接收();
            textBox2.Text = str;
            timer1.Start();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox3.Text != "")
            {
                dtm.Rows.Add(textBox3.Text, "");
                textBox3.Text = "";
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            timer2.Stop();
            foreach (DataRow dr in dtm.Rows)
            {
                byte[] byt1 = fun_转化(dr["寄存器"].ToString());
                fun_发送(byt1);
                string s_rul = fun_接收();
                dr["数据"] = s_rul;
            }





            timer2.Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            timer2.Enabled=true;
        }   
       
    }
}

界面
简单实现了一下上位机发送指令读取PLC寄存器的值,PLC收到指令后返回寄存器的值。

Last modification:September 2nd, 2019 at 02:01 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment