Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
M

MitsubishiPlcProtocol

> 编程语言
Open source

三菱PLC(Mitsubishi)通讯协议的C#实现,支持FX、Q系列的ASCII-3E、BIN-3E、FX串口格式。

333 stars0 likes1 views
WebsiteGitHub

About

三菱PLC(Mitsubishi)通讯协议的C#实现,支持FX、Q系列的ASCII-3E、BIN-3E、FX串口格式。

MitsubishiPlcProtocol

三菱PLC(Mitsubishi)通讯协议的C#实现,支持FX、Q系列的ASCII-3E、BIN-3E、FX串口格式。 感谢 https://github.com/SatohNorio, 这里的MC协议的代码在 https://github.com/SatohNorio/McProtocol 的基础上改进得来。

HOW TO USE

将Mitsubishi、FX等目录及其下文件直接加到目标项目中即可。

下面是读写Q系列的示例代码(ASCII-3E或BIN-3E格式)

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");
	}

}

下面是读写FX系列的测试代码

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 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);

		//// 复位
		//response = FxCommandHelper.Make(FxCommandConst.FxCmdForceOff, new FxAddress("S1"));
		//res = _FxSerial.SendCmdToQnCPU(0, response);

		#region 读取所有 X/Y/M/ ,并计算其耗时

		Stopwatch sw = new Stopwatch();
		sw.Start();
		for(int ct = 0; ct  {0}", sw.ElapsedMilliseconds);

		}

		System.Threading.Thread.Sleep(1000);

		#endregion

		#region 针对 X/Y 的设置、读取

		// 置位与复位:必须采用 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);

		// 针对 Y001..Y177 设置与读取
		for(int i = 0; i = 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);
		}

		// 一次性读取多个字节的 X,例如 16 字节::必须采用 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);

		// 一次性读取多个字节的 Y,例如 16 字节::必须采用 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 针对 M 类型的读写
		// 针对 M001..M077 设置与读取
		for(int i = 0; i  lst = new List() { (uint)i };
			for(int k = 0; k (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响应。");
			}
		}
		#endregion

		Debug.Assert(false, "运行暂停!!!");
	}
}

联系

如果对本项目有什么建议或其它,请与我联系 [email protected] .

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C#

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言