【PMAC】Chapter4:PMAC的C#开发

1、简介

这是个关于pmac上位机的简单编程,以下皆以1号电机为例,其他电机需根据实际修改变量

2、代码

2.1 dll的导入

第一种方法:首先要下载pmac的软件pewin32 pro,在vs中新建一个项目,在引用——添加引用——COM中查找PcommServer 1.0 type Library,确认引入。
在这里插入图片描述
第二种方法:官网下载

2.2 实例化
using PCOMMSERVERLib;

public partial class DEMO : Form
    {

        public static PCOMMSERVERLib.PmacDeviceClass PMAC;
        public bool selectPmacSuccess = false;
        public bool openPmacSuccess = false;
        public int pmacNumber;
   

        // 1. (打开程序后的动作)
        public DEMO()
        {
            InitializeComponent();
            PMAC = new PmacDeviceClass();
        }
}
2.3 建立通讯
        // 2. 建立通讯
        private void LinkPMAC()
        {     
            PMAC.SelectDevice(0,out pmacNumber,out selectPmacSuccess);
            if ( selectPmacSuccess )
            {
                PMAC.Open(pmacNumber, out openPmacSuccess);
                if (openPmacSuccess)
                {
                    string Text = "通讯成功";
                }
            }                    
        }       
2.4 控制电机运动
//正方向移动
private void motor1jog_positive()
 {
            string ans = null ;
            PMAC.GetResponse(pmacNumber, "#1j+", out ans);
}

//负方向移动
private void motor1jog_ negative()
 {
            string ans = null ;
            PMAC.GetResponse(pmacNumber, "#1j-", out ans);
}

//停止
private void motor1jog_stop()
 {
            string ans = null ;
            PMAC.GetResponse(pmacNumber, "#1j/", out ans);
}

//移动到特定位置
private void motor1jog_position(double p)
 {
            string ans = null ;
            command =  "#1j="+Convert.ToString(p);
            PMAC.GetResponse(pmacNumber, command, out ans);
}
2.5 读取变量
// 读取变量
        private void readPMAC(string readVariable)
        {
            string pmacAnswer = null;
            string readVariable = null;
            int pmacStatus = 0;
            PMAC.GetResponseEx(pmacNumber, readVariable, true, out pmacAnswer,out pmacStatus);
        }
2.6 下载文件
// 下载文件
        private void download_Click(object sender, EventArgs e)
        {
            string fileDirectory = null;
            bool downloadSuccess = false;
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "打开PMAC文件";
            ofd.Filter = "ASCII 文件 |  *.txt;*.pmc";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                fileDirectory = ofd.FileName;
                PMAC.Download(pmacNumber, fileDirectory, false, false, true, true, out downloadSuccess);
                if (downloadSuccess)
                {
                    string.Text = "下载成功";
                }
                else 
                {
                    string.Text = "下载失败";
                 }
               }
        }
2.7 读取电机编码盘位置信息
//读取电机编码盘位置信息
        public void double GetPosition()
        {
            if (PMAControl.openPmacSuccess)
            {
                int pmacStatus = 0;
                //M211指向编码盘信息存储位置
                PMAC.GetResponseEx(0, "M211“, true, out string pmacAnswer, out pmacStatus);
                string actual_position = PMAC.RequirePMAC(command);
                //string x_ScaleFactor = read_pmac("I108");
                double PDouble = Convert.ToSingle(actual_position);         
                position = PDouble / 96 / 32;
            }
        }
2.8 设置电机速度
public void SetSpeed(string Speed)
        {
            string jogspeed = string.Concat("I122=",Speed);
            PMAC.GetResponse(pmacNumber, jogspeed, out string ans);
        }
  • 2
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值