Program.cs 12 KB

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