You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
6.5 KiB
C#
198 lines
6.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using System.Xml;
|
|
|
|
namespace Mesnac.Equips.Connection.COM
|
|
{
|
|
public partial class SetConfigForm : Form, Mesnac.Equips.Connection.ConnConfig
|
|
{
|
|
#region 单例模式
|
|
private static SetConfigForm _this;
|
|
public static SetConfigForm Instance
|
|
{
|
|
get
|
|
{
|
|
if (null == _this)
|
|
_this = new SetConfigForm();
|
|
return _this;
|
|
}
|
|
}
|
|
#endregion
|
|
public SetConfigForm()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
ConnType thisConnType = new ConnType();
|
|
private void button1_Click(object sender, EventArgs e)
|
|
{
|
|
this.thisConnType.StationNum = this.cmbStationNum.Text;
|
|
this.thisConnType.PortName = this.cmbPortName.Text;
|
|
this.thisConnType.BaudRate = Convert.ToInt32(this.txtBaudRate.Text);
|
|
this.thisConnType.Parity = this.cmbParity.Text;
|
|
this.thisConnType.DataBits = Convert.ToInt32(this.cmbDataBits.Text);
|
|
this.thisConnType.StopBits = this.cmbStopBits.Text;
|
|
this.thisConnType.BuffSize = Convert.ToInt32(this.txtBuffSize.Text);
|
|
this.Close();
|
|
}
|
|
|
|
#region SetConfig GetConfig
|
|
public Connection.ConnType SetConfig(Connection.ConnType connType)
|
|
{
|
|
ConnType Result = new ConnType();
|
|
if (connType != null)
|
|
{
|
|
Result = (ConnType)connType;
|
|
}
|
|
this.cmbStationNum.Text = Result.StationNum;
|
|
this.cmbPortName.Text = Result.PortName;
|
|
this.txtBaudRate.Text = Result.BaudRate.ToString();
|
|
this.cmbParity.Text = Result.Parity;
|
|
this.cmbDataBits.Text = Result.DataBits.ToString();
|
|
this.cmbStopBits.Text = Result.StopBits;
|
|
this.txtBuffSize.Text = Result.BuffSize.ToString();
|
|
this.ShowDialog();
|
|
return thisConnType;
|
|
}
|
|
|
|
private bool GetFirstNodeValue(XmlNode node, string path, string attribute, bool throwException, out string value)
|
|
{
|
|
value = string.Empty;
|
|
try
|
|
{
|
|
value = node.SelectNodes(path)[0].Attributes[attribute.ToLower()].Value.ToString();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (throwException)
|
|
{
|
|
throw (ex);
|
|
}
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
private bool GetNodeAttributeValue(XmlNode node, string attribute, bool throwException, out string value)
|
|
{
|
|
value = string.Empty;
|
|
try
|
|
{
|
|
value = node.Attributes[attribute.ToLower()].Value.ToString();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (throwException)
|
|
{
|
|
throw (ex);
|
|
}
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
public Connection.ConnType GetConfig(XmlNode node)
|
|
{
|
|
ConnType Result = new ConnType();
|
|
|
|
string stationNum = "00";
|
|
string portName = "COM1";
|
|
string baudRate = "9600";
|
|
string parity = "Even";
|
|
string dataBits = "7";
|
|
string stopBits = "2";
|
|
string buffSize = "1024";
|
|
GetFirstNodeValue(node, ".//StationNum[@value]", "value", true, out stationNum);
|
|
GetFirstNodeValue(node, ".//PortName[@value]", "value", true, out portName);
|
|
GetFirstNodeValue(node, ".//BaudRate[@value]", "value", true, out baudRate);
|
|
GetFirstNodeValue(node, ".//Parity[@value]", "value", true, out parity);
|
|
GetFirstNodeValue(node, ".//DataBits[@value]", "value", true, out dataBits);
|
|
GetFirstNodeValue(node, ".//StopBits[@value]", "value", true, out stopBits);
|
|
GetFirstNodeValue(node, ".//BuffSize[@value]", "value", true, out buffSize);
|
|
Result.StationNum = stationNum;
|
|
Result.PortName = portName;
|
|
Result.BaudRate = Convert.ToInt32(baudRate);
|
|
Result.Parity = parity;
|
|
Result.DataBits = Convert.ToInt32(dataBits);
|
|
Result.StopBits = stopBits;
|
|
Result.BuffSize = Convert.ToInt32(buffSize);
|
|
return Result;
|
|
}
|
|
#endregion
|
|
}
|
|
public class ConnType : Mesnac.Equips.Connection.ConnType
|
|
{
|
|
private string _stationNum = "00"; //站号
|
|
private string _portName = "COM1"; //端口号
|
|
private int _baudRate = 9600; //波特率
|
|
private string _parity = "Even"; //校验位
|
|
private int _dataBits = 7; //数据位
|
|
private string _stopBits = "2"; //停止位
|
|
private int _buffSize = 1024; //缓冲区大小
|
|
|
|
public ConnType()
|
|
{
|
|
}
|
|
/// <summary>
|
|
/// 站号
|
|
/// </summary>
|
|
public string StationNum
|
|
{
|
|
get { return _stationNum; }
|
|
set { _stationNum = value; }
|
|
}
|
|
/// <summary>
|
|
/// 端口号
|
|
/// </summary>
|
|
public string PortName
|
|
{
|
|
get { return _portName; }
|
|
set { _portName = value; }
|
|
}
|
|
/// <summary>
|
|
/// 波特率
|
|
/// </summary>
|
|
public int BaudRate
|
|
{
|
|
get { return _baudRate; }
|
|
set { _baudRate = value; }
|
|
}
|
|
/// <summary>
|
|
/// 校验位
|
|
/// </summary>
|
|
public string Parity
|
|
{
|
|
get { return _parity; }
|
|
set { _parity = value; }
|
|
}
|
|
/// <summary>
|
|
/// 数据位
|
|
/// </summary>
|
|
public int DataBits
|
|
{
|
|
get { return _dataBits; }
|
|
set { _dataBits = value; }
|
|
}
|
|
/// <summary>
|
|
/// 停止位
|
|
/// </summary>
|
|
public string StopBits
|
|
{
|
|
get { return _stopBits; }
|
|
set { _stopBits = value; }
|
|
}
|
|
/// <summary>
|
|
/// 缓冲区大小
|
|
/// </summary>
|
|
public int BuffSize
|
|
{
|
|
get { return _buffSize; }
|
|
set { _buffSize = value; }
|
|
}
|
|
}
|
|
}
|