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);
}