树莓派Linux跨平台免授权CNC数控数据采集协议,TCP协议包,支持(fanuc、三菱、西门子、海德汉、马扎克、兄弟、哈斯、广数、凯恩帝KND、华中)数控跨平台,支持网关Linux

市面上的数控基本都支持了跨平台通讯,下面以三菱为列讲解跨平台协议如何通讯,无需任何DLL,适配任何开发语言,纯Socket通讯

先上采集图 

 

 代码如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 三菱TCP
{
    public partial class Form1 : Form
    {
        public static Socket TcpSocket;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
         

        }

        private void btnVer_Click(object sender, EventArgs e)
        {
           
        }
        //TcpSocket.Send(SendCommand.伺服名称);


        //    byte[] Data = ReceiveData();



        //byte[] Data2 = new byte[] { 0x10 };



        //string s = Encoding.Default.GetString(Data.Skip(28).Take(1).ToArray()).Replace("\0", string.Empty);

        public   byte[] ReceiveData()
        {
            byte[] b = new byte[1024];
            int n = TcpSocket.Receive(b);
            byte[] DATA = b.Take(n).ToArray();

            textBox1.Text = BitConverter.ToString(DATA).Replace("-","");;
            return DATA;






        }
     
        public string timeconvert(string time)
        {
            time= time.PadLeft(6,'0');

            if (time.Length > 6)
            {

                return time.Substring(0, time.Length-4)+":" + time.Substring(time.Length - 4, 2) + ":" + time.Substring(time.Length - 6, 2);
            }
            else
            {
                return time.Substring(0, 2) + ":" + time.Substring(2, 2) + ":" + time.Substring(4, 2);
            }


        }
      
     
        private void button12_Click(object sender, EventArgs e)
        {
            TcpSocket.Send(SendCommand.程序内容);
            byte[] Data = ReceiveData();
            程序内容.Text = Encoding.Default.GetString(Data.Skip(22).ToArray()).Replace("\0", string.Empty);

        }

        private void button13_Click(object sender, EventArgs e)
        {
            TcpSocket.Send(SendCommand.当前刀具号);
            byte[] Data = ReceiveData();
            int time = BitConverter.ToInt32(Data.Skip(22).Take(4).ToArray(), 0);
            当前刀具号.Text = time.ToString();
        }

        private void button14_Click(object sender, EventArgs e)
        {
            TcpSocket.Send(SendCommand.主轴设定);
            byte[] Data = ReceiveData();
            int time = BitConverter.ToInt32(Data.Skip(22).Take(4).ToArray(), 0);
            主轴转速设定.Text = time.ToString();
        
        }

        private void button15_Click(object sender, EventArgs e)
        {
            TcpSocket.Send(SendCommand.进给速度设定);
            byte[] Data = ReceiveData();
            
            进给速度设定.Text = BitConverter.ToDouble(Data.Skip(22).Take(8).ToArray(), 0).ToString();

        }

        private void button16_Click(object sender, EventArgs e)
        {
            TcpSocket.Send(SendCommand.进给速度实际值);
            byte[] Data = ReceiveData();
            进给速度实际值.Text = BitConverter.ToDouble(Data.Skip(44).Take(8).ToArray(), 0).ToString(); ;
        }

    

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                TcpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                TcpSocket.ReceiveTimeout = 1000;
                TcpSocket.Connect(txt_ip.Text, int.Parse(txt_port.Text));
                if (TcpSocket.Connected)
                {
                    MessageBox.Show("ok");
                }

            }
            catch (Exception ex)
            {

            }
        }

        private void button19_Click(object sender, EventArgs e)
        {
           
            TcpSocket.Send(SendCommand.主轴倍率);
            byte[] Data = ReceiveData();
  
            主轴倍率.Text = Data.Last().ToString();

        }

        private void button20_Click(object sender, EventArgs e)
        {
            TcpSocket.Send(SendCommand.进给倍率);
            byte[] Data = ReceiveData();

            进给倍率.Text = Data.Last().ToString();
        }

        private void button21_Click(object sender, EventArgs e)
        {

            TcpSocket.Send(SendCommand.CNC类型);
            byte[] Data = ReceiveData();
            CNC类型.Text = Encoding.Default.GetString(Data.Skip(22).Take(22).ToArray()).Replace("\0", string.Empty);
             

        }

        private void button22_Click(object sender, EventArgs e)
        {
            TcpSocket.Send(SendCommand.加工件数);
            byte[] Data = ReceiveData();
            int time = BitConverter.ToInt32(Data.Skip(22).Take(4).ToArray(), 0);
            加工件数.Text = time.ToString();
            
        }

       

        private void button23_Click(object sender, EventArgs e)
        {
            增益.Text = 读取伺服监控(0x01);
        }

        private void button24_Click(object sender, EventArgs e)
        {
            跟踪延迟.Text = 读取伺服监控(0x02);
        }

      

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (Control c in this.Controls)
            {
            
                Control c1 = c as Button;
                if (c1 != null)
                {
                    if (c1.Name != "button1"&& c1.Name != "button25")
                    {
                        ((Button)c1).PerformClick();

                    }
              
                }
            }
        }

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

      
    }
}

 

posted @ 2023-07-10 11:12  专业CNC数据采集  阅读(302)  评论(0编辑  收藏  举报