Skip to content

Commit

Permalink
perf: 优化串口属性的配置
Browse files Browse the repository at this point in the history
Signed-off-by: Leven <levenchn@163.com>
  • Loading branch information
leven99 committed Oct 16, 2019
1 parent 2691b3a commit 02a6ec3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 46 deletions.
71 changes: 27 additions & 44 deletions Models/SerialPortM.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OSDA.ViewModels;
using System.Collections.ObjectModel;
using System.IO.Ports;
using System.Windows.Media;

Expand All @@ -7,10 +8,10 @@ namespace OSDA.Models
class SerialPortModel : MainWindowBase
{
public string[] SPPortItemsSource { get; set; }
public int[] SPBaudRateItemsSource { get; set; }
public int[] SPDataBitsItemsSource { get; set; }
public string[] SPStopBitsItemsSource { get; set; }
public string[] SPParityItemsSource { get; set; }
public Collection<int> SPBaudRateItemsSource { get; set; }
public Collection<int> SPDataBitsItemsSource { get; set; }
public Collection<StopBits> SPStopBitsItemsSource { get; set; }
public Collection<Parity> SPParityItemsSource { get; set; }

#region 串口配置区 - 串口属性
public string _SPPort;
Expand Down Expand Up @@ -64,8 +65,8 @@ public int SPDataBits
}
}

public string _SPStopBits;
public string SPStopBits
public StopBits _SPStopBits;
public StopBits SPStopBits
{
get
{
Expand All @@ -81,8 +82,8 @@ public string SPStopBits
}
}

public string _SPParity;
public string SPParity
public Parity _SPParity;
public Parity SPParity
{
get
{
Expand Down Expand Up @@ -494,48 +495,30 @@ public void SerialPort_PinChanged(object sender, SerialPinChangedEventArgs e)
}
#endregion

#region 停止位和校验位
public static StopBits GetStopBits(string emp)
{
StopBits stopBits = StopBits.One;
switch (emp)
{
case "One": stopBits = StopBits.One; break;
case "Two": stopBits = StopBits.Two; break;
case "OnePointFive": stopBits = StopBits.OnePointFive; break;
default: break;
}
return stopBits;
}

public static Parity GetParity(string emp)
{
Parity parity = Parity.None;
switch (emp)
{
case "None": parity = Parity.None; break;
case "Odd": parity = Parity.Odd; break;
case "Even": parity = Parity.Even; break;
case "Mark": parity = Parity.Mark; break;
case "Space": parity = Parity.Space; break;
default: break;
}
return parity;
}
#endregion

public void SerialPortDataContext()
{
SPPortItemsSource = SerialPort.GetPortNames();
SPBaudRateItemsSource = new int[] { 1200, 2400, 4800, 7200, 9600, 14400, 19200, 38400, 57600, 115200, 128000, 230400 };
SPDataBitsItemsSource = new int[] { 5, 6, 7, 8 };
SPStopBitsItemsSource = new string[] { "One", "Two", "OnePointFive" };
SPParityItemsSource = new string[] { "None", "Odd", "Even", "Mark", "Space" };
SPBaudRateItemsSource = new Collection<int>
{
1200, 2400, 4800, 7200, 9600, 14400, 19200, 38400, 57600, 115200, 128000, 230400
};
SPDataBitsItemsSource = new Collection<int>
{
5, 6, 7, 8
};
SPStopBitsItemsSource = new Collection<StopBits>
{
StopBits.One, StopBits.Two, StopBits.OnePointFive
};
SPParityItemsSource = new Collection<Parity>
{
Parity.None, Parity.Odd, Parity.Even, Parity.Mark, Parity.Space
};

SPBaudRate = 9600;
SPDataBits = 8;
SPStopBits = "One";
SPParity = "None";
SPStopBits = StopBits.One;
SPParity = Parity.None;

SPBrush = Brushes.Red;
OpenCloseSP = string.Format(cultureInfo, "打开串口");
Expand Down
4 changes: 2 additions & 2 deletions ViewModels/MainWindowVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,8 @@ public bool OpenSP()
SPserialPort.PortName = SerialPortModel.SPPort;
SPserialPort.BaudRate = SerialPortModel.SPBaudRate;
SPserialPort.DataBits = SerialPortModel.SPDataBits;
SPserialPort.StopBits = SerialPortModel.GetStopBits(SerialPortModel.SPStopBits.ToString(cultureInfo));
SPserialPort.Parity = SerialPortModel.GetParity(SerialPortModel.SPParity.ToString(cultureInfo));
SPserialPort.StopBits = SerialPortModel.SPStopBits;
SPserialPort.Parity = SerialPortModel.SPParity;

SPserialPort.WriteBufferSize = 1048576; /* 输出缓冲区的大小为1048576字节 = 1MB */
SPserialPort.ReadBufferSize = 2097152; /* 输入缓冲区的大小为2097152字节 = 2MB */
Expand Down

0 comments on commit 02a6ec3

Please sign in to comment.