private void cycle()
{
for (i=0;i<=100;i++)
{
....
timer1.Interval = 5000;
timer1.start();
}
}private void timer1_Tick(object sender, EventArgs e)
{
if (满足某种条件)
???
}
我希望的是,满足条件进行下一步循环,或时间到也进行下一步循环,应该怎么写呢?
{
for (i=0;i<=100;i++)
{
....
timer1.Interval = 5000;
timer1.start();
}
}private void timer1_Tick(object sender, EventArgs e)
{
if (满足某种条件)
???
}
我希望的是,满足条件进行下一步循环,或时间到也进行下一步循环,应该怎么写呢?
解决方案 »
- 如何将list〈byte〉型的变量中的数值以16进制的数据类型传给一个字符串?
- 用MODI.Document做OCR的一些问题
- 什么是Entity Inheritance(映射继承层次结构 (LINQ to SQL))
- 关于datagridview移动问题
- 用C#连接数据库时出现的问题
- 使用C#将SQLSERVER数据库表内容复制到EXCEL文件中
- 时间控件是VS自带的还是要自己实现?
- 关于利用System.Net.HttpWebRequest 和System.Net.HttpWebResponse 抓取网页内容的问题
- 多串口卡多线程问题
- 使用DevExpress GridView 控件,GridView显示一个红叉?
- 高手们,求一个SQL的最新的IP地址数据库,分全给了,在线等,谢谢
- 扩展过TextBox 的请进!
if (满足某种条件)
{
cycle()
}把timer1.start.start()从cycle函数中移出来,到别的地方去
Serialport1串口控件,sp1
当timer1.start()的时候sp1也开始接收数据如果他接满了多少位数据则进入下一步循环
但由于可能有参数设置错误的情况,接不到数据,那么就定了5秒做为超时进入下一步循环
string mystring;private void cycle() //自定义的函数
{
int[] arrayboudrate = new int[9] { 300,1200,2400,4800,115200,9600,19200,38400,57600};
for (i=0;i <=100;i++)
{ sp1.BaudRate = arrayboudrate[i1];
.... //设置串口参数
try
{
sp1.open();//打开串口,开始接收数据
}catch{}
timer1.Interval = 5000;
timer1.start();
if (超过时间5秒)
{
continue;
}
else
{
if (接收的字符串与协议匹配)
终止循环
else
continue;
}
}
} private void timer1_Tick(object sender, EventArgs e)
{
这里应该怎么写
???
} private void sp1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
myrecieve = sp1.ReadExisting();
if (mystring.Length < 40)
mystring = mystring + myrecieve;
}
单一超时好办,但是在这个函数里,即要打开sp,又要启动timer,然后超时还要再反馈回来
不会写,大侠帮帮忙
private void cycle() //自定义的函数
{
int[] arrayboudrate = new int[9] { 300,1200,2400,4800,115200,9600,19200,38400,57600};
for (i=0;i <=100;i++)
{ sp1.BaudRate = arrayboudrate[i1];
//.... //设置串口参数
try
{
sp1.open();//打开串口,开始接收数据 timer1.Enabled=false;//如果正常,时间控件为无效状态 //在这里写代码判断是否接满了你所说的多少位 }
catch
{ timer1.Enabled=true;//发生错误,时间控件启动
timer1.Interval = 5000;
}
}
} private void timer1_Tick(object sender, EventArgs e)
{
cycle();//执行该方法
}
我用timer1的目的是因为,波特率不对的话,可能会一直接到空数据
也就是说我是要在sp1.open正常后开始计时5秒那么也就不能在timer1_Tick里执行cycle()了。。不知道我想法错哪了。。
bool iscontinue = false;
private void cycle() //自定义的函数
{
int[] arrayboudrate = new int[9] { 300,1200,2400,4800,115200,9600,19200,38400,57600};
for (i=0;i <=100;i++)
{ sp1.BaudRate = arrayboudrate[i1];
.... //设置串口参数
try
{
iscontinue = false;
timer1.Interval = 5000;
timer1.start();
sp1.open();//打开串口,开始接收数据
}
catch
{
iscontinue = true;
}
if(iscontinue) continue;
else
{
if (接收的字符串与协议匹配)
终止循环
else
continue;
}
}
} private void timer1_Tick(object sender, EventArgs e)
{
iscontinue = true;
timer1.stop();
}