using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MOXA_CSharp_MXIO;
namespace MOXA_1
{
class Program
{
public const UInt16 Port = 502;//502; //Modbus TCP port
//----------------------------------------------------------------------------------
public static void CheckErr(int iRet, string szFunctionName)
{
string szErrMsg = "MXIO_OK";
if (iRet != MXIO_CS.MXIO_OK)
{
switch (iRet)
{
case MXIO_CS.ILLEGAL_FUNCTION:
szErrMsg = "ILLEGAL_FUNCTION";
break;
case MXIO_CS.ILLEGAL_DATA_ADDRESS:
szErrMsg = "ILLEGAL_DATA_ADDRESS";
break;
case MXIO_CS.ILLEGAL_DATA_VALUE:
szErrMsg = "ILLEGAL_DATA_VALUE";
break;
case MXIO_CS.SLAVE_DEVICE_FAILURE:
szErrMsg = "SLAVE_DEVICE_FAILURE";
break;
case MXIO_CS.SLAVE_DEVICE_BUSY:
szErrMsg = "SLAVE_DEVICE_BUSY";
break;
case MXIO_CS.EIO_TIME_OUT:
szErrMsg = "EIO_TIME_OUT";
break;
case MXIO_CS.EIO_INIT_SOCKETS_FAIL:
szErrMsg = "EIO_INIT_SOCKETS_FAIL";
break;
case MXIO_CS.EIO_CREATING_SOCKET_ERROR:
szErrMsg = "EIO_CREATING_SOCKET_ERROR";
break;
case MXIO_CS.EIO_RESPONSE_BAD:
szErrMsg = "EIO_RESPONSE_BAD";
break;
case MXIO_CS.EIO_SOCKET_DISCONNECT:
szErrMsg = "EIO_SOCKET_DISCONNECT";
break;
case MXIO_CS.PROTOCOL_TYPE_ERROR:
szErrMsg = "PROTOCOL_TYPE_ERROR";
break;
case MXIO_CS.SIO_OPEN_FAIL:
szErrMsg = "SIO_OPEN_FAIL";
break;
case MXIO_CS.SIO_TIME_OUT:
szErrMsg = "SIO_TIME_OUT";
break;
case MXIO_CS.SIO_CLOSE_FAIL:
szErrMsg = "SIO_CLOSE_FAIL";
break;
case MXIO_CS.SIO_PURGE_COMM_FAIL:
szErrMsg = "SIO_PURGE_COMM_FAIL";
break;
case MXIO_CS.SIO_FLUSH_FILE_BUFFERS_FAIL:
szErrMsg = "SIO_FLUSH_FILE_BUFFERS_FAIL";
break;
case MXIO_CS.SIO_GET_COMM_STATE_FAIL:
szErrMsg = "SIO_GET_COMM_STATE_FAIL";
break;
case MXIO_CS.SIO_SET_COMM_STATE_FAIL:
szErrMsg = "SIO_SET_COMM_STATE_FAIL";
break;
case MXIO_CS.SIO_SETUP_COMM_FAIL:
szErrMsg = "SIO_SETUP_COMM_FAIL";
break;
case MXIO_CS.SIO_SET_COMM_TIME_OUT_FAIL:
szErrMsg = "SIO_SET_COMM_TIME_OUT_FAIL";
break;
case MXIO_CS.SIO_CLEAR_COMM_FAIL:
szErrMsg = "SIO_CLEAR_COMM_FAIL";
break;
case MXIO_CS.SIO_RESPONSE_BAD:
szErrMsg = "SIO_RESPONSE_BAD";
break;
case MXIO_CS.SIO_TRANSMISSION_MODE_ERROR:
szErrMsg = "SIO_TRANSMISSION_MODE_ERROR";
break;
case MXIO_CS.PRODUCT_NOT_SUPPORT:
szErrMsg = "PRODUCT_NOT_SUPPORT";
break;
case MXIO_CS.HANDLE_ERROR:
szErrMsg = "HANDLE_ERROR";
break;
case MXIO_CS.SLOT_OUT_OF_RANGE:
szErrMsg = "SLOT_OUT_OF_RANGE";
break;
case MXIO_CS.CHANNEL_OUT_OF_RANGE:
szErrMsg = "CHANNEL_OUT_OF_RANGE";
break;
case MXIO_CS.COIL_TYPE_ERROR:
szErrMsg = "COIL_TYPE_ERROR";
break;
case MXIO_CS.REGISTER_TYPE_ERROR:
szErrMsg = "REGISTER_TYPE_ERROR";
break;
case MXIO_CS.FUNCTION_NOT_SUPPORT:
szErrMsg = "FUNCTION_NOT_SUPPORT";
break;
case MXIO_CS.OUTPUT_VALUE_OUT_OF_RANGE:
szErrMsg = "OUTPUT_VALUE_OUT_OF_RANGE";
break;
case MXIO_CS.INPUT_VALUE_OUT_OF_RANGE:
szErrMsg = "INPUT_VALUE_OUT_OF_RANGE";
break;
}
Console.WriteLine("Function \"{0}\" execution Fail. Error Message : {1}\n", szFunctionName, szErrMsg);
if (iRet == MXIO_CS.EIO_TIME_OUT || iRet == MXIO_CS.HANDLE_ERROR)
{
//To terminates use of the socket
MXIO_CS.MXEIO_Exit();
Console.WriteLine("Press any key to close application\r\n");
Console.ReadLine();
Environment.Exit(1);
}
}
}
//----------------------------------------------------------------------------------
static void Main(string[] args)
{
string IPAddr = "192.168.0.213";
string szPassword = "";
UInt32 Timeout = 5000; //超时时间
Int32[] hConnection = new Int32[1];
int ret = MXIO_CS.MXIO_GetDllVersion();
Console.WriteLine("MXIO_GetDllVersion:{0}.{1}.{2}.{3}", (ret >> 12) & 0xF, (ret >> 8) & 0xF, (ret >> 4) & 0xF, (ret) & 0xF);
//初始化MXIO库
if (MXIO_CS.MXEIO_Init() != MXIO_CS.MXIO_OK)
{
Console.WriteLine("程序初始化失败!");
return;
}
//连接设备
ret = MXIO_CS.MXEIO_E1K_Connect(System.Text.Encoding.UTF8.GetBytes(IPAddr),Port,Timeout,hConnection,System.Text.Encoding.UTF8.GetBytes(szPassword));
//检查连接
byte[] bytCheckStatus = new byte[1];
ret = MXIO_CS.MXEIO_CheckConnection(hConnection[0], Timeout, bytCheckStatus);
CheckErr(ret, "MXEIO_E1K_Connect");
if (ret == MXIO_CS.MXIO_OK)
{
switch (bytCheckStatus[0])
{
case MXIO_CS.CHECK_CONNECTION_OK:
Console.WriteLine("MXEIO_CheckConnection: Check connection ok => {0}", bytCheckStatus[0]);
break;
case MXIO_CS.CHECK_CONNECTION_FAIL:
Console.WriteLine("MXEIO_CheckConnection: Check connection fail => {0}", bytCheckStatus[0]);
break;
case MXIO_CS.CHECK_CONNECTION_TIME_OUT:
Console.WriteLine("MXEIO_CheckConnection: Check connection time out => {0}", bytCheckStatus[0]);
break;
default:
Console.WriteLine("MXEIO_CheckConnection: Check connection status unknown => {0}", bytCheckStatus[0]);
break;
}
}
//读取数据
UInt16[] usRawData = new UInt16[5];
ret = MXIO_CS.E1K_AI_ReadRaws(hConnection[0],0,5,usRawData);
CheckErr(ret, "E1K_AI_ReadMaxs");
if (ret == MXIO_CS.MXIO_OK)
Console.WriteLine("E1K_AI_GetRanges AI Range Values:\r\n[0]={0}\r\n[1]={1}\r\n[2]={2}\r\n[3]={3}", usRawData[0], usRawData[1], usRawData[2], usRawData[3]);
ret = MXIO_CS.MXEIO_Disconnect(hConnection[0]);
CheckErr(ret, "MXEIO_Disconnect");
if (ret == MXIO_CS.MXIO_OK)
Console.WriteLine("MXEIO_Disconnect return {0}", ret);
MXIO_CS.MXEIO_Exit();
Console.ReadLine();
}
}
}
TEST
最新推荐文章于 2024-02-01 15:45:25 发布