C# 串口发送一组数据,然后接收一个BD,再发第二组,不知道哪里有错,收不了!
ReDatCount = Receive();if (ReDatCount == 7)
comm.Read(readbuffer, 0, ReDatCount);
if (readbuffer[0] != 0xbd)
break;
//子函数
public int Receive()
{
int ReDatCount = 0;
timeReceive.Interval = 500;
timeReceive.Enabled = true;
while (timeReceive.Enabled == true)
{
if (mycomm.BytesToRead > ReDatCount)
{
ReDatCount = mycomm.BytesToRead;
timeReceive.Enabled = false;
timeReceive.Interval = 100;
timeReceive.Enabled = true;
}
else
{
Application.DoEvents();
}
}
return ReDatCount;
}
private void timeReceive_Tick(object sender, EventArgs e)
{
timeReceive.Enabled = false;
} --------------------编程问答-------------------- 快点,搞定小菜鸟 --------------------编程问答-------------------- 在线等啊, --------------------编程问答-------------------- 确认下串口打开没? --------------------编程问答-------------------- 绝对打开了,好像是定时跑不到 --------------------编程问答-------------------- 有好的主意也行,只要能接收到BD,在串口发送之后就可以! --------------------编程问答-------------------- private void timer_Tick(object sender, EventArgs e)
{
char[] sendbyte = { '#', 'R', 'P', '\r', '\n' };
cptComPort.Write(sendbyte, 0, sendbyte.Length);
if (cptComPort.BytesToRead > 0)
{
char[] receivebyte = new char[cptComPort.BytesToRead];
cptComPort.Read(receivebyte, 0, receivebyte.Length);
if ((receivebyte[0] == '#') && (receivebyte[1] == 'R') && (receivebyte[2] == 'P') && (receivebyte[13] == 'M') && (receivebyte[14] == 'P') && (receivebyte[15] == 'a'))
{
char[] b = new char[10];
for (int i = 0; i < 10; i++)
{
b[i] = receivebyte[i + 3];
}
decComPress = decimal.Parse(new string(b));
/*decComPress = (decimal)((b[1] - 0x30) * 100 + (b[2] - 0x30) * 10 + (b[4] - 0x30) * 0.1 + (b[5] - 0x30) * 0.01 + (b[6] - 0x30) * 0.001 + (b[7] - 0x30) * 0.0001 + (b[8] - 0x30) * 0.00001 + (b[9] - 0x30) * 0.000001);
if (b[0] == '-')
decComPress = -decComPress;*/
}
}
我也是刚学,在定时器中断里面,发送一串字符,然后查询接收,看看对你有用不。 --------------------编程问答--------------------
private void timer_Tick(object sender, EventArgs e)
{
char[] sendbyte = { '#', 'R', 'P', '\r', '\n' };
cptComPort.Write(sendbyte, 0, sendbyte.Length);
//System.Threading.Thread.Sleep(100);
if (cptComPort.BytesToRead > 0)
{
char[] receivebyte = new char[cptComPort.BytesToRead];
cptComPort.Read(receivebyte, 0, receivebyte.Length);
if ((receivebyte[0] == '#') && (receivebyte[1] == 'R') && (receivebyte[2] == 'P') && (receivebyte[13] == 'M') && (receivebyte[14] == 'P') && (receivebyte[15] == 'a'))
{
char[] b = new char[10];
for (int i = 0; i < 10; i++)
{
b[i] = receivebyte[i + 3];
}
decComPress = decimal.Parse(new string(b));
/*decComPress = (decimal)((b[1] - 0x30) * 100 + (b[2] - 0x30) * 10 + (b[4] - 0x30) * 0.1 + (b[5] - 0x30) * 0.01 + (b[6] - 0x30) * 0.001 + (b[7] - 0x30) * 0.0001 + (b[8] - 0x30) * 0.00001 + (b[9] - 0x30) * 0.000001);
if (b[0] == '-')
decComPress = -decComPress;*/
}
}
看起来好乱,怎么发成代码! --------------------编程问答-------------------- 终于有人回了,先谢谢了!
这个不太好! --------------------编程问答-------------------- private void timeReceive_Tick(object sender, EventArgs e)
{
timeReceive.Enabled = false;
}
一触发你就设为FALSE
public int Receive()能获取一次算好了,有可能一次都获取不到
补充:.NET技术 , C#