using System;
namespace CL.HCPLC
{
/// <summary>
/// 汇川PLC
/// </summary>
public class HCPLCHelp
{
/// <summary>
/// PLC 链接状态
/// </summary>
public bool m_bConnected = false;
///工位ID
public int nId = 0;
/// <summary>
/// 读取PLC
/// </summary>
/// <param name=”nStartAddr”>起初地址</param>
/// <param name=”nCount”>读取数量</param>
/// <param name=”nNetId”>链接ID</param>
/// <param name=”nREGI”>元器件地址</param>
/// <param name=”nPLCNo”>PLC 型号</param>
/// <param name=”nDataType”>读取PLC 数据类型</param>
/// <returns></returns>
public string Read(int nStartAddr, int nCount, int nNetId, REGI nREGI, PLCNo nPLCNo, DataType nDataType)
{
//缓冲区。读取整型字元件则一个元件占两个byte,位元件则占一个byte。例如读取D元件500个,则缓冲区大小为1000以上
byte[] pBuf = new byte[16000];
//int nStartAddr = Convert.ToInt32(txtStartAddr2.Text);
//int nCount = Convert.ToInt32(txtCount2.Text);
bool bIsWord = false;//是否字元件
SoftElemType ElemType = SoftElemType.REGI_H5U_Y;
if (nREGI == REGI.Y)
{
switch (nPLCNo)
{
case PLCNo.REGI_H3U:
ElemType = SoftElemType.REGI_H3U_Y;
break;
case PLCNo.REGI_H5U:
ElemType = SoftElemType.REGI_H5U_Y;
break;
}
}
else if (nREGI == REGI.X)
{
switch (nPLCNo)
{
case PLCNo.REGI_H3U:
ElemType = SoftElemType.REGI_H3U_X;
break;
case PLCNo.REGI_H5U:
ElemType = SoftElemType.REGI_H5U_X;
break;
}
}
else if (nREGI == REGI.S)
{
switch (nPLCNo)
{
case PLCNo.REGI_H3U:
ElemType = SoftElemType.REGI_H3U_S;
break;
case PLCNo.REGI_H5U:
ElemType = SoftElemType.REGI_H5U_S;
break;
}
}
else if (nREGI == REGI.M)
{
switch (nPLCNo)
{
case PLCNo.REGI_H3U:
ElemType = SoftElemType.REGI_H3U_M;
break;
case PLCNo.REGI_H5U:
ElemType = SoftElemType.REGI_H5U_M;
break;
}
}
else if (nREGI == REGI.B)
{
ElemType = SoftElemType.REGI_H5U_B;
}
else if (nREGI == REGI.D)
{
bIsWord = true;
switch (nPLCNo)
{
case PLCNo.REGI_H3U:
ElemType = SoftElemType.REGI_H3U_DW;
break;
case PLCNo.REGI_H5U:
ElemType = SoftElemType.REGI_H5U_D;
break;
}
}
else if (nREGI == REGI.R)
{
bIsWord = true;
switch (nPLCNo)
{
case PLCNo.REGI_H3U:
ElemType = SoftElemType.REGI_H3U_R;
break;
case PLCNo.REGI_H5U:
ElemType = SoftElemType.REGI_H5U_R;
break;
}
}
int nRet = 0;
switch (nPLCNo)
{
case PLCNo.REGI_H3U:
nRet = HCPLCApi.H3u_Read_Soft_Elem(ElemType, nStartAddr, nCount, pBuf, nNetId);
break;
case PLCNo.REGI_H5U:
nRet = HCPLCApi.H5u_Read_Soft_Elem(ElemType, nStartAddr, nCount, pBuf, nNetId);
break;
}
if (nRet != 1)
{
//txtMsg.Text = txtMsg.Text + DateTime.Now.ToString() + “:ErrorCode ” + nRet.ToString() + “\r\n”;
//MessageBox.Show(nRet.ToString());
//return “PC “+nRet;
return “”;
}
string strData = “”;
///DataType = cmbDataType2.SelectedIndex;
if (nDataType == DataType.Int32 || nDataType == DataType.Float)
{
nCount = nCount / 2;
}
for (int i = 0; i < nCount; i++)
{
if (bIsWord)
{
if (nDataType == DataType.Int16)//16位整形
{
byte[] databuf = new byte[2] { 0, 0 };
databuf[0] = pBuf[i * 2];
databuf[1] = pBuf[i * 2 + 1];
int iTemp = BitConverter.ToInt16(databuf, 0);
strData = strData + iTemp.ToString() + ” “;
continue;
}
else if (nDataType == DataType.Int32)//读取32位整形
{
byte[] databuf = new byte[4] { 0, 0, 0, 0 };
databuf[0] = pBuf[i * 4];
databuf[1] = pBuf[i * 4 + 1];
databuf[2] = pBuf[i * 4 + 2];
databuf[3] = pBuf[i * 4 + 3];
int iTemp = BitConverter.ToInt32(databuf, 0);
strData = strData + iTemp.ToString() + ” “;
continue;
}
else if (nDataType == DataType.Float)//读取浮点型
{
byte[] databuf = new byte[4] { 0, 0, 0, 0 };
databuf[0] = pBuf[i * 4];
databuf[1] = pBuf[i * 4 + 1];
databuf[2] = pBuf[i * 4 + 2];
databuf[3] = pBuf[i * 4 + 3];
float fTemp = BitConverter.ToSingle(databuf, 0);
strData = strData + fTemp.ToString() + ” “;
continue;
}
}
else
{
int nVal = 0;
nVal = pBuf[i];
strData = strData + nVal.ToString() + ” “;
}
}
return strData;
//txtData2.Text = strData;
//txtMsg2.AppendText(DateTime.Now.ToString() + “读取成功!\r\n”);
//txtMsg2.ScrollToCaret();
}
public bool Write(int nStartAddr, int nCount, int nNetId, REGI nREGI, PLCNo nPLCNo, DataType nDataType, string[] arr)
{
byte[] pBuf = new byte[16000];
//int nStartAddr = Convert.ToInt32(txtStartAddr2.Text);
//int nCount = Convert.ToInt32(txtCount2.Text);
bool bIsWord = false;//是否字元件
SoftElemType ElemType = SoftElemType.REGI_H5U_Y;
if (nREGI == REGI.Y)
{
switch (nPLCNo)
{
case PLCNo.REGI_H3U:
ElemType = SoftElemType.REGI_H3U_Y;
break;
case PLCNo.REGI_H5U:
ElemType = SoftElemType.REGI_H5U_Y;
break;
}
}
else if (nREGI == REGI.X)
{
switch (nPLCNo)
{
case PLCNo.REGI_H3U:
ElemType = SoftElemType.REGI_H3U_X;
break;
case PLCNo.REGI_H5U:
ElemType = SoftElemType.REGI_H5U_X;
break;
}
}
else if (nREGI == REGI.S)
{
switch (nPLCNo)
{
case PLCNo.REGI_H3U:
ElemType = SoftElemType.REGI_H3U_S;
break;
case PLCNo.REGI_H5U:
ElemType = SoftElemType.REGI_H5U_S;
break;
}
}
else if (nREGI == REGI.M)
{
switch (nPLCNo)
{
case PLCNo.REGI_H3U:
ElemType = SoftElemType.REGI_H3U_M;
break;
case PLCNo.REGI_H5U:
ElemType = SoftElemType.REGI_H5U_M;
break;
}
}
else if (nREGI == REGI.B)
{
ElemType = SoftElemType.REGI_H5U_B;
}
else if (nREGI == REGI.D)
{
bIsWord = true;
switch (nPLCNo)
{
case PLCNo.REGI_H3U:
ElemType = SoftElemType.REGI_H3U_DW;//REGI_H3U_DW,REGI_H5U_D
break;
case PLCNo.REGI_H5U:
ElemType = SoftElemType.REGI_H5U_D;//REGI_H3U_DW,REGI_H5U_D
break;
}
}
else if (nREGI == REGI.R)
{
bIsWord = true;
switch (nPLCNo)
{
case PLCNo.REGI_H3U:
ElemType = SoftElemType.REGI_H3U_R;
break;
case PLCNo.REGI_H5U:
ElemType = SoftElemType.REGI_H5U_R;
break;
}
}
//string[] arr = txtData2.Text.Split(‘ ‘);
int dataType = -1;
switch (nDataType)
{
case DataType.Float:
dataType = 0;
break;
case DataType.Int16:
dataType = 1;
break;
case DataType.Int32:
dataType = 2;
break;
}
GetDataFromUI(pBuf, arr, bIsWord, dataType);
int nRet = 0;
switch (nPLCNo)
{
case PLCNo.REGI_H3U:
nRet = HCPLCApi.H3u_Write_Soft_Elem(ElemType, nStartAddr, nCount, pBuf, nNetId);
break;
case PLCNo.REGI_H5U:
nRet = HCPLCApi.H5u_Write_Device_Block(ElemType, nStartAddr, nCount, pBuf, nNetId);
break;
}
if (nRet != 1)
{
//txtMsg2.AppendText(DateTime.Now.ToString() + “:ErrorCode ” + nRet.ToString() + “\r\n”);
//txtMsg2.ScrollToCaret();
//MessageBox.Show(“写入失败”);
//m_bKeepWrite = false;
return false;
}
else
{
return true;
//txtMsg2.AppendText(DateTime.Now.ToString() + “写入成功!\r\n”);
//txtMsg2.ScrollToCaret();
}
}
private void GetDataFromUI(byte[] pBuf, string[] arr, bool bIsWord, int nDataType)
{
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] == string.Empty)
{
break;
}
double nVal = Convert.ToDouble(arr[i]);
if (bIsWord)
{
if (nDataType == 0)//16位整形
{
int idata = Convert.ToInt16(arr[i]);
byte[] dataBuf = new byte[2] { 0, 0 };
dataBuf = BitConverter.GetBytes(idata);
pBuf[2 * i] = dataBuf[0];
pBuf[2 * i + 1] = dataBuf[1];
}
else if (nDataType == 1)//32位整形
{
int idata = Convert.ToInt32(arr[i]);
byte[] dataBuf = new byte[4] { 0, 0, 0, 0 };
dataBuf = BitConverter.GetBytes(idata);
pBuf[4 * i] = dataBuf[0];
pBuf[4 * i + 1] = dataBuf[1];
pBuf[4 * i + 2] = dataBuf[2];
pBuf[4 * i + 3] = dataBuf[3];
}
else if (nDataType == 2)//浮点数
{
float fdata = Convert.ToSingle(arr[i]);
byte[] dataBuf = new byte[4] { 0, 0, 0, 0 };
dataBuf = BitConverter.GetBytes(fdata);
pBuf[4 * i] = dataBuf[0];
pBuf[4 * i + 1] = dataBuf[1];
pBuf[4 * i + 2] = dataBuf[2];
pBuf[4 * i + 3] = dataBuf[3];
}
}
else
{
pBuf[i] = (byte)nVal;
}
}
}
/// <summary>
/// 打开链接
/// </summary>
/// <param name=”nIp”>PLC ip</param>
/// <param name=”nIpPort”>PLC 端口 默认 502</param>
/// <param name=”nNetId”>链接id</param>
public void OpenPlc(String nIp, int nIpPort = 502, int nNetId = 0)
{
bool result = false;
string msg = “链接成功!”;
try
{
result = HCPLCApi.Init_ETH_String(nIp, nNetId, nIpPort);
}
catch (Exception ex)
{
result = false;
msg = “链接失败 :” + ex.Message;
}
finally
{
if (result == true)
{
m_bConnected = true;
//MessageBox.Show(“连接成功”);
//txtMsg.Text = txtMsg.Text + DateTime.Now.ToString() + “连接PLC!\r\n”;
}
else
{
m_bConnected = false;
//MessageBox.Show(“连接失败”);
//txtMsg.Text = “”;
}
}
}
/// <summary>
/// 关闭链接 PLC
/// </summary>
public void ClosePLC(int nNetId = 0)
{
bool result = false;
string msg = “PLC链接关闭成功!”;
try
{
result = HCPLCApi.Exit_ETH(nNetId);
}
catch (Exception ex)
{
msg = “PLC关闭失败 错误 :” + ex.Message;
result = false;
}
finally
{
if (result == true)
{
m_bConnected = false;
//MessageBox.Show(“连接成功”);
//txtMsg.Text = txtMsg.Text + DateTime.Now.ToString() + “连接PLC!\r\n”;
}
else
{
m_bConnected = false;
//MessageBox.Show(“连接失败”);
//txtMsg.Text = “”;
}
}
}
}