using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Motor
public enum SoftElemType
//AM600
ELEM_QX = 0, //QX 元件
ELEM_MW = 1, //MW 元件
ELEM_X = 2, //X 元件(对应 QX200~QX300)
ELEM_Y = 3, //Y 元件(对应 QX300~QX400)
//H3U
REGI_H3U_Y = 0x20, //Y 元件的定义
REGI_H3U_X = 0x21, //X 元件的定义
REGI_H3U_S = 0x22, //S 元件的定义
REGI_H3U_M = 0x23, //M 元件的定义
REGI_H3U_TB = 0x24, //T 位元件的定义
REGI_H3U_TW = 0x25, //T 字元件的定义
REGI_H3U_CB = 0x26, //C 位元件的定义
REGI_H3U_CW = 0x27, //C 字元件的定义
REGI_H3U_DW = 0x28, //D 字元件的定义
REGI_H3U_CW2 = 0x29, //C 双字元件的定义
REGI_H3U_SM = 0x2a, //SM
REGI_H3U_SD = 0x2b, //
REGI_H3U_R = 0x2c, //
//H5u
REGI_H5U_Y = 0x30, //Y 元件的定义
REGI_H5U_X = 0x31, //X 元件的定义
REGI_H5U_S = 0x32, //S 元件的定义
REGI_H5U_M = 0x33, //M 元件的定义
REGI_H5U_B = 0x34, //B 元件的定义
REGI_H5U_D = 0x35, //D 字元件的定义
REGI_H5U_R = 0x36, //R 字元件的定义
class Program
[DllImport("StandardModbusApi.dll", EntryPoint = "Init_ETH_String", CallingConvention =
CallingConvention.Cdecl)]
public static extern bool Init_ETH_String(string sIpAddr, int nNetId = 0, int IpPort = 502);
[DllImport("StandardModbusApi.dll", EntryPoint = "Exit_ETH", CallingConvention =
CallingConvention.Cdecl)]
public static extern bool Exit_ETH(int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Write_Soft_Elem", CallingConvention =
CallingConvention.Cdecl)]
public static extern int H3u_Write_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount,
byte[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem", CallingConvention =
CallingConvention.Cdecl)]
public static extern int H3u_Read_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount, byte[]
pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H3u_Read_Soft_Elem_Float", CallingConvention
= CallingConvention.Cdecl)]
public static extern int H3u_Read_Soft_Elem_Float(SoftElemType eType, int nStartAddr, int nCount,
float[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Write_Soft_Elem", CallingConvention =
CallingConvention.Cdecl)]
public static extern int H5u_Write_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount,
byte[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Read_Soft_Elem", CallingConvention =
CallingConvention.Cdecl)]
public static extern int H5u_Read_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount, byte[]
pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Read_Soft_Elem_Float", CallingConvention
= CallingConvention.Cdecl)]
public static extern int H5u_Read_Soft_Elem_Float(SoftElemType eType, int nStartAddr, int nCount,
float[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "H5u_Read_Device_Block", CallingConvention =
CallingConvention.Cdecl)]
public static extern int H5u_Read_Device_Block(SoftElemType eType, int nStartAddr, int nCount,
byte[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "Am600_Write_Soft_Elem", CallingConvention =
CallingConvention.Cdecl)]
public static extern int Am600_Write_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount,
byte[] pValue, int nNetId = 0);
[DllImport("StandardModbusApi.dll", EntryPoint = "Am600_Read_Soft_Elem", CallingConvention =
CallingConvention.Cdecl)]
public static extern int Am600_Read_Soft_Elem(SoftElemType eType, int nStartAddr, int nCount,
byte[] pValue, int nNetId = 0);
static int Main(string[] args)
string ip = args[1];
int nNetId = int.Parse(args[2]);
int Ipport = int.Parse(args[3]);
int sensor_addr = 200;
int updown_relay_addr = 202;
int[] position_sensor = { 1, 2, 4 };
string value_result = "";
string info_result = "NORMAL";
bool result = Init_ETH_String(ip, nNetId, Ipport);
if (!result)
{
value_result = "FAIL -1 CONNECT_ERROR";
Console.WriteLine(value_result);
//Console.WriteLine("failed to init.");
return 255;
if (args[0] == "CHECK_ROTATE")
value_result = "FAIL ";
string dir = args[4].Substring("DIR=".Length);
string rounds = args[5];
string time_out = args[6];
int rounds_num = int.Parse(rounds.Substring("ROUND=".Length));
int timeout = int.Parse(time_out.Substring("TIMEOUT=".Length));
int count_round = -1;
int durationStart = Environment.TickCount;
int updown_relay_value = -1;
int sensor_value = -1;
int first_sensor_value = -1;
int present_sensor_value = -1;
int dir_value;
int present_index = 0;
int next_index = 0;
bool next_position_flag = false;
if (dir == "CW") dir_value = 1;
else if (dir == "CCW") dir_value = -1;
while (Environment.TickCount - durationStart <= timeout)
if (count_round >= rounds_num) break;
if (next_position_flag)
next_index = present_index + dir_value;
if (next_index > 2) next_index = 0;
else if (next_index < 0) next_index = 2;
next_position_flag = false;
updown_relay_value = read_data_h3u_model(updown_relay_addr, nNetId);
if (updown_relay_value == 0)
info_result = " UNLOCK_DUT";
break;
else if (updown_relay_value == 1)
sensor_value = read_data_h3u_model(sensor_addr, nNetId);
if (sensor_value == 0) continue;
while (Environment.TickCount - durationStart <= timeout)
if (read_data_h3u_model(sensor_addr, nNetId) == 0) break;
if (first_sensor_value == -1)
first_sensor_value = sensor_value;
present_index = Math.Log(sensor_value,2);
next_position_flag = true;
count_round += 1;
continue;
if (sensor_value == position_sensor[present_index])
info_result = " WRONG_DIRECTION";
break;
else if (sensor_value == position_sensor[next_index])
next_position_flag = true;
present_index = next_index;
else break;
if (sensor_value == first_sensor_value) count_round += 1;
if (Environment.TickCount - durationStart +5 > timeout) info_result = " TIMEOUT";
if (count_round >= rounds_num) value_result = "PASS ";
value_result += count_round + info_result;
Console.WriteLine(value_result);
else if (args[0] == "DEBUG_MODE")
//writeCommand(100, 2);
int value;
while (true)
{
value = read_data_h3u_model(sensor_addr, nNetId);
Console.WriteLine(String.Format("(Status Sensor) nValue:{0},", result_value));
Thread.Sleep(5);
Exit_ETH(int.Parse(args[2]));
return 0;
static int read_data_h3u_model(int Addr,int nNetIdR = 0)
byte[] pValueR = new byte[2];
int nRet = H3u_Read_Soft_Elem(SoftElemType.REGI_H3U_DW, Addr, 1, pValueR, nNetIdR);
int nValue = BitConverter.ToInt16(pValueR, 0);
Debug.WriteLine(String.Format("H5u_Read_Soft_Elem:{0}", nValue));
//Console.WriteLine(String.Format("(Status Sensor) nValue:{0}, nRet:{1}", nValue,nRet));
return nValue;
//static int Main(string[] args)
//{
// bool result = Init_ETH_String(args[1], int.Parse(args[2]), int.Parse(args[3]));
// if (!result)
// {
// Console.WriteLine("failed to init.");
// return 255;
// }
// int sensor_addr = 200;
// int[] position_sensor = { 1, 2, 4 };
// if (args[0] == "ROTATE_CHECK_DIR")
// {
// int rounds_num = int.Parse(args[4]);
// int count_num = -1;
// result = checkStatus(200, 1, 5000);
// }
// else if (args[0] == "Z-UP")
// {
// writeCommand(100, 2);
// result = checkStatus(200, 2, 5000);
// }
// //if (args[0] == "Z-DOWN")
// //{
// // writeCommand(100, 1);
// // result = checkStatus(200, 1, 5000);
// //}
// //else if (args[0] == "Z-UP")
// //{
// // writeCommand(100, 2);
// // result = checkStatus(200, 2, 5000);
// //}
// //else if (args[0] == "CLAMP")
// //{
// // writeCommand(110, 1);
// // result = checkStatus(210, 1, 5000);
// //}
// //else if (args[0] == "RELEASE")
// //{
// // writeCommand(110, 2);
// // result = checkStatus(210, 2, 5000);
// //}
// //else if (args[0] == "MOVE-IN")
// //{
// // writeCommand(130, 1);
// // result = checkStatus(230, 1, 5000);
// //}
// //else if (args[0] == "MOVE-OUT")
// //{
// // writeCommand(130, 2);
// // result = checkStatus(230, 2, 5000);
// //}
// //else if (args[0] == "RELAY-ON")
// //{
// // writeCommand(150, 1);
// // result = checkStatus(250, 1, 5000);
// //}
// //else if (args[0] == "RELAY-OFF")
// //{
// // writeCommand(150, 2);
// // result = checkStatus(250, 2, 5000);
// //}
// //else if (args[0] == "RESET")
// //{
// // writeCommand(600, 1);
// // result = checkStatus(650, 1, 30000);
// //}
// //else if (args[0] == "SET-SPEED")
// //{
// // writeCommand(500, 1);
// // Thread.Sleep(500);
// // int v = int.Parse(args[4]);
// // writeCommand(350, v);
// // Thread.Sleep(500);
// // result = checkStatus(350, v, 5000);
// // writeCommand(500, 0);
// //}
// //else if (args[0] == "SET-ANGLE")
// //{
// // writeCommand(500, 1);
// // Thread.Sleep(500);
// // int v = int.Parse(args[4]);
// // writeCommand(450, v);
// // Thread.Sleep(500);
// // result = checkStatus(450, v, 30000);
// // writeCommand(500, 0);
// //}
// //else if (args[0] == "READ-SET-SPEED")
// //{
// // result = readRegistor2(350, 3000);
// //}
// //else if (args[0] == "READ-SET-ANGLE")
// //{
// // result = readRegistor2(450, 3000);
// //}
// //else if (args[0] == "READ-CUR-SPEED")
// //{
// // result = readRegistor2(300, 3000);
// //}
// //else if (args[0] == "READ-CUR-ANGLE")
// //{
// // result = readRegistor2(400, 3000);
// //}
// //else if (args[0] == "ROTATE-CHECK-POS")
// //{
// // writeCommand(120, 1);
// // result = checkStatus(220, 1, 30000);
// // writeCommand(120, 0);
// //}
// //else if (args[0] == "ROTATE")
// //{
// // writeCommand(120, 1);
// // //result = checkStatus(220, 1, 30000);
// // //writeCommand(120, 0);
// //}
// //else if (args[0] == "CHECK-ROTATE")
// //{
// // result = checkStatus(220, 1, 30000);
// // writeCommand(120, 0);
// //}
// //else if (args[0] == "ZERO")
// //{
// // Thread.Sleep(500);
// // writeCommand(120, 2);
// // result = checkStatus(220, 2, 30000);
// //}
// Exit_ETH(int.Parse(args[2]));
// if (result)
// Console.WriteLine("pass");
// else
// Console.WriteLine("fail");
// return 0;
//}
//static void writeCommand(int Addr, int value)
//{
// byte[] pValue = new byte[2];
// int nNetId = 0;
// pValue = BitConverter.GetBytes(value);
// int nRet = H5u_Write_Soft_Elem(SoftElemType.REGI_H5U_D, Addr, 2, pValue, nNetId);
// //Console.WriteLine(String.Format("(checkStatus) nRet:{0}",nRet));
//}
//static bool checkStatus_h3u_model(int Addr, int value, int Timeout = 5000)
//{
// byte[] pValueR = new byte[2];
// int nNetIdR = 0;
// int durationStart = Environment.TickCount;
// while (Environment.TickCount - durationStart <= Timeout)
// {
// int nRet = H5u_Read_Soft_Elem(SoftElemType.REGI_H5U_D, Addr, 1, pValueR, nNetIdR);
// int nValue = BitConverter.ToInt16(pValueR, 0);
// Debug.WriteLine(String.Format("H5u_Read_Soft_Elem:{0}", nValue));
// //Console.WriteLine(String.Format("(checkStatus) nValue:{0}, nRet:{1}", nValue,nRet));
// if (nValue == value)
// return true;
// }
// return false;
//}
//static bool checkStatus(int Addr, int value, int Timeout = 5000)
//{
// byte[] pValueR = new byte[2];
// int nNetIdR = 0;
// int durationStart = Environment.TickCount;
// while (Environment.TickCount - durationStart <= Timeout)
// {
// int nRet = H5u_Read_Soft_Elem(SoftElemType.REGI_H5U_D, Addr, 1, pValueR, nNetIdR);
// int nValue = BitConverter.ToInt16(pValueR, 0);
// Debug.WriteLine(String.Format("H5u_Read_Soft_Elem:{0}", nValue));
// //Console.WriteLine(String.Format("(checkStatus) nValue:{0}, nRet:{1}", nValue,nRet));
// if (nValue == value)
// return true;
// }
// return false;
//}
//static bool readRegistor(int Addr, int Timeout = 5000)
//{
// byte[] pValueR = new byte[2];
// int nNetIdR = 0;
// int nRet = 0;
// int durationStart = Environment.TickCount;
// while (Environment.TickCount - durationStart <= Timeout)
// {
// nRet = H5u_Read_Device_Block(SoftElemType.REGI_H5U_D, Addr, 2, pValueR, nNetIdR);
// int readData = BitConverter.ToInt16(pValueR, 0);
// Console.WriteLine(String.Format("(readRegistor) readData:{0}, nRet:{1}", readData, nRet));
// if (nRet == 1)
// return true;
// }
// return false;
//}
//static bool readRegistor2(int Addr, int Timeout = 5000)
//{
// byte[] pValueR = new byte[2];
// int nNetIdR = 0;
// int durationStart = Environment.TickCount;
// while (Environment.TickCount - durationStart <= Timeout)
// {
// int nRet = H5u_Read_Soft_Elem(SoftElemType.REGI_H5U_D, Addr, 1, pValueR, nNetIdR);
// int nValue = BitConverter.ToInt16(pValueR, 0);
// Debug.WriteLine(String.Format("H5u_Read_Soft_Elem:{0}", nValue));
// Console.WriteLine(String.Format("(checkStatus) nValue:{0}, nRet:{1}", nValue,nRet));
// if (nRet == 1)
// return true;
// }
// return false;
//}