我有一个 WPF 应用程序,它连接到地磅以获取权重。
spWeigh = new SerialPort("COM1", 9600, Parity.Even, 7, StopBits.One);
spWeigh.RtsEnable = false;
spWeigh.DtrEnable = false;
spWeigh.Handshake = Handshake.None;
spWeigh.ReadTimeout = 10000;
spWeigh.DataReceived += spWeigh_DataReceived;
spWeigh.Write(((char)5).ToString());
void spWeigh_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
strResponseWeigh = spWeigh.ReadLine();
if (strResponseWeigh.Length == 0)
{
MessageBoxWrapper.Show("Error in communication with weighbridge", "Error");
return;
}
string wt = strResponseWeigh.Substring(15, 6);
}
我需要在不同的地磅上使用相同的应用程序。然后我需要更改地磅的代码,如下所示:
spWeigh = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
spWeigh.RtsEnable = false;
spWeigh.DtrEnable = false;
spWeigh.Handshake = Handshake.None;
spWeigh.ReadTimeout = 10000;
spWeigh.DataReceived += spWeigh_DataReceived;
void spWeigh_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
strResponseWeigh = spWeigh.ReadLine();
if (strResponseWeigh=="")
{
MessageBoxWrapper.Show("Error communicating with the weighbridge", "Error");
return;
}
//Some more checking are to be done here depending on the response(different from the first weighbridge type)
string wt = strResponseWeigh.Substring(2, 7);
}
是否可以使地磅部分通用?我们所做的就是向地磅发送一个(或多个)字符,获取响应,检查响应是否有效并读取权重。
是否可以做一个配置文件,以便我们根据地磅更改此文件中的某些值而不更改实际代码?