Program.cs 12 KB

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