从数字秤DLL文件C#同步更新权重 [英] Update weight synchronously from digital scale DLL file C#
问题描述
我已经构建了一个软件,可以从数字刻度中检索权重,并将其输出到WinForms中的标签中。每次重量在刻度上变化时,它也会在标签中自动更新。我成功了。
这是我的代码:
I have built a software which retrieve the weight from digital scale and output it in a label in WinForms. Every time the weight changes in the scale it is also automatically updated in the label. Successfully I made it.
This is my code:
public partial class Form1 : Form
{
private SerialPort port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
port.DtrEnable = true;
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
private void Form1_Load(object sender, EventArgs e)
{
port.Open();
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
this.Invoke(new EventHandler(DoUpdate));
}
catch (Exception ex)
{
}
}
private void DoUpdate(object s, EventArgs e)
{
try
{
label1.Text = port.ReadLine();
}
catch (Exception ex)
{
label1.Text = ex.ToString();
}
}
}
现在,我的老板让我在.DLL文件(类库)中实现此代码。他只想添加作为参考,调用类并返回权重并自动更新。
他只想在页面加载上进行简单的调用,如下例所示:
Now, my boss asked me to implement this code in a .DLL file (Class Library). He just wants to add as a reference, call the class and it returns the weight and also it updates automatically.
He just wants to make a simple call on Page Load like this sample:
Scale sc = new Scale();
label1.text = sc.weight();
我可以成功退货重量,但它不会通过类库自动更新,这是我在我的类库(.DLL文件)中使用的代码:
I can successfully return the weight but it does not updated automatically via the Class Library and this is the code I am using in my Class Library (.DLL file):
public class Scale
{
SerialPort port;
public string weight()
{
try
{
port = new SerialPort(com, 9600, Parity.None, 8, StopBits.One);
if (port.IsOpen == false)
{
port.Open();
}
port.DtrEnable = true;
return port.ReadLine();;
}
catch(Exception ex)
{
return "";
}
}
}
我希望我是解释清楚。有关代码的任何帮助吗?
提前致谢。
I hope I was explained clearly. Any help with the code?
Thanks in advance.
推荐答案
向您的Scale类添加一个事件 - 该类每个都发出一个事件信号重量变化的时间。
然后你的演示代码添加一个处理程序来更新标签:
Add an event to your Scale class - and the class signals the event each time the weight changes.
You then add a handler to your presentation code which updates the label:
Scale sc = new Scale();
sc.WeightChanged += Scale_WeightChanged;
...
void Scale_WeightChanged(object sender, EventArgs e)
{
Scale sc = sender as Scale;
if (sc != null)
{
label1.text = sc.weight();
}
}
添加活动很简单:一个简单的代码片段来添加一个事件 [ ^ ]显示代码以及更容易添加它们的方法将来。
Adding an event is easy: A Simple Code Snippet to Add an Event[^] shows the code and a way to make it easier to add them in future.
从你的dll中公开一个事件。每次调用数据SerialDataReceivedEventHandler
时,调用事件:
Expose an event from your dll. Every time the dataSerialDataReceivedEventHandler
is called, invoke the event:
public class Scale : IDisposable //This will mean that the Dispose method is called when the invokation is cleaned up
{
//It's ok to kep this up. We'll close it when this invokation is cleaned up
SerialPort port = new SerialPort("com", 9600, Parity.None, 8, StopBits.One);
public Scale()
{
port.DtrEnable = true;
port.DataReceived += OnWeightChanged;
}
//This dictates what the event will look like. It's often declared outside the class.
public delegate void ScaleWeightChangeHandler(object sender, string weight);
//Out event. This is what is exposed to the calling code (i.e. scale.WeightChanged += new ScaleWeightChangeHandler(fubar);
public event ScaleWeightChangeHandler WeightChanged;
//We call this to fire the event
private void OnWeightChanged(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
{
if (WeightChanged != null)
WeightChanged(sender, Weight());
}
//The Weight can still be retrieved manually (especially the first time)
public string Weight()
{
try
{
if (port.IsOpen == false)
{
port.Open();
}
port.DtrEnable = true;
return port.ReadLine();
}
catch (Exception ex)
{
return "";
}
}
//Cleanup the port on Dispose. This is the benefit of IDisposable
#region Corrected from comments below
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
port.DataReceived -= new SerialDataReceivedEventHandler(getPesha);
}
if (port != null)
{
if (port.IsOpen)
{
port.Close();
}
port.Dispose();
}
}
#endregion
}
(打赌我不是第一个解决方案,但它必须是最好的^ _ ^)
希望这有帮助
Andy
编辑:从下面的评论中纠正Dispose方法
(Bet I'm not the first solution response, but it's gotta be the best ^_^ )
Hope that helps
Andy
corrected the Dispose methods from comments below
这篇关于从数字秤DLL文件C#同步更新权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!