基本信息
源码名称:基于TCP网络通信控制PLC的两个轴
源码大小:0.22M
文件格式:.zip
开发语言:C#
更新时间:2022-04-28
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

上位机基于TCP网络通信控制PLC的两个轴,对电机电动正反转、回原点、定位、速度修改等控制,以及反馈下位机输入信号、电机状态。



(部分代码,完整见文件)

private void AutoSendTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Invoke(new Action(() =>
            {
                //SendASCII("DEBUG:GET:ALL?");

                //PLC返回所有状态信息的字符串组成为MSG:....,
                //解析:IO状态、前平台编码器位置、后平台编码器位置、前平台PLC位置、后平台PLC位置、前平台速度、后平台速度、前平台目标位、后平台目标位
                if (Msg.Contains("MSG:"))
                {
                    string[] PlcAllStatus = new string[24];
                    //.Substring(截取位置,截取个数)
                    PlcAllStatus[0] = Msg.Substring(4, 4);//IO状态
                    PlcAllStatus[1] = Msg.Substring(8, 4);//前平台编码器位置
                    PlcAllStatus[2] = Msg.Substring(12, 4);//后平台编码器位置
                    PlcAllStatus[3] = Msg.Substring(16, 4);//前平台PLC位置
                    PlcAllStatus[4] = Msg.Substring(20, 4);//后平台PLC位置
                    PlcAllStatus[5] = Msg.Substring(24, 4);//前平台速度
                    PlcAllStatus[6] = Msg.Substring(28, 4);//后平台速度
                    PlcAllStatus[7] = Msg.Substring(32, 4);//前平台目标位
                    PlcAllStatus[8] = Msg.Substring(36, 4);//后平台目标位

                    //PlcAllStatus[0] 为hex,转为16位的bin,再解析每个IO
                    //string PlcIoStatus = Convert.ToString(Convert.ToInt32(PlcAllStatus[0], 16),2);//IO状态
                    int hexStringToHexValue = Int32.Parse(PlcAllStatus[0], System.Globalization.NumberStyles.HexNumber);
                    string PlcIoStatus = Convert.ToString(hexStringToHexValue, 2).PadLeft(16, '0');

                    //位排列时左高右低,字符串操作索引是左低右高,故反向排序:
                    PlcIoStatus = string.Join("", PlcIoStatus.Reverse());

                    bool[] PlcIo = new bool[16];


                    //把16位的bin解析到IO数组中
                    for (int i = 0; i < 16; i  )
                    {
                        if (PlcIoStatus.Substring(i, 1) == "1")
                        {
                            PlcIo[i] = true;
                        }
                        else
                        {
                            PlcIo[i] = false;
                        }
                    }

                    //IO状态刷新                    
                    UpdateIoStatus(PlcIo[0], lblCh0NegativeLimit, "感应到", "未感应");//前平台负限位                    
                    UpdateIoStatus(PlcIo[1], lblCh0PositiveLimit, "感应到", "未感应");//前平台正限位
                    UpdateIoStatus(PlcIo[2], lblCh0Origin, "感应到", "未感应");//前平台原点
                    UpdateIoStatus(PlcIo[3], lblCh1NegativeLimit, "感应到", "未感应");//后平台负限位
                    UpdateIoStatus(PlcIo[0], lblCh1PositiveLimit, "感应到", "未感应");//后平台正限位(与前平台负限位共用传感器)
                    UpdateIoStatus(PlcIo[4], lblCh1Origin, "感应到", "未感应");//后平台原点
                    UpdateIoStatus(PlcIo[5], lblCh0Status, "运动中", "空闲中");//系统CH0_BUSY
                    UpdateIoStatus(PlcIo[6], lblCh1Status, "运动中", "空闲中");//系统CH1_BUSY

                    //接收到的hexString,单位为0.1mm。故转为十进制整数--》缩小十倍转mm--》转浮点数--》转十进制string
                    this.txtCh0CurrentPositon.Text = (Convert.ToSingle(Convert.ToInt32(PlcAllStatus[3], 16)) / 10).ToString();//前平台PLC位置
                    this.txtCh1CurrentPositon.Text = (Convert.ToSingle(Convert.ToInt32(PlcAllStatus[4], 16)) / 10).ToString();//后平台PLC位置

                    //是否获取过速度当前位置
                    if (!IsGetSpeedAndCurrentPosition)
                    {
                        this.txtCh0Speed.Text = (Convert.ToSingle(Convert.ToInt32(PlcAllStatus[5], 16)) / 10).ToString();//前平台速度
                        this.txtCh1Speed.Text = (Convert.ToSingle(Convert.ToInt32(PlcAllStatus[6], 16)) / 10).ToString();//后平台速度
                        this.txtCh0TargetPosition.Text = (Convert.ToSingle(Convert.ToInt32(PlcAllStatus[7], 16)) / 10).ToString();//前平台目标位
                        this.txtCh1TargetPosition.Text = (Convert.ToSingle(Convert.ToInt32(PlcAllStatus[8], 16)) / 10).ToString();//后平台目标位
                        IsGetSpeedAndCurrentPosition = true;
                    }

                }
            }));
        }