Files
LM-Middleware/HSI_SEVENOCEAN_EF1_CsTest/Program.cs
T

204 lines
6.5 KiB
C#

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using HSI_SEVENOCEAN_EF1_CsTest.HSI;
namespace HSI_SEVENOCEAN_EF1_CsTest
{
internal class Program
{
//private static Interface.PEventCallback _eventCallback;
private static void Main(string[] args)
{
//1 获取HSI Dll版本
uint major = Def.HSI_APIVersionMajor;
uint minjor = Def.HSI_APIVersionMinor;
var rStatus = Interface.GetInterfaceVersion(ref major, ref minjor);
Console.WriteLine("Interface.GetInterfaceVersion: {0}", rStatus);
Console.WriteLine("HSI.dll Version: {0}.{1}", major, minjor);
//2 HSI 初始化
var mainIntPtr = User32Api.GetCurrentWindowHandle();
rStatus = Interface.Startup(mainIntPtr, false);
Console.WriteLine("Interface.Startup: {0}", rStatus);
//3 获取机器类型
var machineVersion = Def.HSI_EF3Version;
rStatus = Interface.GetMachineInfo(ref machineVersion);
Console.WriteLine("Interface.GetMachineInfo: {0}", rStatus);
Console.WriteLine("HSI EF3 Version: {0}", machineVersion);
//4 设置回调,方便根据回调结果进行提醒
Interface.HsiCallback = Interface.EventCallback;
rStatus = Interface.SetEventCallback(Interface.HsiCallback);
Console.WriteLine("SetEventCallback:{0}", rStatus); //事件回调成功
//if (Motion.IsActive(true))
{
rStatus = Motion.Startup(true); //运动初始化,回家判断
Console.WriteLine("Motion.Startup:{0}", rStatus);
//5 获取EF3固件版本号,待测试
//var EF3Version = new byte[20];
//IntPtr intPtrEF3Version = Marshal.StringToHGlobalAnsi("");
//rStatus = Interface.MotionGetFirewareVerion(intPtrEF3Version);
//Console.WriteLine("Interface.MotionGetFirewareVerion: {0}", rStatus);
//Marshal.Copy(intPtrEF3Version, EF3Version, 0, EF3Version.Length);
//Console.WriteLine("EF3 FirewareVerion Version: {0}", Encoding.UTF8.GetString(EF3Version));
#region
//启动扫描
Motion.DCCScanStart();
//扫描中
Thread.Sleep(10000);
//扫描结束
Motion.DCCScanStop();
//获取扫描结果
var dataCache = Marshal.AllocHGlobal(1000);
var pointCount = 0;
var data = new byte[1000];
Motion.GetPositionXyzCache(dataCache, ref pointCount);
Console.WriteLine("pointCount = " + pointCount);
Marshal.Copy(dataCache, data, 0, pointCount);
//打印扫描点
for (var i = 0; i < pointCount; i++) Console.WriteLine("Hex:{0} {1:X}", i, data[i]);
#endregion
//6 是否回家
var bHomed = true;
rStatus = Motion.IsHomed(ref bHomed);
Console.WriteLine("Motion.IsHomed:{0}", rStatus);
var bexit = false;
var SpeedGear = 0.2;
var dPos = new double[3];
do
{
var info = Console.ReadKey();
double dTime = 0;
switch (info.Key)
{
case ConsoleKey.P:
dPos[0] += 10.0; //X轴
dPos[1] += 20.0; //Y轴
dPos[2] += 30.0; //Y轴
rStatus = Motion.SetPositionXyz(Def.HSI_MOTION_AXIS_ALL, dPos[0], dPos[1], dPos[2],
Def.HSI_MOTION_MOVE_TYPE.HSI_MOTION_MOVE_WAIT, 0.0);
Console.WriteLine("Motion.SetPositionXyz:{0:f4},{1:f4},{2:f4} {3}", dPos[0], dPos[1],
dPos[2],
rStatus);
break;
case ConsoleKey.Enter:
rStatus = Motion.GetPositionXyz(Def.HSI_MOTION_AXIS_ALL, ref dPos[0], ref dPos[1],
ref dPos[2], ref dTime);
Console.WriteLine("Motion.GetPositionXyz:{0}", rStatus);
Console.WriteLine("Motion.XYZ:{0:f4},{1:f4},{2:f4}", dPos[0], dPos[1], dPos[2]);
break;
case ConsoleKey.Escape:
bexit = true;
break;
case ConsoleKey.Spacebar:
rStatus = Motion.StopJog();
Console.WriteLine("Motion.StopJog:{0}", rStatus);
break;
// Z轴运动
case ConsoleKey.NumPad1:
case ConsoleKey.D1:
rStatus = Motion.Jog((uint)Def.HSI_MOTION_AXIS_TYPE.HSI_MOTION_AXIS_Z, SpeedGear);
Console.WriteLine("Motion.Jog(Z,{1}):{0}", rStatus, SpeedGear);
break;
case ConsoleKey.NumPad3:
case ConsoleKey.D3:
rStatus = Motion.Jog((uint)Def.HSI_MOTION_AXIS_TYPE.HSI_MOTION_AXIS_Z, -SpeedGear);
Console.WriteLine("Motion.Jog(Z,{1}):{0}", rStatus, -SpeedGear);
break;
//Y轴运动
case ConsoleKey.NumPad2:
case ConsoleKey.D2:
rStatus = Motion.Jog((uint)Def.HSI_MOTION_AXIS_TYPE.HSI_MOTION_AXIS_Y, -SpeedGear);
Console.WriteLine("Motion.Jog(Y,{1}):{0}", rStatus, -SpeedGear);
break;
case ConsoleKey.NumPad8:
case ConsoleKey.D8:
rStatus = Motion.Jog((uint)Def.HSI_MOTION_AXIS_TYPE.HSI_MOTION_AXIS_Y, SpeedGear);
Console.WriteLine("Motion.Jog(Y,{1}):{0}", rStatus, SpeedGear);
break;
// X轴运动
case ConsoleKey.NumPad4:
case ConsoleKey.D4:
rStatus = Motion.Jog((uint)Def.HSI_MOTION_AXIS_TYPE.HSI_MOTION_AXIS_X, -SpeedGear);
Console.WriteLine("Motion.Jog(X,{1}):{0}", rStatus, -SpeedGear);
break;
case ConsoleKey.NumPad6:
case ConsoleKey.D6:
rStatus = Motion.Jog((uint)Def.HSI_MOTION_AXIS_TYPE.HSI_MOTION_AXIS_X, SpeedGear);
Console.WriteLine("Motion.Jog(X,{1}):{0}", rStatus, SpeedGear);
break;
//加减速
case ConsoleKey.Add:
SpeedGear += 0.2;
if (SpeedGear > 1.1) SpeedGear = 1.1;
Console.WriteLine("Motion.SpeedGera:{0}", SpeedGear);
break;
case ConsoleKey.Subtract:
SpeedGear -= 0.2;
if (SpeedGear < 0.1) SpeedGear = 0.1;
Console.WriteLine("Motion.SpeedGera:{0}", SpeedGear);
break;
case ConsoleKey.NumPad5:
Motion.DCCScanStart();
break;
case ConsoleKey.NumPad7:
Motion.DCCScanStop();
break;
case ConsoleKey.NumPad9:
var dPos1 = new double[3];
dPos1[0] = 100;
Motion.DCCScanSetData(1, Def.HSI_SCAN_MOTION_TYPE.HSI_SCAN_MOTION_EQ_DIS, 7, dPos1);
break;
//case ConsoleKey.LeftArrow:
// break;
//case ConsoleKey.RightArrow:
// break;
//case ConsoleKey.DownArrow:
// break;
//case ConsoleKey.UpArrow:
// break;
//case ConsoleKey.PageUp:
// break;
//case ConsoleKey.PageDown:
// break;
default:
Console.WriteLine("Invalid");
break;
}
} while (!bexit);
rStatus = Motion.Shutdown();
Console.WriteLine("Motion.Startup:{0}", rStatus);
}
rStatus = Interface.Shutdown();
Console.WriteLine("Interface.Shutdown:{0}", rStatus);
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}