我们可以根据C#中串口的时间发送文件吗? [英] Can we send files according to time over serial port in C#?
本文介绍了我们可以根据C#中串口的时间发送文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了一个简单的GUI,通过串口发送和接收文件,我成功发送和接收文件,但我需要的是我们可以在每1秒或2秒或任何时间间隔后发送相同的文件。
任何人都可以解释如何添加时间戳
我尝试过:
I created a simple GUI which send and receive file over serial port, i successfully sent and receive the files , but what i need is can we send the same file after every 1 sec or 2 sec or any time interval.
can any one explain how to add time stamp
What I have tried:
private void butSendFile_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen == false)
{
this.serialPort1.Open();
}
serialPort1.Write(System.IO.File.ReadAllText(this.txtfile.Text));
}
private void butSetFile_Click(object sender, EventArgs e)
{
this.openFileDialog1.ShowDialog();
this.txtfile.Text = this.openFileDialog1.FileName;
}
这是我用来发送文件的代码
This is the code i used to send files
推荐答案
你真的有文本文件吗?或者您的应用程序中是否有数据首先写入文本文件然后应该通过ComPort发送?
如果你想在每一行中都有一个时间戳您的文本文件只有一个选择:
- 将您的文本文件读入Stringarray
- 向此数组的每个元素添加时间戳
- 使用Comport发送所有行
如果你想做那个循环,你应该在使用定时器之后看看(例如谷歌)。
但你应该知道以下内容:如果你的周期太快,你很容易过度充电你的Comport ......
Do you really have a Textfile ? Or do you have data inside your application which first is written into a Textfile and then should be send via ComPort ?
If you want to have a Timestamp in each line of your Textfile you have only one choice :
- read your Textfile into a Stringarray
- add to each element of this Array the Timestamp
- send all the lines with the Comport
If you want to do that cyclic you should look (with Google for example) after the use of a Timer.
But you should know the following : if you cycle is too fast you could easily overcharge your Comport ...
你可以创建一个计时器并从 Timer.Elapsed Event(System.Timers) [ ^ 。请参阅上面链接中的示例代码。
You can create a timer and send from within the Timer.Elapsed Event (System.Timers)[^]. See the example code at the above link.
这篇关于我们可以根据C#中串口的时间发送文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文