diff --git a/SunlightCentralizedControlManagement_SCCM_.csproj b/SunlightCentralizedControlManagement_SCCM_.csproj index f37aacf..c1f8523 100644 --- a/SunlightCentralizedControlManagement_SCCM_.csproj +++ b/SunlightCentralizedControlManagement_SCCM_.csproj @@ -497,7 +497,10 @@ 4.3.0 - 3.0.9 + 3.1.16 + + + 3.1.16 diff --git a/ViewModel/MainWindowViewModel.cs b/ViewModel/MainWindowViewModel.cs index 345b5cc..5ec834d 100644 --- a/ViewModel/MainWindowViewModel.cs +++ b/ViewModel/MainWindowViewModel.cs @@ -164,7 +164,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel port1.StopBits = StopBits.One; port1.ReadTimeout = 500; port1.WriteTimeout = 500; + port1.Encoding = Encoding.GetEncoding("ASCII"); port1.Open();//打开串口 + port1.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived); UPort1 = true; } catch (Exception e) @@ -184,7 +186,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel port2.StopBits = StopBits.One; port2.ReadTimeout = 500; port2.WriteTimeout = 500; + port2.Encoding = Encoding.GetEncoding("ASCII"); port2.Open();//打开串口 + port2.DataReceived += new SerialDataReceivedEventHandler(SerialPort2_DataReceived); UPort2 = true; } catch (Exception e) @@ -204,7 +208,9 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel port3.StopBits = StopBits.One; port3.ReadTimeout = 500; port3.WriteTimeout = 500; + port3.Encoding = Encoding.GetEncoding("ASCII"); port3.Open();//打开串口 + port3.DataReceived += new SerialDataReceivedEventHandler(SerialPort3_DataReceived); UPort3 = true; } catch (Exception e) @@ -213,6 +219,34 @@ namespace SunlightCentralizedControlManagement_SCCM_.ViewModel LogGing.LogGingDATA(e.Message); } }//ck + // 事件处理函数 + private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) + { + int bytesToRead = port1.BytesToRead; + byte[] buffer = new byte[bytesToRead]; + port1.Read(buffer, 0, bytesToRead); // 读取数据 + + // 将字节数据转换为字符串(注意编码方式) + string receivedData = Encoding.ASCII.GetString(buffer); // 根据实际情况调整编码 + }// 事件处理函数 + private void SerialPort2_DataReceived(object sender, SerialDataReceivedEventArgs e) + { + int bytesToRead = port2.BytesToRead; + byte[] buffer = new byte[bytesToRead]; + port2.Read(buffer, 0, bytesToRead); // 读取数据 + + // 将字节数据转换为字符串(注意编码方式) + string receivedData = Encoding.ASCII.GetString(buffer); // 根据实际情况调整编码 + }// 事件处理函数 + private void SerialPort3_DataReceived(object sender, SerialDataReceivedEventArgs e) + { + int bytesToRead = port3.BytesToRead; + byte[] buffer = new byte[bytesToRead]; + port3.Read(buffer, 0, bytesToRead); // 读取数据 + + // 将字节数据转换为字符串(注意编码方式) + string receivedData = Encoding.ASCII.GetString(buffer); // 根据实际情况调整编码 + } private async void Port_link() { await Task.Run(() =>