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