Program.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. using FinsTcp;
  2. using GeneralData;
  3. using HardwareData;
  4. using ModBusTcp;
  5. using ORM;
  6. using ProtocalGeneral;
  7. using RTCommunicatorBase;
  8. using RTCommunicatorTLV;
  9. using SqlSugarORM;
  10. using Universal;
  11. using Universal.IO;
  12. namespace Test;
  13. internal class Program
  14. {
  15. static void Main()
  16. {
  17. //RingBuffer<int> ring = new(5);
  18. //for (int i = 1; i <= 19; i++)
  19. //{
  20. // ring.Insert(i);
  21. //}
  22. //var t = ring.ReadValues();
  23. //string DBConnectionString = "Database=postgres;Password=123456;Host=localhost;Username=postgres;Persist Security Info=True";
  24. //int start = DBConnectionString.IndexOf('=') + 1;
  25. //int end = DBConnectionString.IndexOf(';');
  26. //string newDbString1 = DBConnectionString[..start];
  27. //string newDBString2 = DBConnectionString[end..];
  28. //string newDBS = $"{newDbString1}{DateTime.Now:yyyy_MM_dd_HH_mm_ss}{newDBString2}";
  29. //TestClass testClass = new();
  30. //testClass.SetFile();
  31. //CreateUser cu = new();
  32. //cu.Create();
  33. //TestFins test = new();
  34. //test.TestFinsP();
  35. TestClass test = new();
  36. test.RunOnce();
  37. Thread.Sleep(-1);
  38. }
  39. }
  40. public class TestFins : ITcpConnectNority
  41. {
  42. public void Connect(string ip, int port)
  43. {
  44. Console.WriteLine($"Connected {ip}:{port}");
  45. }
  46. public void DisConnect(string ip, int port)
  47. {
  48. Console.WriteLine($"Disconnected {ip}:{port}");
  49. }
  50. public void Test()
  51. {
  52. //POmronFinsTCP.Net.EtherNetPLC
  53. //FinsTCP service = new();
  54. //var result = service.Link("192.168.250.1", 9600);
  55. //service.SetData<bool>("D1061.1", true);
  56. //service.SetData<float>("D14022", 2f);
  57. //float f = service.GetData<float>("D14022");
  58. }
  59. Fins_Tcp fins = new();
  60. public void TestFinsP()
  61. {
  62. fins.Initialize(this);
  63. fins.Connect("192.168.250.1", 9600, HeartBeatCallBack);
  64. //fins.StartHeartBeat(HeartBeatCallBack);
  65. while (true)
  66. {
  67. bool b = fins.SetData<bool>("D1061.1", true);
  68. Console.WriteLine($"Heart beat {b}");
  69. Thread.Sleep(1000);
  70. }
  71. //b = fins.GetData<bool>("D1061.1", out bool re);
  72. //b = fins.SetData<float>("D14022", 3f);
  73. //b = fins.GetData<float>("D14022", out float f);
  74. }
  75. private bool HeartBeatCallBack()
  76. {
  77. return fins.SetData<bool>("D1061.1", true);
  78. }
  79. }
  80. public class CreateUser
  81. {
  82. public void Create()
  83. {
  84. IORM orm = new SqlSugarCustom();
  85. orm.Initialize();
  86. orm.Open("Database=postgres;Password=123456;Host=localhost;Username=postgres;Persist Security Info=True", DbType.PostgreSQL);
  87. orm.CreateTable<UserInfo>("UserAuthority");
  88. UserInfo userInfo = new()
  89. {
  90. UserName = "Operator",
  91. Password = "Aa123456".ToBase64(),
  92. Authority = UserAuthority.Operator
  93. };
  94. orm.Insert<UserInfo>("UserAuthority", userInfo);
  95. userInfo = new()
  96. {
  97. UserName = "Engineer",
  98. Password = "Aa123456".ToBase64(),
  99. Authority = UserAuthority.Engineer
  100. };
  101. orm.Insert<UserInfo>("UserAuthority", userInfo);
  102. }
  103. }
  104. public class TestClass : IRTMini8Provider
  105. {
  106. public void Connected(string ip, int port)
  107. {
  108. Console.WriteLine($"ConnectedNotify {ip} {port}");
  109. }
  110. public void DisConnected(string ip, int port)
  111. {
  112. Console.WriteLine($"DisConnectedNotify {ip} {port}");
  113. }
  114. //Singleton Mode
  115. private RTCommunicator_TLV communicator = new();
  116. public void RunOnce()
  117. {
  118. communicator = new();
  119. communicator.Initialize(this);
  120. communicator.StartService("127.0.0.1", 50052);
  121. }
  122. public void SetFile()
  123. {
  124. communicator.SelectConfigFile("Minics Config 1");
  125. }
  126. public void End()
  127. {
  128. communicator.CloseService();
  129. }
  130. void IRTMini8Provider.CurrentTempConfigFile(string fileName)
  131. {
  132. }
  133. void IConnectNotify.Connected(string ip, int port)
  134. {
  135. Console.WriteLine($"Connected {ip}:{port}");
  136. }
  137. void IConnectNotify.DisConnected(string ip, int port)
  138. {
  139. Console.WriteLine($"Disconnected {ip}:{port}");
  140. }
  141. public void ChannelAlarmNotify(ST_ALARM_Notify alarm)
  142. {
  143. Console.WriteLine($"Alarm Mini8 {alarm.Mini8Index}-{alarm.ChannelIndex:00} {alarm.PV:000.00} {alarm.Caps:000.00} {alarm.Floor:000.00} {alarm.AlarmType} {alarm.HeaterType}");
  144. }
  145. public void Mini8ConnectNotify(byte mini8Index)
  146. {
  147. Console.WriteLine($"Mini8 Connected {mini8Index}");
  148. }
  149. public void Mini8DisconnectNotify(byte mini8)
  150. {
  151. Console.WriteLine($"Mini8 Disconnected {mini8}");
  152. }
  153. public void ChannelRealtimeNotify(ST_CHANNEL_Notify channel)
  154. {
  155. Console.WriteLine($"{DateTime.Now:HH:mm:ss.f} Mini8 {channel.Mini8Index} Channel {channel.ChannelIndex} {channel.PV} {channel.WorkingOutput}");
  156. }
  157. }
  158. interface ITest
  159. {
  160. void Run();
  161. }
  162. class TestSerialXML
  163. {
  164. public static void Test()
  165. {
  166. //Mini8Address mini8Address = new()
  167. //{
  168. // Address = "192.168.250.11",
  169. // Port = 502
  170. //};
  171. ChannelAddress address = new()
  172. {
  173. Mini8Index = 1,
  174. ChannelIndex = 2,
  175. //TcBorken = new(1, TypeCode.Single, AddressType.Input, RW_Access.Read),
  176. //CurrentTemp = new(2, TypeCode.Single, AddressType.Input, RW_Access.Read),
  177. //Target = new(3, TypeCode.Single),
  178. //Caps = new(4, TypeCode.Single),
  179. //Floor = new(5, TypeCode.Single),
  180. //Delay = new(6, TypeCode.Single),
  181. //P = new(7, TypeCode.Single),
  182. //I = new(8, TypeCode.Single),
  183. //D = new(9, TypeCode.Single)
  184. };
  185. XmlFileHelper.WriteFile(@"E:\address.xml", address);
  186. XmlFileHelper.ReadFile(@"E:\address.xml", out ChannelAddress? channel);
  187. Console.WriteLine(channel);
  188. }
  189. }
  190. //class TestModbus
  191. //{
  192. // public static void Tests()
  193. // {
  194. // string address = "192.168.250.11";
  195. // ushort port = 502;
  196. // Modbus_Nmodbus modbus = new();
  197. // modbus.Initialize("T");
  198. // Console.WriteLine($"Open {address}:{port} --{modbus.Open(address, port)}");
  199. // //await modbus.SetValue<float>(15694, 50);
  200. // //await modbus.SetValues<float>(15694, [60, 70]);
  201. // //await modbus.SetValue<float>(15532, 20);
  202. // modbus.GetFloat(41014, out float? test);
  203. // Console.WriteLine();
  204. // Console.WriteLine("Read PVs");
  205. // modbus.GetFloats(15500, 16, out float[]? fs);
  206. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00") + "℃"));
  207. // Console.WriteLine();
  208. // Console.WriteLine("Read Work Out Percentage");
  209. // modbus.GetFloats(15532, 16, out fs);
  210. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00") + "%"));
  211. // Console.WriteLine();
  212. // Console.WriteLine("Read AT Status");
  213. // ushort[]? us = modbus.GetUshort(15564, (ushort)16);
  214. // us!.Foreach(t => Console.WriteLine((AutoTune)t));
  215. // Console.WriteLine();
  216. // Console.WriteLine("Read AutoTune P");
  217. // modbus.GetFloats(15596, 16, out fs);
  218. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00")));
  219. // Console.WriteLine();
  220. // Console.WriteLine("Read AutoTune I");
  221. // modbus.GetFloats(15628, 16, out fs);
  222. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00")));
  223. // Console.WriteLine();
  224. // Console.WriteLine("Read AutoTune D");
  225. // modbus.GetFloats(15660, 16, out fs);
  226. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00")));
  227. // Console.WriteLine();
  228. // Console.WriteLine("Read SetPoint");
  229. // modbus.GetFloats(15694, 16, out fs);
  230. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00") + "℃"));
  231. // Console.WriteLine();
  232. // Console.WriteLine("Read Active TuneSet");
  233. // us = modbus.GetUshort(15726, (ushort)16);
  234. // us!.Foreach(t => Console.WriteLine((ActiveTuneSet)t));
  235. // Console.WriteLine();
  236. // Console.WriteLine("Read Running P");
  237. // modbus.GetFloats(15758, 16, out fs);
  238. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00")));
  239. // Console.WriteLine();
  240. // Console.WriteLine("Read Running I");
  241. // modbus.GetFloats(15780, 16, out fs);
  242. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00")));
  243. // Console.WriteLine();
  244. // Console.WriteLine("Read Running D");
  245. // modbus.GetFloats(15822, 16, out fs);
  246. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00")));
  247. // Console.WriteLine();
  248. // Console.WriteLine("Read Up Rate");
  249. // modbus.GetFloats(15860, 16, out fs);
  250. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00") + "℃/Min"));
  251. // Console.WriteLine();
  252. // Console.WriteLine("Read Down Rate");
  253. // modbus.GetFloats(15892, 16, out fs);
  254. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00") + "℃/Min"));
  255. // Console.WriteLine();
  256. // Console.WriteLine("Read TC Broken");
  257. // modbus.GetBits(15692, out byte[]? bits);
  258. // bits!.For(0, 8, t => Console.WriteLine((TcBorken)t));
  259. // modbus.GetBits(15693, out bits);
  260. // bits!.For(0, 8, t => Console.WriteLine((TcBorken)t));
  261. // Console.WriteLine();
  262. // Console.WriteLine("Read Inhibit");
  263. // modbus.SetUshort(15854, 252);
  264. // modbus.SetUshort(15855, 255);
  265. // modbus.SetUshort(15855, 255);
  266. // modbus.GetBits(15854, out bits);
  267. // bits!.For(0, 8, t => Console.WriteLine((Active)t));
  268. // modbus.GetBits(15855, out bits);
  269. // bits!.For(0, 8, t => Console.WriteLine((Active)t));
  270. // Console.WriteLine();
  271. // Console.WriteLine("Read Auto Tune Active");
  272. // modbus.SetUshort(15858, 255);
  273. // modbus.SetUshort(15859, 255);
  274. // modbus.GetBits(15858, out bits);
  275. // bits!.For(0, 8, t => Console.WriteLine((Active)t));
  276. // modbus.GetBits(15859, out bits);
  277. // bits!.For(0, 8, t => Console.WriteLine((Active)t));
  278. // }
  279. //}
  280. class TestModbus_FluentModbus
  281. {
  282. public static void Test()
  283. {
  284. //IPAddress ipAddress = IPAddress.Parse("192.168.250.11");
  285. //IPEndPoint endPoint = new(ipAddress, 502);
  286. //ModbusTcpClient client = new();
  287. //client.Connect(endPoint);
  288. //Span<byte> t = client.ReadHoldingRegisters(0x00, 15500, 500);
  289. //Span<byte> t2 = client.ReadHoldingRegisters(0x00, 15564, 1);
  290. Modbus_Tcp client = new();
  291. client.Open("192.168.250.11", 502);
  292. byte[]? buffer = client.GetBuffer(4962, 32);
  293. //client.SetFloat(4962, 32.1f);
  294. client.SetUshort(4962, 1);
  295. buffer = client.GetBuffer(4962, 1);
  296. ushort s = BufferToUshort(buffer);
  297. }
  298. private static unsafe ushort BufferToUshort(byte[]? bytes)
  299. {
  300. if (bytes is null)
  301. return ushort.MaxValue;
  302. Span<byte> sb = bytes.AsSpan<byte>();
  303. sb.Reverse();
  304. fixed (byte* pByte = sb)
  305. {
  306. ushort* pResult = (ushort*)pByte;
  307. return *pResult;
  308. }
  309. }
  310. }
  311. enum TcBorken : byte
  312. {
  313. Normal = 0,
  314. Error
  315. }
  316. enum Active : byte
  317. {
  318. On = 0,
  319. Off = 1,
  320. }
  321. enum ActiveTuneSet : ushort
  322. {
  323. AutoTune = 0,
  324. Running
  325. }
  326. enum AutoTune : ushort
  327. {
  328. Unavailable = 0,
  329. Ready,
  330. Triggered,
  331. Running,
  332. Complete,
  333. Aborted,
  334. Timeout,
  335. Overflow
  336. }
  337. class YieldTest
  338. {
  339. public static IEnumerable<int> Test()
  340. {
  341. for (int i = 0; i < 10; i++)
  342. {
  343. if (i == 1)
  344. yield return -1;
  345. yield return i;
  346. }
  347. }
  348. }
  349. class ORMTest : IOrmProvider
  350. {
  351. public int Test1 { get; set; }
  352. public int Test2 { get; set; }
  353. public void Log(string log, DateTime dateTime, LogLevel logLevel)
  354. {
  355. Console.WriteLine($"{logLevel}-{dateTime:hh:mm:ss} {log}");
  356. }
  357. public async Task Test()
  358. {
  359. IORM orm = new SqlSugarCustom();
  360. orm.Initialize(this);
  361. orm.Open("Database=postgres;Password=123456;Host=localhost;Username=postgres;Persist Security Info=True", DbType.PostgreSQL);
  362. //orm.CreateDataBase("Minics");
  363. orm.CreateTable<ORMTest>("OrmTest1");
  364. //await orm.Insert<ORMTest>("OrmTest1", new() { Test1 = 1, Test2 = 1 });
  365. await orm.Query<ORMTest>("OrmTest1", Result);
  366. await orm.Query<ORMTest>("OrmTest1", t => t.Test1 == 1, Result);
  367. }
  368. void Result(List<ORMTest> oRMTests)
  369. {
  370. Console.WriteLine(oRMTests.Count);
  371. }
  372. }