2021-11-09 08:51:47 索煒達(dá)電子 911
項(xiàng)目編號(hào):D69
文件大?。?.66M
源碼說明:帶中文注釋
開發(fā)環(huán)境:.cs
簡要概述:
三菱PLC(Mitsubishi)通訊協(xié)議的C#實(shí)現(xiàn),支持FX、Q系列的ASCII-3E、BIN-3E、FX串口格式。
將Mitsubishi、FX等目錄及其下文件直接加到目標(biāo)項(xiàng)目中即可。
public static class McTest
{
private static IMitsubishiPlc _plc = null;
public static void Test()
{
_plc = new McProtocolUdp("192.0.1.254", 8195);
_plc.Open();
_plc.Excute("D0,32");
_plc.Excute("M850,32");
_plc.Excute("D001");
_plc.Excute("D126,2");
_plc.Excute("D126..130=0");
_plc.Excute("D10=135");
}
}
public class Fx_Test
{
private FxSerialDeamon _FxSerial;
public void OpenPort ()
{
if(_FxSerial == null) {
_FxSerial = new FxSerialDeamon();
_FxSerial.Start(1);
}
}
public void ClosePort ()
{
if(_FxSerial != null) {
_FxSerial.Dispose();
}
_FxSerial = null;
}
public void Test_ReadAllPoints ()
{
List<AcquireRawValue> rawValues =
_FxSerial.ReadAllPoints(null, TimeSpan.FromSeconds(3));
}
public void Test_All ()
{
string cmd;
FxCommandResponse res;
Random _Random = new Random();
//// 置位
//response = FxCommandHelper.Make(FxCommandConst.FxCmdForceOn, new FxAddress("S1"));
//res = _FxSerial.SendCmdToQnCPU(0, response);
//// 復(fù)位
//response = FxCommandHelper.Make(FxCommandConst.FxCmdForceOff, new FxAddress("S1"));
//res = _FxSerial.SendCmdToQnCPU(0, response);
#region 讀取所有 X/Y/M/ ,并計(jì)算其耗時(shí)
Stopwatch sw = new Stopwatch();
sw.Start();
for(int ct = 0; ct < 10; ct++) {
// 一次性讀取多個(gè)字節(jié)的 X,例如 16 字節(jié)::必須采用 AddressLayoutBin 方式
cmd = FxCommandHelper.Make(FxCommandConst.FxCmdRead, new FxAddress("X0", ControllerTypeConst.ctPLC_Fx), 16);
res = _FxSerial.Send(0, cmd);
//Debug.WriteLine(string.Format("成批讀X0..X177 \t{0}", res.ToString()));
//System.Threading.Thread.Sleep(1000);
// 一次性讀取多個(gè)字節(jié)的 Y,例如 16 字節(jié)::必須采用 AddressLayoutBin 方式
cmd = FxCommandHelper.Make(FxCommandConst.FxCmdRead, new FxAddress("Y0", ControllerTypeConst.ctPLC_Fx), 16);
res = _FxSerial.Send(0, cmd);
//Debug.WriteLine(string.Format("成批讀Y0..Y177 \t{0}", res.ToString()));
//System.Threading.Thread.Sleep(1000);
// 一次性讀取多個(gè)字節(jié)的 M,每次讀取128個(gè)單元::必須采用 AddressLayoutBin 方式
cmd = FxCommandHelper.Make(FxCommandConst.FxCmdRead, new FxAddress("M0", ControllerTypeConst.ctPLC_Fx), 128);
res = _FxSerial.Send(0, cmd);
//Debug.WriteLine(string.Format("成批讀M{0}..M{1} \t{2}", result, result + 128, res.ToString()));
Debug.Print("=====================> {0}", sw.ElapsedMilliseconds);
}
System.Threading.Thread.Sleep(1000);
#endregion
#region 針對(duì) X/Y 的設(shè)置、讀取
// 置位與復(fù)位:必須采用 AddressLayoutByte 方式
cmd = FxCommandHelper.Make(FxCommandConst.FxCmdForceOn, new FxAddress("Y20", FxAddressLayoutType.AddressLayoutByte));
res = _FxSerial.Send(0, cmd);
Debug.WriteLine(res.ToString());
System.Threading.Thread.Sleep(1000);
cmd = FxCommandHelper.Make(FxCommandConst.FxCmdForceOff, new FxAddress("Y20", FxAddressLayoutType.AddressLayoutByte));
res = _FxSerial.Send(0, cmd);
Debug.WriteLine(res.ToString());
System.Threading.Thread.Sleep(1000);
// 針對(duì) Y001..Y177 設(shè)置與讀取
for(int i = 0; i < 128; i++) {
cmd = FxCommandHelper.Make(FxCommandConst.FxCmdForceOff,
new FxAddress(string.Format("Y{0}", Convert.ToString(i, 8)), FxAddressLayoutType.AddressLayoutByte));
res = _FxSerial.Send(0, cmd);
}
// 針對(duì) Y001..Y077 設(shè)置與讀取
for(int i = 0; i < 128; i++) {
cmd = FxCommandHelper.Make(FxCommandConst.FxCmdForceOn,
new FxAddress(string.Format("Y{0}", Convert.ToString(i, 8)), FxAddressLayoutType.AddressLayoutByte));
res = _FxSerial.Send(0, cmd);
//Debug.WriteLine(res.ToString());
System.Threading.Thread.Sleep(100);
if((i - 8) >= 0) {
cmd = FxCommandHelper.Make(FxCommandConst.FxCmdForceOff,
new FxAddress(string.Format("Y{0}", Convert.ToString(i - 8, 8)), FxAddressLayoutType.AddressLayoutByte));
res = _FxSerial.Send(0, cmd);
Debug.WriteLine(string.Format("Y{0}\t{1}", Convert.ToString(i, 8), res.ToString()));
}
//System.Threading.Thread.Sleep(1000);
}
// 一次性讀取多個(gè)字節(jié)的 X,例如 16 字節(jié)::必須采用 AddressLayoutBin 方式
cmd = FxCommandHelper.Make(FxCommandConst.FxCmdRead, new FxAddress("X0", ControllerTypeConst.ctPLC_Fx), 16);
res = _FxSerial.Send(0, cmd);
Debug.WriteLine(string.Format("成批讀X0..X177 \t{0}", res.ToString()));
System.Threading.Thread.Sleep(1000);
// 一次性讀取多個(gè)字節(jié)的 Y,例如 16 字節(jié)::必須采用 AddressLayoutBin 方式
cmd = FxCommandHelper.Make(FxCommandConst.FxCmdRead, new FxAddress("Y0", ControllerTypeConst.ctPLC_Fx), 16);
res = _FxSerial.Send(0, cmd);
Debug.WriteLine(string.Format("成批讀Y0..Y177 \t{0}", res.ToString()));
System.Threading.Thread.Sleep(1000);
#endregion
#region 針對(duì) M 類型的讀寫
// 針對(duì) M001..M077 設(shè)置與讀取
for(int i = 0; i < 64; i++) {
cmd = FxCommandHelper.Make(FxCommandConst.FxCmdForceOn,
new FxAddress(string.Format("M{0}", i), FxAddressLayoutType.AddressLayoutByte));
res = _FxSerial.Send(0, cmd);
Debug.WriteLine(res.ToString());
//response = FxCommandHelper.Make(FxCommandConst.FxCmdForceOff,
// new FxAddress(string.Format("M{0}", Convert.ToString(result, 8)), FxAddressLayoutType.AddressLayoutByte));
//res = _FxSerial.SendCmdToQnCPU(0, response);
//Debug.WriteLine(res.ToString());
//System.Threading.Thread.Sleep(100);
}
// 一次性讀取多個(gè)字節(jié)的 M,例如 16 字節(jié)::必須采用 AddressLayoutBin 方式
cmd = FxCommandHelper.Make(FxCommandConst.FxCmdRead, new FxAddress("M0", ControllerTypeConst.ctPLC_Fx), 64);
res = _FxSerial.Send(0, cmd);
Debug.WriteLine(string.Format("成批讀M0..M77 \t{0}", res.ToString()));
System.Threading.Thread.Sleep(1000);
#endregion
#region 循環(huán)設(shè)置與讀取 Dxxx 的數(shù)據(jù)
for(int i = 0; i < 1; i++) {
List<uint> lst = new List<uint>() { (uint)i };
for(int k = 0; k < 10; k++)
lst.Add((uint)_Random.Next());
cmd = FxCommandHelper.Make<UInt32DataType>(FxCommandConst.FxCmdWrite, new FxAddress("D1", ControllerTypeConst.ctPLC_Fx), lst);
res = _FxSerial.Send(0, cmd);
Debug.WriteLine(res.ToString());
cmd = FxCommandHelper.Make(FxCommandConst.FxCmdRead, new FxAddress("D1", ControllerTypeConst.ctPLC_Fx), lst.Count * 4);
res = _FxSerial.Send(0, cmd, UInt32DataType.Default);
Debug.WriteLine(res.ToString());
if(res.ResponseValue != null && res.ResponseValue.Count > 0) {
Debug.WriteLine("");
Debug.Write(DateTime.Now.ToString());
Debug.Write("\t");
for(int j = 0; j < res.ResponseValue.Count; j++) {
Debug.Write(res.ResponseValue[j]);
Debug.Write(',');
}
} else {
Debug.WriteLine("沒有收到FX PLC響應(yīng)。");
}
}
#endregion
Debug.Assert(false, "運(yùn)行暫停?。。?quot;);
}
}
目錄│文件列表:
└ MitsubishiPlcProtocol
│ App.config
│ MitsubishiTester.csproj
│ MitsubishiTester.sln
│ Program.cs
├ Common
│ │ AcquirePoint.cs
│ │ AcquirePointConversion.cs
│ │ AcquirePointEnum.cs
│ │ AcquirePointUnitType.cs
│ │ AcquireRawValue.cs
│ │ AcquireValue.cs
│ │ ConfigItem.cs
│ │ ConfigItemCatalogo.cs
│ │ ControllerBaseImpl.cs
│ │ ControllerType.cs
│ │ CraftworkItemInfo.cs
│ │ CraftworkMaterialNoEnum.cs
│ │ EnumForMixSystem.cs
│ │ IControllerAction.cs
│ │ IControllerBase.cs
│ │ IOServerStatus.cs
│ │ WeightingInfoArgs.cs
│ │ WeightingStepEnum.cs
│ │ WorkRegionEnum.cs
│ └ WorkRegionStateItem.cs
├ packages
│ │ System.Data.SQLite.dll
│ │ System.Data.SQLite.pdb
│ │ System.Data.SQLite.xml
│ │ Vila.Core.dll
│ └ Vila.Core.pdb
├ PLC
│ │ CellDataTypes.cs
│ │ ResultCodeConst.cs
│ ├ FX
│ │ │ FxAddress.cs
│ │ │ FxAddressLayoutInfo.cs
│ │ │ FxAddressManager.cs
│ │ │ FxCommandArgs.cs
│ │ │ FxCommandHelper.cs
│ │ │ FxCommandResponse.cs
│ │ │ FxCommController.cs
│ │ │ FxConvert.cs
│ │ │ FxDefine.cs
│ │ │ FxRingBuffer.cs
│ │ │ FxSerialDeamon.cs
│ │ └ Fx_Test.cs
│ ├ MCPackage
│ │ │ MCComponentGroup.cs
│ │ │ MCDefine.cs
│ │ └ ResponseMessage.cs
│ ├ Mitsubishi
│ │ │ IMitsubishiPlc.cs
│ │ │ McCommand.cs
│ │ │ McCommandExtersions.cs
│ │ │ McProtocolApp.cs
│ │ │ McProtocolBinary.cs
│ │ │ McProtocolTcp.cs
│ │ │ McProtocolUdp.cs
│ │ │ McSwapExtersions.cs
│ │ │ McTest.cs
│ │ └ PlcDeviceTypeExtersions.cs
│ └ SerialDevice
│ │ SerialParam.cs
│ │ SerialPortManager.cs
│ └ SerialPortSync.cs
└ Properties
└ AssemblyInfo.cs