如何在c#中延迟串口写入1秒 [英] How to delay the serial port write for 1second in c#
本文介绍了如何在c#中延迟串口写入1秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码中,我必须处理串口写入,应该延迟1秒钟。
我的代码如下。
while (bytesRd < byteExpctd)
{
bytesRd + = serialPort.Read(Req,bytesRd,byteExpctd - bytesRd);
// serialPort.ReadTimeout = 80;
}
byteExpctd =( int )Req [ 1 ];
while (bytesRd < byteExpctd)
{
bytesRd + = serialPort.Read(Req,bytesRd,byteExpctd - bytesRd);
}
if (Req [ 0 ] == 0x14&& Req [ 0 ] == 0x06)
{
AckMsg [ 0 ] = 0x0B;
AckMsg [ 1 ] = 0x06;
AckMsg [ 2 ] = 0xA4;
AckMsg [ 3 ] = CheckSum(AckMsg);
serialPort.Write(AckMsg, 0 , 5 );
}
其他
{
// < span class =code-comment>某事的代码
}
Thread.sleep( 1000 );
RsltMsg [ 0 ] = 0x9F;
RsltMsg [ 1 ] = 0x05;
RsltMsg [ 2 ] = 0x19;
RsltMsg [ 3 ] = CheckSum(RsltMsg);
serialPort.Write(RsltMsg, 0 , 3 );
如何在c#中延迟串口写入1秒钟?我必须在第一次和第二次写入之间引入1分钟的延迟。我试过Thread.Sleep(),但它没有做任何改变。是否有任何替代解决方案来引入1分钟的延迟。我使用的自定义波特率为10,4K。请帮助。
解决方案
Thread.Sleep(1000);
在这里应该可以正常工作。试一下,看看它是怎么回事。Thread.Sleep(1000 * 60);
否则你可以查看计时器。
http://msdn.microsoft.com /en-us/library/system.timers.timer%28v=vs.110%29.aspx [ ^ ]
祝你好运!
Hi,
In my code i have to handle the serial port write, which should be delayed for 1 second.
My code is as follows.
while (bytesRd < byteExpctd)
{
bytesRd += serialPort.Read(Req, bytesRd, byteExpctd - bytesRd);
//serialPort.ReadTimeout = 80;
}
byteExpctd = (int)Req[1];
while (bytesRd < byteExpctd)
{
bytesRd += serialPort.Read(Req, bytesRd, byteExpctd - bytesRd);
}
if (Req[0] == 0x14 && Req[0] == 0x06)
{
AckMsg[0] = 0x0B;
AckMsg[1] = 0x06;
AckMsg[2] = 0xA4;
AckMsg[3] = CheckSum(AckMsg);
serialPort.Write(AckMsg, 0, 5);
}
else
{
// code for something
}
Thread.sleep(1000);
RsltMsg[0] = 0x9F;
RsltMsg[1] = 0x05;
RsltMsg[2] = 0x19;
RsltMsg[3] = CheckSum(RsltMsg);
serialPort.Write(RsltMsg, 0, 3);
How to delay the serial port write for 1 second in c#? I have to introduce a delay of 1 minute between the first and second write. I tried with Thread.Sleep(), but it doesn't make any change. Is there any alternate solution to introduce the delay of 1 minute. I am using custom baud rate of 10,4K. Please help.
解决方案
Thread.Sleep(1000);
should work fine here. Try a minute and see how it goes.Thread.Sleep(1000 * 60);
Otherwise you could check out timers.
http://msdn.microsoft.com/en-us/library/system.timers.timer%28v=vs.110%29.aspx[^]
Good luck!
这篇关于如何在c#中延迟串口写入1秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!