Program.cs 13 KB

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