Program.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 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}");
  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. }
  154. interface ITest
  155. {
  156. void Run();
  157. }
  158. class TestSerialXML
  159. {
  160. public static void Test()
  161. {
  162. //Mini8Address mini8Address = new()
  163. //{
  164. // Address = "192.168.250.11",
  165. // Port = 502
  166. //};
  167. ChannelAddress address = new()
  168. {
  169. Mini8Index = 1,
  170. ChannelIndex = 2,
  171. //TcBorken = new(1, TypeCode.Single, AddressType.Input, RW_Access.Read),
  172. //CurrentTemp = new(2, TypeCode.Single, AddressType.Input, RW_Access.Read),
  173. //Target = new(3, TypeCode.Single),
  174. //Caps = new(4, TypeCode.Single),
  175. //Floor = new(5, TypeCode.Single),
  176. //Delay = new(6, TypeCode.Single),
  177. //P = new(7, TypeCode.Single),
  178. //I = new(8, TypeCode.Single),
  179. //D = new(9, TypeCode.Single)
  180. };
  181. XmlFileHelper.WriteFile(@"E:\address.xml", address);
  182. XmlFileHelper.ReadFile(@"E:\address.xml", out ChannelAddress? channel);
  183. Console.WriteLine(channel);
  184. }
  185. }
  186. //class TestModbus
  187. //{
  188. // public static void Tests()
  189. // {
  190. // string address = "192.168.250.11";
  191. // ushort port = 502;
  192. // Modbus_Nmodbus modbus = new();
  193. // modbus.Initialize("T");
  194. // Console.WriteLine($"Open {address}:{port} --{modbus.Open(address, port)}");
  195. // //await modbus.SetValue<float>(15694, 50);
  196. // //await modbus.SetValues<float>(15694, [60, 70]);
  197. // //await modbus.SetValue<float>(15532, 20);
  198. // modbus.GetFloat(41014, out float? test);
  199. // Console.WriteLine();
  200. // Console.WriteLine("Read PVs");
  201. // modbus.GetFloats(15500, 16, out float[]? fs);
  202. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00") + "℃"));
  203. // Console.WriteLine();
  204. // Console.WriteLine("Read Work Out Percentage");
  205. // modbus.GetFloats(15532, 16, out fs);
  206. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00") + "%"));
  207. // Console.WriteLine();
  208. // Console.WriteLine("Read AT Status");
  209. // ushort[]? us = modbus.GetUshort(15564, (ushort)16);
  210. // us!.Foreach(t => Console.WriteLine((AutoTune)t));
  211. // Console.WriteLine();
  212. // Console.WriteLine("Read AutoTune P");
  213. // modbus.GetFloats(15596, 16, out fs);
  214. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00")));
  215. // Console.WriteLine();
  216. // Console.WriteLine("Read AutoTune I");
  217. // modbus.GetFloats(15628, 16, out fs);
  218. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00")));
  219. // Console.WriteLine();
  220. // Console.WriteLine("Read AutoTune D");
  221. // modbus.GetFloats(15660, 16, out fs);
  222. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00")));
  223. // Console.WriteLine();
  224. // Console.WriteLine("Read SetPoint");
  225. // modbus.GetFloats(15694, 16, out fs);
  226. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00") + "℃"));
  227. // Console.WriteLine();
  228. // Console.WriteLine("Read Active TuneSet");
  229. // us = modbus.GetUshort(15726, (ushort)16);
  230. // us!.Foreach(t => Console.WriteLine((ActiveTuneSet)t));
  231. // Console.WriteLine();
  232. // Console.WriteLine("Read Running P");
  233. // modbus.GetFloats(15758, 16, out fs);
  234. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00")));
  235. // Console.WriteLine();
  236. // Console.WriteLine("Read Running I");
  237. // modbus.GetFloats(15780, 16, out fs);
  238. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00")));
  239. // Console.WriteLine();
  240. // Console.WriteLine("Read Running D");
  241. // modbus.GetFloats(15822, 16, out fs);
  242. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00")));
  243. // Console.WriteLine();
  244. // Console.WriteLine("Read Up Rate");
  245. // modbus.GetFloats(15860, 16, out fs);
  246. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00") + "℃/Min"));
  247. // Console.WriteLine();
  248. // Console.WriteLine("Read Down Rate");
  249. // modbus.GetFloats(15892, 16, out fs);
  250. // fs!.Foreach(t => Console.WriteLine(t.ToString("0.00") + "℃/Min"));
  251. // Console.WriteLine();
  252. // Console.WriteLine("Read TC Broken");
  253. // modbus.GetBits(15692, out byte[]? bits);
  254. // bits!.For(0, 8, t => Console.WriteLine((TcBorken)t));
  255. // modbus.GetBits(15693, out bits);
  256. // bits!.For(0, 8, t => Console.WriteLine((TcBorken)t));
  257. // Console.WriteLine();
  258. // Console.WriteLine("Read Inhibit");
  259. // modbus.SetUshort(15854, 252);
  260. // modbus.SetUshort(15855, 255);
  261. // modbus.SetUshort(15855, 255);
  262. // modbus.GetBits(15854, out bits);
  263. // bits!.For(0, 8, t => Console.WriteLine((Active)t));
  264. // modbus.GetBits(15855, out bits);
  265. // bits!.For(0, 8, t => Console.WriteLine((Active)t));
  266. // Console.WriteLine();
  267. // Console.WriteLine("Read Auto Tune Active");
  268. // modbus.SetUshort(15858, 255);
  269. // modbus.SetUshort(15859, 255);
  270. // modbus.GetBits(15858, out bits);
  271. // bits!.For(0, 8, t => Console.WriteLine((Active)t));
  272. // modbus.GetBits(15859, out bits);
  273. // bits!.For(0, 8, t => Console.WriteLine((Active)t));
  274. // }
  275. //}
  276. class TestModbus_FluentModbus
  277. {
  278. public static void Test()
  279. {
  280. //IPAddress ipAddress = IPAddress.Parse("192.168.250.11");
  281. //IPEndPoint endPoint = new(ipAddress, 502);
  282. //ModbusTcpClient client = new();
  283. //client.Connect(endPoint);
  284. //Span<byte> t = client.ReadHoldingRegisters(0x00, 15500, 500);
  285. //Span<byte> t2 = client.ReadHoldingRegisters(0x00, 15564, 1);
  286. Modbus_Tcp client = new();
  287. client.Open("192.168.250.11", 502);
  288. byte[]? buffer = client.GetBuffer(4962, 32);
  289. //client.SetFloat(4962, 32.1f);
  290. client.SetUshort(4962, 1);
  291. buffer = client.GetBuffer(4962, 1);
  292. ushort s = BufferToUshort(buffer);
  293. }
  294. private static unsafe ushort BufferToUshort(byte[]? bytes)
  295. {
  296. if (bytes is null)
  297. return ushort.MaxValue;
  298. Span<byte> sb = bytes.AsSpan<byte>();
  299. sb.Reverse();
  300. fixed (byte* pByte = sb)
  301. {
  302. ushort* pResult = (ushort*)pByte;
  303. return *pResult;
  304. }
  305. }
  306. }
  307. enum TcBorken : byte
  308. {
  309. Normal = 0,
  310. Error
  311. }
  312. enum Active : byte
  313. {
  314. On = 0,
  315. Off = 1,
  316. }
  317. enum ActiveTuneSet : ushort
  318. {
  319. AutoTune = 0,
  320. Running
  321. }
  322. enum AutoTune : ushort
  323. {
  324. Unavailable = 0,
  325. Ready,
  326. Triggered,
  327. Running,
  328. Complete,
  329. Aborted,
  330. Timeout,
  331. Overflow
  332. }
  333. class YieldTest
  334. {
  335. public static IEnumerable<int> Test()
  336. {
  337. for (int i = 0; i < 10; i++)
  338. {
  339. if (i == 1)
  340. yield return -1;
  341. yield return i;
  342. }
  343. }
  344. }
  345. class ORMTest : IOrmProvider
  346. {
  347. public int Test1 { get; set; }
  348. public int Test2 { get; set; }
  349. public void Log(string log, DateTime dateTime, LogLevel logLevel)
  350. {
  351. Console.WriteLine($"{logLevel}-{dateTime:hh:mm:ss} {log}");
  352. }
  353. public async Task Test()
  354. {
  355. IORM orm = new SqlSugarCustom();
  356. orm.Initialize(this);
  357. orm.Open("Database=postgres;Password=123456;Host=localhost;Username=postgres;Persist Security Info=True", DbType.PostgreSQL);
  358. //orm.CreateDataBase("Minics");
  359. orm.CreateTable<ORMTest>("OrmTest1");
  360. //await orm.Insert<ORMTest>("OrmTest1", new() { Test1 = 1, Test2 = 1 });
  361. await orm.Query<ORMTest>("OrmTest1", Result);
  362. await orm.Query<ORMTest>("OrmTest1", t => t.Test1 == 1, Result);
  363. }
  364. void Result(List<ORMTest> oRMTests)
  365. {
  366. Console.WriteLine(oRMTests.Count);
  367. }
  368. }