資源簡介
C#和臺達PLC通信,基于TCP/ip通信方式。適合初學者學習交流。

代碼片段和文件信息
using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Text;
namespace?BU43
{
????class?DeltaPLC
????{
????????public?static?void?SetSerial(string?Port?int?Baud?int?StopBits?int?Parity?int?DataBits)
????????{
????????????Modbus.COMPort?=?Port;
????????????Modbus.Baud?=?Baud;
????????????Modbus.StopBits?=?StopBits;
????????????Modbus.Parity?=?Parity;
????????????Modbus.DataBits?=?DataBits;
????????}
????????public?static?int?SetDReg(int?Reg?int?RegValue)
????????{
????????????int?Result?=?-1;
????????????int?DReg?=?0x1000?+?Reg;
????????????try
????????????{
????????????????string?tx?=?String.Concat(“00“?“06“?DReg.ToString(“X4“)?RegValue.ToString(“X4“));
????????????????string?rx?=?Modbus.Read(tx);
????????????????Result?=?0;
????????????}
????????????catch
????????????{
????????????????Result?=?1;
????????????}
????????????return?Result;
????????}
????????public?static?void?SetDDReg(int?Reg?int?RegValue)
????????{
????????????int?LowWord?=?0?HighWord?=?0;
????????????int?DReg?=?0x1000?+?Reg;
????????????LowWord?=?RegValue?&?0xFFFF;
????????????HighWord?=?(RegValue?>>?16)?&?0xFFFF;
????????????string?tx?=?String.Concat(“00“?“06“?DReg.ToString(“X4“)?LowWord.ToString(“X4“));
????????????string?rx?=?Modbus.Read(tx);
????????????tx?=?String.Concat(“00“?“06“?(DReg?+?1).ToString(“X4“)?HighWord.ToString(“X4“));
????????????rx?=?Modbus.Read(tx);
????????}
????????public?static?void?ReadDReg(int?Reg?out?double?RegValue)
????????{
????????????Reg?+=?0x1000;
????????????string?tx?=?String.Concat(“00“?“03“?Reg.ToString(“X4“)?“0001“);
????????????string?rx?=?Modbus.Read(tx);
????????????RegValue?=?Modbus.ToDec(rx.Substring(7?4)?0?4);
????????}
????????public?static?void?ReadDDReg(int?Reg?out?double?RegValue)
????????{
????????????double?RegData_Tmp?=?0;
????????????ushort?LowWord?=?0?HighWord?=?0;
????????????Reg?+=?0x1000;
????????????string?tx?=?String.Concat(“00“?“03“?Reg.ToString(“X4“)?“0001“);
????????????string?rx?=?Modbus.Read(tx);
????????????RegData_Tmp?=?Modbus.ToDec(rx.Substring(7?4)?0?4);
????????????LowWord?=?(ushort)RegData_Tmp;
????????????tx?=?String.Concat(“00“?“03“?(Reg?+?1).ToString(“X4“)?“0001“);
????????????rx?=?Modbus.Read(tx);
????????????RegData_Tmp?=?Modbus.ToDec(rx.Substring(7?4)?0?4);
????????????HighWord?=?(ushort)RegData_Tmp;
????????????RegValue?=?(int)(LowWord?+?(HighWord?<16));
????????}
????????public?static?void?SetMReg(int?Reg?bool?RegState)
????????{
????????????string?RegStateValue;
????????????Reg?+=?0x800;
????????????if?(RegState)
????????????????RegStateValue?=?“FF00“;
????????????else
????????????????RegStateValue?=?“0000“;
????????????string?tx?=?String.Concat(“00“?“05“?Reg.ToString(“X4“)?RegStateValue);
????????????string?rx?=?Modbus.Read(tx);
????????????string?sCheck?=?String.Concat(“:“?tx?Modbus.CheckSum(tx)?“\r“);
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2016-10-29?17:03??DeltaPLC\
?????目錄???????????0??2016-10-29?17:02??DeltaPLC\Class\
?????文件????????4504??2016-10-29?17:01??DeltaPLC\Class\DeltaPLC.cs
?????文件????????9032??2016-10-29?16:38??DeltaPLC\Class\Modbus.cs
?????目錄???????????0??2016-10-29?17:03??DeltaPLC\DeltaPLC_Demo\
?????目錄???????????0??2016-10-29?17:04??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\
?????文件?????????881??2016-10-29?17:03??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo.sln
?????文件???????22528??2016-11-17?08:34??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo.suo
?????目錄???????????0??2016-10-29?17:03??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\bin\
?????目錄???????????0??2016-10-29?17:11??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\bin\Debug\
?????文件???????17920??2016-10-30?10:14??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\bin\Debug\DeltaPLC_Demo.exe
?????文件???????46592??2016-10-30?10:14??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\bin\Debug\DeltaPLC_Demo.pdb
?????文件???????11600??2017-02-08?14:34??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\bin\Debug\DeltaPLC_Demo.vshost.exe
?????文件?????????490??2010-03-17?22:39??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\bin\Debug\DeltaPLC_Demo.vshost.exe.manifest
?????目錄???????????0??2016-10-29?17:04??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\Class\
?????文件???????11452??2016-10-30?10:14??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\Class\DeltaPLC.cs
?????文件????????9836??2016-10-29?18:13??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\Class\Modbus.cs
?????文件????????3773??2016-10-29?17:10??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\DeltaPLC_Demo.csproj
?????文件?????????522??2016-10-29?17:13??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\Form1.cs
?????文件????????9807??2016-10-29?17:08??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\Form1.Designer.cs
?????文件????????5817??2016-10-29?17:08??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\Form1.resx
?????目錄???????????0??2016-10-29?17:03??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\obj\
?????目錄???????????0??2016-10-29?17:03??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\obj\x86\
?????目錄???????????0??2016-11-17?08:34??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\obj\x86\Debug\
?????文件?????????666??2017-02-08?14:34??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\obj\x86\Debug\DeltaPLC_Demo.csproj.FileListAbsolute.txt
?????文件?????????975??2016-10-29?17:10??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\obj\x86\Debug\DeltaPLC_Demo.csproj.GenerateResource.Cache
?????文件???????17920??2016-10-30?10:14??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\obj\x86\Debug\DeltaPLC_Demo.exe
?????文件?????????180??2016-10-29?17:10??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\obj\x86\Debug\DeltaPLC_Demo.Form1.resources
?????文件???????46592??2016-10-30?10:14??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\obj\x86\Debug\DeltaPLC_Demo.pdb
?????文件?????????180??2016-10-29?17:10??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\obj\x86\Debug\DeltaPLC_Demo.Properties.Resources.resources
?????文件????????5359??2016-11-17?08:34??DeltaPLC\DeltaPLC_Demo\DeltaPLC_Demo\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
............此處省略9個文件信息
- 上一篇:用C#編寫的新聞發布系統
- 下一篇:C# 圖片比對 圖片查找
評論
共有 條評論