如何在C#中通过串口发送数据? [英] How to send send data over serial with respect to time in C#?
问题描述
大家好
我在c#中通过串口发送和接收文本文件没有任何问题。但我想根据文本文件中的内存大小发送数据就像在1秒内发送1 kb,下一秒发送1 kb就像那样。
我正在为此工作从很久以后到目前为止还没有得到完美的解决方案
这是我的代码
private void butSetFile_Click(object sender,EventArgs e)
{
this.openFileDialog1.ShowDialog();
this.txtfile2.Text = this.openFileDialog1.FileName;
}
private void butSendFile_Click(object sender,EventArgs e)
{
if(serialPort1.IsOpen = = false)
{
this.serialPort1.Open();
}
serialPort1.Write(System.IO.File.ReadAllText(this.txtfile2.Text));
}
我的尝试:
我尝试过StremReader函数和Buffers。但没有得到我需要的结果任何人都可以帮助我
i使用此代码也
List< ;串GT; list = new List< string>();
使用(StreamReader reader = new StreamReader(this.txtfile2.Text))
{
string line;
while((line = reader.ReadLine())!= null)
{
list.Add(line); // 添加到列表。
}
}
只需阅读文件成一个字符串。然后你可以使用SubString(blockNumber * 1024,1024)
发送1 KB的部分,其中blockNumber
在写完之后递增阻止直到所有块都被发送。
程序流程:
全局对象:
string buffer;
int blockNumber = -1;
设置文件/点击发送文件:
buffer = System.IO.File.ReadAllText( this .openFileDialog1.FileName);
if (!buffer.IsNullOrEmpty())
{
if (serialPort1.IsOpen == false )
{
this .serialPort1。打开();
}
blockNumber = 0 ;
// 启动计时器
}
计时器事件函数:
if (blockNumber > = 0 )
{
string blockString = buffer.SubString(blockNumber * 1024 , 1024 );
// 如果所有数据都已发送,则为空
if (!blockString.IsNullOrEmpty())
{
serialPort1.Write(blockString);
blockNumber ++;
}
else
// OR:if(blockString.IsNullOrEmpty()|| blockString.Length()< 1024)
{
blockNumber = -1;
buffer = ;
// 停止计时器
}
}
Hi all
I am sending and receiving text files over serial port in c# without any problem . But i want to send data according the memory size in text file "like sending 1 kb in 1 sec , next 1 kb in next second like that ".
I am working on this from long back didn't get the perfect solution till now
Here is my code
private void butSetFile_Click(object sender, EventArgs e) { this.openFileDialog1.ShowDialog(); this.txtfile2.Text = this.openFileDialog1.FileName; } private void butSendFile_Click(object sender, EventArgs e) { if (serialPort1.IsOpen == false) { this.serialPort1.Open(); } serialPort1.Write(System.IO.File.ReadAllText(this.txtfile2.Text)); }
What I have tried:
I tried StremReader function and Buffers also. But didn't get the result what i need can any one help me
i used this code also
List<string> list = new List<string>(); using (StreamReader reader = new StreamReader(this.txtfile2.Text)) { string line; while ((line = reader.ReadLine()) != null) { list.Add(line); // Add to list.
}
}
Just read the file into a string. Then you can send 1 KB portions by usingSubString(blockNumber * 1024, 1024)
whereblockNumber
is incremented after writing each block until all blocks has been send.
Program flow:
Global objects:
string buffer; int blockNumber = -1;
Set File / click send file:
buffer = System.IO.File.ReadAllText(this.openFileDialog1.FileName); if (!buffer.IsNullOrEmpty()) { if (serialPort1.IsOpen == false) { this.serialPort1.Open(); } blockNumber = 0; // start timer }
Timer event function:
if (blockNumber >= 0) { string blockString = buffer.SubString(blockNumber * 1024, 1024); // Empty if all data has been send if (!blockString.IsNullOrEmpty()) { serialPort1.Write(blockString); blockNumber++; } else //OR: if (blockString.IsNullOrEmpty() || blockString.Length() < 1024) { blockNumber = -1; buffer = ""; // stop timer } }
这篇关于如何在C#中通过串口发送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!