fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO.Ports;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Data;
  11. using System.Windows.Documents;
  12. using System.Windows.Input;
  13. using System.Windows.Media;
  14. using System.Windows.Media.Imaging;
  15. using System.Windows.Shapes;
  16. using WindowsInput;
  17.  
  18. namespace WpfMacroser
  19. {
  20. /// <summary>
  21. /// Логика взаимодействия для PMPSU.xaml
  22.  
  23.  
  24.  
  25. /// </summary>
  26. ///
  27.  
  28.  
  29. public partial class PMPSU : Window
  30. {
  31. int portState = 0;
  32. int LEDSkv = 0;
  33. int prLEDSkv = 0;
  34. string Data = "";
  35. string RawData = "";
  36. string NowKey = "";
  37. public PMPSU()
  38. {
  39. InitializeComponent();
  40.  
  41.  
  42. }
  43.  
  44. SerialPort SerialP = new SerialPort();
  45. InputSimulator InputSim = new InputSimulator();
  46. void OpenSerial()
  47. {
  48. try
  49. {
  50. SerialP.PortName = TBX_SerialPortName.Text;
  51. SerialP.BaudRate = 115200;
  52. SerialP.StopBits = StopBits.One;
  53. SerialP.Parity = Parity.None;
  54. SerialP.Open();
  55. }
  56. catch
  57. {
  58. Trace.WriteLine("Исключение. Не удается открыть порт");
  59. }
  60. finally
  61. {
  62. //#FFFF2C2C
  63. if(SerialP.IsOpen == true)
  64. {
  65. BTN_ConnectSerial.Background = BrushFromHex("#8fff4a");
  66. BTN_ConnectSerial.Content = "Закрыть";
  67. portState = 1;
  68. SendData();
  69. GetData();
  70. }
  71. }
  72. }
  73.  
  74. void CloseSerial()
  75. {
  76. Trace.WriteLine("CloseSerial init");
  77.  
  78.  
  79. try
  80. {
  81. cancelTokenSource.Cancel();
  82. portState = 0;
  83. Thread.Sleep(3000);
  84. Trace.WriteLine("Next hop SerialP.Close()");
  85. SerialP.Close();
  86. Thread.Sleep(3000);
  87. }
  88. catch
  89. {
  90. Trace.WriteLine("Исключение. Не удается закрыть порт");
  91. }
  92. finally
  93. {
  94. //#FFFF2C2C
  95. if (SerialP.IsOpen == false)
  96. {
  97. Trace.WriteLine("CloseSerial IsOpen=false");
  98. BTN_ConnectSerial.Background = BrushFromHex("#FFFF2C2C");
  99. BTN_ConnectSerial.Content = "Открыть";
  100.  
  101. }
  102. }
  103. }
  104.  
  105. void SendRawInput(string text)
  106. {
  107. if(SerialP.IsOpen == true)
  108. {
  109. SerialP.WriteLine(text);
  110. }
  111. }
  112.  
  113. public SolidColorBrush BrushFromHex(string hexColorString)
  114. {
  115. return (SolidColorBrush)(new BrushConverter().ConvertFrom(hexColorString));
  116. }
  117.  
  118. private void BTN_ConnectSerial_Toggle(object sender, RoutedEventArgs e)
  119. {
  120. if (portState == 0) OpenSerial();
  121. else if (portState == 1) CloseSerial();
  122. }
  123.  
  124. private void BTN_SendRawInput_Click(object sender, RoutedEventArgs e)
  125. {
  126. SendRawInput(TBX_RawInput.Text);
  127. TBX_RawInput.Text = "";
  128. }
  129.  
  130. private void SL_Led1_skv_Changed(object sender, RoutedPropertyChangedEventArgs<double> e)
  131. {
  132. LEDSkv = Convert.ToInt16(SL_Led1_skv.Value);
  133. LEDSkv = Convert.ToInt16(LEDSkv*2.55);
  134. TBL_Led1_Indicator.Text = (LEDSkv.ToString());
  135. }
  136.  
  137. public async void SendData()
  138. {
  139. await Task.Run(() =>
  140. {
  141.  
  142. if (SerialP.IsOpen == true)
  143. {
  144. while (portState == 1)
  145. {
  146. if(LEDSkv != prLEDSkv)
  147. {
  148. if(SerialP.IsOpen == true) SerialP.Write("A" + LEDSkv.ToString());
  149. prLEDSkv = LEDSkv;
  150. }
  151. Thread.Sleep(100);
  152. }
  153. }
  154.  
  155. });
  156. }
  157.  
  158. CancellationTokenSource cancelTokenSource = new CancellationTokenSource();
  159. public async void GetData()
  160. {
  161. CancellationToken token = cancelTokenSource.Token;
  162. await Task.Run(() =>
  163. {
  164.  
  165.  
  166. while (portState == 1)
  167. {
  168. if (token.IsCancellationRequested) // проверяем наличие сигнала отмены задачи
  169. {
  170. Trace.WriteLine("Операция прервана");
  171. return; // выходим из метода и тем самым завершаем задачу
  172. }
  173. if (SerialP.IsOpen == true | portState == 1)
  174. {
  175. Trace.WriteLine("GetData while init portState IS 1");
  176. NowKey = "";
  177. Trace.WriteLine("GetData SerialP.IsOpen == true | portState == 1");
  178. try
  179. {
  180. //string message = SerialP.ReadLine();
  181. RawData = SerialP.ReadLine();
  182. }
  183. catch (TimeoutException) { }
  184. Trace.WriteLine("SERIAL OPEN");
  185. if (RawData != null & RawData != "")
  186. {
  187. NowKey = RawData.Substring(0, 2);
  188. Data = RawData.Substring(2);
  189. }
  190.  
  191. if (NowKey == "BA") PRC_BTN1(Data);
  192. if (NowKey == "BB") PRC_BTN2(Data);
  193. if (NowKey == "BC") PRC_BTN3(Data);
  194. if (NowKey == "BD") PRC_BTN4(Data);
  195. if (NowKey == "PA") PRC_PA(Data);
  196. if (NowKey == "SA") PRC_SA(Data);
  197. if (NowKey == "SB") PRC_SB(Data);
  198. Thread.Sleep(100);
  199.  
  200. }
  201. }
  202. },token);
  203. }
  204. void PRC_BTN1(string localData)
  205. {
  206. int localDataInt = Convert.ToInt32(localData);
  207. if( localDataInt == 1)
  208. {
  209. Dispatcher.Invoke((Action)(() =>
  210. {
  211. TBL_BTN1.Background = BrushFromHex("#8fff4a");
  212.  
  213. }));
  214. }
  215. else if (localDataInt == 0)
  216. {
  217. Dispatcher.Invoke((Action)(() =>
  218. {
  219. TBL_BTN1.Background = BrushFromHex("#ffffff");
  220. }));
  221. }
  222. }
  223. void PRC_BTN2(string localData)
  224. {
  225. int localDataInt = Convert.ToInt32(localData);
  226. if (localDataInt == 1)
  227. {
  228. Dispatcher.Invoke((Action)(() =>
  229. {
  230. TBL_BTN2.Background = BrushFromHex("#8fff4a");
  231. }));
  232. }
  233. else if (localDataInt == 0)
  234. {
  235. Dispatcher.Invoke((Action)(() =>
  236. {
  237. TBL_BTN2.Background = BrushFromHex("#ffffff");
  238. }));
  239. }
  240. }
  241. void PRC_BTN3(string localData)
  242. {
  243. int localDataInt = Convert.ToInt32(localData);
  244. if (localDataInt == 1)
  245. {
  246. Dispatcher.Invoke((Action)(() =>
  247. {
  248. TBL_BTN3.Background = BrushFromHex("#8fff4a");
  249. }));
  250. }
  251. else if (localDataInt == 0)
  252. {
  253. Dispatcher.Invoke((Action)(() =>
  254. {
  255. TBL_BTN3.Background = BrushFromHex("#ffffff");
  256. }));
  257. }
  258. }
  259. void PRC_BTN4(string localData)
  260. {
  261. int localDataInt = Convert.ToInt32(localData);
  262. if (localDataInt == 1)
  263. {
  264. Dispatcher.Invoke((Action)(() =>
  265. {
  266. TBL_BTN4.Background = BrushFromHex("#8fff4a");
  267. }));
  268. }
  269. else if (localDataInt == 0)
  270. {
  271. Dispatcher.Invoke((Action)(() =>
  272. {
  273. TBL_BTN4.Background = BrushFromHex("#ffffff");
  274. }));
  275. }
  276. }
  277. void PRC_PA(string localData)
  278. {
  279. Dispatcher.Invoke((Action)(() =>
  280. {
  281. int localDataInt = Convert.ToInt32(localData);
  282. TBL_SensorResistor.Text = localData;
  283. }));
  284. }
  285. void PRC_SA(string localData)
  286. {
  287. Dispatcher.Invoke((Action)(() =>
  288. {
  289. int localDataInt = Convert.ToInt32(localData);
  290. if (localDataInt == 1) TBL_DR1_1.Background = BrushFromHex("#8fff4a");
  291. else if (localDataInt == 0) TBL_DR1_1.Background = BrushFromHex("#ffffff");
  292. }));
  293. }
  294. void PRC_SB(string localData)
  295. {
  296. Dispatcher.Invoke((Action)(() =>
  297. {
  298. int localDataInt = Convert.ToInt32(localData);
  299. if (localDataInt == 1) TBL_DR1_2.Background = BrushFromHex("#8fff4a");
  300. else if (localDataInt == 0) TBL_DR1_2.Background = BrushFromHex("#ffffff");
  301. }));
  302. }
  303.  
  304. }
  305. }
  306.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Welcome to .NET 6.0!
