C# 串口通讯 发送报文丢失数据 急急急
SerialPort sp = new SerialPort();
sp.PortName = com1;
sp.BaudRate = 1200;
sp.WriteTimeout = 5000;
sp.ReadTimeout = 5000;
sp.ReadBufferSize = 1024;
int portflag = 0;
for (int j = 0; j < dt2.Rows.Count; ++j)
{
idd = dt2.Rows[j][3].ToString();
if (idd.Length == 1)
{
idd = "0" + idd;
}
type = dt2.Rows[j][6].ToString();
bbb = (Convert.ToByte(idd, 16) + Convert.ToByte("4B", 16) + Convert.ToByte(type, 16));
text = "10" + idd + "4B" + type + bbb.ToString("X") + "16";
strText = "10 " + idd + " 4B " + type + " " + bbb.ToString("X") + " 16 ";
sb.Append(this.comName + "发送报文为:" + strText + "\n");
//转化为十六进制
byte[] bt; //要发送的报文
bt = new byte[text.Length / 2];
if (text.Length % 2 == 0)
{
for (int i = 0; i < text.Length; i += 2)
{
byte b = Convert.ToByte(text.Substring(i, 2), 16);
bt[i / 2] = b;
}
}
string str = ""; //要显示的报文
int Num = 1;
while (true)
{
//发送报文
try
{
sp.Write(bt, 0, bt.Length);
}
catch (Exception e){}
number = Convert.ToInt32(type.Substring(0, 1)) * 8 + Convert.ToInt32(type.Substring(1, 1)) + 6;
//Thread.Sleep(1000);
byte[] btt = new byte[number];
try
{
for (int i = 0; i < btt.Length; ++i)
{
btt[i] = (byte)sp.ReadByte();
}
//sp.Read(btt, 0, btt.Length);
}
catch (Exception e){}
for (int i = 0; i < btt.Length; ++i)
{
string strtemp = "";
strtemp = btt[i].ToString("X");
if (strtemp.Length == 1)
str += "0" + strtemp;
else
str += strtemp;
str += " ";
}
//sts.Send("QZ 68 " + id.ToString() + " tt 68 " + str.Substring(3)); //发送给服务器
se.StoreOneAnalog("QZ 68 " + id.ToString() + " tt 68 " + str.Substring(3));
//更新控件
sb.Append(this.comName + "接收报文为:" + str + "\n");
f.Name = this.ComName;
f.sb = sb;
f.ThreadMethod();
sb.Remove(0, sb.Length);
break;
}
发送报文比如10 04 4B 22 6E 16
采集器接收到只有04 4B 22 6E 16
如何解决 请高手指教