柚子快報激活碼778899分享:C#匯川PLC通訊
柚子快報激活碼778899分享:C#匯川PLC通訊
1.下載匯川plc通訊API,然后下載文件,如下圖:
解壓上面下載的文件夾,在項目實例中找到兩個動態(tài)鏈接庫(ModbusTcpAPI.dll;StandardModbusApi.dll),如下圖:
2.創(chuàng)建匯川PLC通訊測試項目,我用的VS2019軟件,將上面的兩個動態(tài)鏈接庫(ModbusTcpAPI.dll;StandardModbusApi.dll)復制到創(chuàng)建的項目下,如圖:
項目開發(fā),UI界面如圖:
通訊類SvMitsubishiEASY,代碼如下:
public class SvMitsubishiEASY
{
#region //標準庫
[DllImport("StandardModbusApi.dll", EntryPoint = "Init_ETH_String", CallingConvention = CallingConvention.Cdecl)]
//連接PLC
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 = "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_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 = "H5u_Write_Device_Block", CallingConvention = CallingConvention.Cdecl)]
public static extern int H5u_Write_Device_Block(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_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);
#endregion
int nNetId = 1;
public void Initial(string ip, int port)
{
bool result = Init_ETH_String(ip, nNetId, port);
if (result == true)
{
MessageBox.Show("連接成功");
}
else
{
MessageBox.Show("連接失敗");
}
}
public void Close()
{
bool result = Exit_ETH(nNetId);
if (result == true)
{
MessageBox.Show("關閉連接成功");
}
else
{
MessageBox.Show("關閉連接失敗");
}
}
public void ReadBlock16(string startAddress, ref short[] addressValues)
{
var addrType = startAddress.Substring(0, 1);
var startAddr = Convert.ToInt32(startAddress.Substring(1, startAddress.Length - 1));
byte[] pBuf = new byte[16000];
int nCount = addressValues.Length;
int nRet = H3u_Read_Soft_Elem(GetAddrTypeByte(addrType), startAddr, nCount, pBuf, nNetId);
for (int i = 0; i < addressValues.Length; i++)
{
addressValues[i] = pBuf[i];
}
}
public void WriteBlock16(string startAddress, short[] addressValues)
{
var addrType = startAddress.Substring(0, 1);
var startAddr = Convert.ToInt32(startAddress.Substring(1, startAddress.Length - 1));
int nCount = addressValues.Length;
var sendByte = new List
for (int i = 0; i < addressValues.Length; i++)
{
sendByte.AddRange(BitConverter.GetBytes(addressValues[i]));
}
int nRet = H3u_Write_Soft_Elem(GetAddrTypeByte(addrType), startAddr, nCount, sendByte.ToArray(), nNetId);
}
private SoftElemType GetAddrTypeByte(string addrType)
{
switch (addrType)
{
case "X": return SoftElemType.REGI_H3U_X;
case "Y": return SoftElemType.REGI_H3U_Y;
case "M": return SoftElemType.REGI_H3U_M;
case "D": return SoftElemType.REGI_H3U_DW;
case "R": return SoftElemType.REGI_H3U_R;
default: return SoftElemType.REGI_H3U_SM;
}
}
}
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字元件的定義
}
實例調(diào)用代碼:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SvMitsubishiEASY PLC_EASY = new SvMitsubishiEASY();
//連接PLC
private void button1_Click(object sender, EventArgs e)
{
var ip = textBox1.Text.Trim();
var port = Convert.ToInt32(textBox2.Text.Trim());
PLC_EASY.Initial(ip, port);
}
//斷開PLC連接
private void button4_Click(object sender, EventArgs e)
{
PLC_EASY.Close();
}
//讀取數(shù)據(jù)
private void button2_Click(object sender, EventArgs e)
{
var addrass = textBox3.Text.Trim();
textBox4.Text=ReadSingle32(addrass).ToString();
}
//寫入數(shù)據(jù)
private void button3_Click(object sender, EventArgs e)
{
var addrass = textBox3.Text.Trim();
var value = Convert.ToInt32(textBox4.Text.Trim());
WriteSingle16(addrass, value);
}
public int ReadSingle32(string address)
{
try
{
var temp = new short[2];
PLC_EASY.ReadBlock16(address, ref temp);
var tempByte = new List
tempByte.AddRange(BitConverter.GetBytes(temp[0]));
tempByte.AddRange(BitConverter.GetBytes(temp[1]));
return BitConverter.ToInt32(tempByte.ToArray(), 0);
}
catch (Exception ex)
{
MessageBox.Show("PLC Read Fail:" + address);
return -999;
}
}
public bool WriteSingle16(string address, int value)
{
try
{
var temp = new short[] { (short)value };
PLC_EASY.WriteBlock16(address, temp);
return true;
}
catch (Exception ex)
{
MessageBox.Show("PLC Read Fail:" + address);
return false;
}
}
}
本篇文章主要介紹匯川PLC通訊開發(fā)實例,實例項目用的H3U系列通訊,目前只能讀取寫入整數(shù),其他還未研究,可以讀取寫入X,Y,M,D,R碼。
柚子快報激活碼778899分享:C#匯川PLC通訊
參考鏈接
本文內(nèi)容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權,聯(lián)系刪除。