---------------------
SDK Version: 6.0.100

----------------
Installed an ASP.NET Core HTTPS development certificate.
To trust the certificate run 'dotnet dev-certs https --trust' (Windows and macOS only).
Learn about HTTPS: https://aka.ms/dotnet-https
----------------
Write your first app: https://aka.ms/dotnet-hello-world
Find out what's new: https://aka.ms/dotnet-whats-new
Explore documentation: https://aka.ms/dotnet-docs
Report issues and find source on GitHub: https://github.com/dotnet/core
Use 'dotnet --help' to see available commands or visit: https://aka.ms/dotnet-cli
--------------------------------------------------------------------------------------
Package source with Name: nuget.org disabled successfully.
Package source with Name: local added successfully.
/home/xHLc1k/Project/Program.cs(9,22): error CS0234: The type or namespace name 'Controls' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(10,22): error CS0234: The type or namespace name 'Data' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(11,22): error CS0234: The type or namespace name 'Documents' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(13,22): error CS0234: The type or namespace name 'Media' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(14,22): error CS0234: The type or namespace name 'Media' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(15,22): error CS0234: The type or namespace name 'Shapes' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(16,7): error CS0246: The type or namespace name 'WindowsInput' could not be found (are you missing a using directive or an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(29,34): error CS0246: The type or namespace name 'Window' could not be found (are you missing a using directive or an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(113,16): error CS0246: The type or namespace name 'SolidColorBrush' could not be found (are you missing a using directive or an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(118,62): error CS0246: The type or namespace name 'RoutedEventArgs' could not be found (are you missing a using directive or an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(124,60): error CS0246: The type or namespace name 'RoutedEventArgs' could not be found (are you missing a using directive or an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(130,57): error CS0246: The type or namespace name 'RoutedPropertyChangedEventArgs<>' could not be found (are you missing a using directive or an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(44,9): error CS1069: The type name 'SerialPort' could not be found in the namespace 'System.IO.Ports'. This type has been forwarded to assembly 'System.IO.Ports, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' Consider adding a reference to that assembly. [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(45,9): error CS0246: The type or namespace name 'InputSimulator' could not be found (are you missing a using directive or an assembly reference?) [/home/xHLc1k/Project/Project.csproj]



/home/xHLc1k/Project/Program.cs(9,22): error CS0234: The type or namespace name 'Controls' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(10,22): error CS0234: The type or namespace name 'Data' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(11,22): error CS0234: The type or namespace name 'Documents' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(13,22): error CS0234: The type or namespace name 'Media' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(14,22): error CS0234: The type or namespace name 'Media' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(15,22): error CS0234: The type or namespace name 'Shapes' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(16,7): error CS0246: The type or namespace name 'WindowsInput' could not be found (are you missing a using directive or an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(29,34): error CS0246: The type or namespace name 'Window' could not be found (are you missing a using directive or an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(113,16): error CS0246: The type or namespace name 'SolidColorBrush' could not be found (are you missing a using directive or an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(118,62): error CS0246: The type or namespace name 'RoutedEventArgs' could not be found (are you missing a using directive or an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(124,60): error CS0246: The type or namespace name 'RoutedEventArgs' could not be found (are you missing a using directive or an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(130,57): error CS0246: The type or namespace name 'RoutedPropertyChangedEventArgs<>' could not be found (are you missing a using directive or an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(44,9): error CS1069: The type name 'SerialPort' could not be found in the namespace 'System.IO.Ports'. This type has been forwarded to assembly 'System.IO.Ports, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' Consider adding a reference to that assembly. [/home/xHLc1k/Project/Project.csproj]
/home/xHLc1k/Project/Program.cs(45,9): error CS0246: The type or namespace name 'InputSimulator' could not be found (are you missing a using directive or an assembly reference?) [/home/xHLc1k/Project/Project.csproj]
    0 Warning(s)
    14 Error(s)

Time Elapsed 00:00:05.00
stdout
Standard output is empty