using System; using System.Collections.Generic; using System.Diagnostics; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using WindowsInput; namespace WpfMacroser { /// <summary> /// Логика взаимодействия для PMPSU.xaml /// </summary> /// public partial class PMPSU : Window { int portState = 0; int LEDSkv = 0; int prLEDSkv = 0; string Data = ""; string RawData = ""; string NowKey = ""; public PMPSU() { InitializeComponent(); } SerialPort SerialP = new SerialPort(); InputSimulator InputSim = new InputSimulator(); void OpenSerial() { try { SerialP.PortName = TBX_SerialPortName.Text; SerialP.BaudRate = 115200; SerialP.StopBits = StopBits.One; SerialP.Parity = Parity.None; SerialP.Open(); } catch { Trace.WriteLine("Исключение. Не удается открыть порт"); } finally { //#FFFF2C2C if(SerialP.IsOpen == true) { BTN_ConnectSerial.Background = BrushFromHex("#8fff4a"); BTN_ConnectSerial.Content = "Закрыть"; portState = 1; SendData(); GetData(); } } } void CloseSerial() { Trace.WriteLine("CloseSerial init"); try { cancelTokenSource.Cancel(); portState = 0; Thread.Sleep(3000); Trace.WriteLine("Next hop SerialP.Close()"); SerialP.Close(); Thread.Sleep(3000); } catch { Trace.WriteLine("Исключение. Не удается закрыть порт"); } finally { //#FFFF2C2C if (SerialP.IsOpen == false) { Trace.WriteLine("CloseSerial IsOpen=false"); BTN_ConnectSerial.Background = BrushFromHex("#FFFF2C2C"); BTN_ConnectSerial.Content = "Открыть"; } } } void SendRawInput(string text) { if(SerialP.IsOpen == true) { SerialP.WriteLine(text); } } public SolidColorBrush BrushFromHex(string hexColorString) { return (SolidColorBrush)(new BrushConverter().ConvertFrom(hexColorString)); } private void BTN_ConnectSerial_Toggle(object sender, RoutedEventArgs e) { if (portState == 0) OpenSerial(); else if (portState == 1) CloseSerial(); } private void BTN_SendRawInput_Click(object sender, RoutedEventArgs e) { SendRawInput(TBX_RawInput.Text); TBX_RawInput.Text = ""; } private void SL_Led1_skv_Changed(object sender, RoutedPropertyChangedEventArgs<double> e) { LEDSkv = Convert.ToInt16(SL_Led1_skv.Value); LEDSkv = Convert.ToInt16(LEDSkv*2.55); TBL_Led1_Indicator.Text = (LEDSkv.ToString()); } public async void SendData() { await Task.Run(() => { if (SerialP.IsOpen == true) { while (portState == 1) { if(LEDSkv != prLEDSkv) { if(SerialP.IsOpen == true) SerialP.Write("A" + LEDSkv.ToString()); prLEDSkv = LEDSkv; } Thread.Sleep(100); } } }); } CancellationTokenSource cancelTokenSource = new CancellationTokenSource(); public async void GetData() { CancellationToken token = cancelTokenSource.Token; await Task.Run(() => { while (portState == 1) { if (token.IsCancellationRequested) // проверяем наличие сигнала отмены задачи { Trace.WriteLine("Операция прервана"); return; // выходим из метода и тем самым завершаем задачу } if (SerialP.IsOpen == true | portState == 1) { Trace.WriteLine("GetData while init portState IS 1"); NowKey = ""; Trace.WriteLine("GetData SerialP.IsOpen == true | portState == 1"); try { //string message = SerialP.ReadLine(); RawData = SerialP.ReadLine(); } catch (TimeoutException) { } Trace.WriteLine("SERIAL OPEN"); if (RawData != null & RawData != "") { NowKey = RawData.Substring(0, 2); Data = RawData.Substring(2); } if (NowKey == "BA") PRC_BTN1(Data); if (NowKey == "BB") PRC_BTN2(Data); if (NowKey == "BC") PRC_BTN3(Data); if (NowKey == "BD") PRC_BTN4(Data); if (NowKey == "PA") PRC_PA(Data); if (NowKey == "SA") PRC_SA(Data); if (NowKey == "SB") PRC_SB(Data); Thread.Sleep(100); } } },token); } void PRC_BTN1(string localData) { int localDataInt = Convert.ToInt32(localData); if( localDataInt == 1) { Dispatcher.Invoke((Action)(() => { TBL_BTN1.Background = BrushFromHex("#8fff4a"); })); } else if (localDataInt == 0) { Dispatcher.Invoke((Action)(() => { TBL_BTN1.Background = BrushFromHex("#ffffff"); })); } } void PRC_BTN2(string localData) { int localDataInt = Convert.ToInt32(localData); if (localDataInt == 1) { Dispatcher.Invoke((Action)(() => { TBL_BTN2.Background = BrushFromHex("#8fff4a"); })); } else if (localDataInt == 0) { Dispatcher.Invoke((Action)(() => { TBL_BTN2.Background = BrushFromHex("#ffffff"); })); } } void PRC_BTN3(string localData) { int localDataInt = Convert.ToInt32(localData); if (localDataInt == 1) { Dispatcher.Invoke((Action)(() => { TBL_BTN3.Background = BrushFromHex("#8fff4a"); })); } else if (localDataInt == 0) { Dispatcher.Invoke((Action)(() => { TBL_BTN3.Background = BrushFromHex("#ffffff"); })); } } void PRC_BTN4(string localData) { int localDataInt = Convert.ToInt32(localData); if (localDataInt == 1) { Dispatcher.Invoke((Action)(() => { TBL_BTN4.Background = BrushFromHex("#8fff4a"); })); } else if (localDataInt == 0) { Dispatcher.Invoke((Action)(() => { TBL_BTN4.Background = BrushFromHex("#ffffff"); })); } } void PRC_PA(string localData) { Dispatcher.Invoke((Action)(() => { int localDataInt = Convert.ToInt32(localData); TBL_SensorResistor.Text = localData; })); } void PRC_SA(string localData) { Dispatcher.Invoke((Action)(() => { int localDataInt = Convert.ToInt32(localData); if (localDataInt == 1) TBL_DR1_1.Background = BrushFromHex("#8fff4a"); else if (localDataInt == 0) TBL_DR1_1.Background = BrushFromHex("#ffffff"); })); } void PRC_SB(string localData) { Dispatcher.Invoke((Action)(() => { int localDataInt = Convert.ToInt32(localData); if (localDataInt == 1) TBL_DR1_2.Background = BrushFromHex("#8fff4a"); else if (localDataInt == 0) TBL_DR1_2.Background = BrushFromHex("#ffffff"); })); } } }
Standard input is empty
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
Standard output is empty