需要引用这三个库:
实例:
//添加PLC控件
axActUtlType = new AxActUtlType();
Controls.Add(this.axActUtlType);
//打开PLC
string s = MitsubishiHelper.Open(ref axActUtlType, 1);
/// <summary>
/// 三菱
/// </summary>
public class MitsubishiHelper
{
public static AxActUtlType _axActUtlType = null;
#region 开启PLC
/// <summary>
/// 开启PLC
/// </summary>
/// <param name="axActUtlType"> AxActUtlType</param>
/// <param name="_StationNo"></param>
/// <param name="_Password">密码</param>
/// <returns></returns>
public static string Open(ref AxActUtlType axActUtlType, int _StationNo = 0, string _Password = "")
{
try
{
//((Form)form).Controls.Add(axActUtlType);
_axActUtlType = axActUtlType;
//int iStation = Convert.ToInt32(_StationNo);
axActUtlType.ActLogicalStationNumber = _StationNo;
_axActUtlType.ActPassword = _Password;
int rtn = _axActUtlType.Open();
if (rtn == 0)
{
return "PLC连接成功";
}
else
{
return "PLC连接失败";
}
}
catch (Exception )
{
return "PLC连接失败";
}
}
#endregion
#region 读取数据
/// <summary>
/// 读取数据
/// </summary>
/// <param name="szDevice"></param>
/// <param name="lSize"></param>
/// <returns></returns>
public static string ReadData(string szDevice, string lSize="2")
{
try
{
int num = Convert.ToInt32(lSize);
short[] arr = new short[num];
int rtn = _axActUtlType.ReadDeviceBlock2(szDevice, num, out arr[0]);
string s = "";
if (rtn == 0)
{
for (int i = arr.Length - 1; i >= 0; i--)
{
s += string.Format("{0:X4}", arr[i]);
}
return (Convert.ToInt32(s, 16).ToString());
}
else
{
return null;
}
}
catch (Exception )
{
return null;
}
}
#endregion
#region PLC高低位
/// <summary>
/// PLC高低位
/// </summary>
/// <param name="twoWord"></param>
/// <param name="highWord"></param>
/// <param name="lowWord"></param>
public static void hi_lo(string twoWord, out string highWord, out string lowWord)
{
if (twoWord.Contains("-"))
{
int low = int.Parse(twoWord) % 65536;
int high = -1;
lowWord = low.ToString();
highWord = high.ToString();
}
else
{
int low = int.Parse(twoWord) % 65536;
int high = int.Parse(twoWord) / 65536;
lowWord = low.ToString();
highWord = high.ToString();
}
}
#endregion
#region 写入
/// <summary>
/// 将数据写入PLC
/// </summary>
/// <param name="szDevice">地区</param>
/// <param name="strData">数据</param>
/// <returns></returns>
public static string WriteData(string szDevice, string[] strData) {
try
{
int num = strData.Length;
int[] arr = new int[num];
for (int i = 0; i < num; i++)
{
arr[i] = Convert.ToInt32(strData[i]);
}
// int rtn = _axActUtlType.WriteDeviceBlock2(szDevice, num, ref arr[0]);
int rtn = _axActUtlType.WriteDeviceRandom(szDevice, num, ref arr[0]);
if (rtn == 0)
{
//this.txtNum.Text = num.ToString();
return (num.ToString()) + "成功写入";
}
else
{
return "写入数据失败";
}
}
catch (Exception )
{
return "写入数据失败";
}
}
#endregion
#region 关闭
/// <summary>
/// 关闭
/// </summary>
/// <returns></returns>
public static bool Close()
{
return _axActUtlType.Close() == 0;
}
#endregion
#region 读bit
/// <summary>
/// 读bit
/// </summary>
/// <param name="szDevice">地区</param>
/// <returns></returns>
public static int GetBit(string szDevice)
{
int lpsData;
try
{
_axActUtlType.GetDevice(szDevice, out lpsData);
}
catch (Exception)
{
return 0;
}
return lpsData;
}
#endregion
#region 写bit
/// <summary>
/// 写bit
/// </summary>
/// <param name="szDevice">地区</param>
/// <param name="sData">数据</param>
/// <returns></returns>
public static bool SetBit(string szDevice, short sData)
{
try
{
_axActUtlType.SetDevice2(szDevice, sData);
}
catch (Exception)
{
return false;
}
return true;
}
#endregion
}