C#串口通信索引超出了数组界限问题

keyrua 2010-06-11 08:37:53
以下是部分代码:

delegate void ShowInformation(string str1);//显示信息
private void ShowInfo(string StrInfo)
{
Invoke(new ShowInformation(OnshowInformation), StrInfo);
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
int recByetLen = this.serialPort1.BytesToRead;
byte[] recByte = new byte[recByetLen];
this.serialPort1.Read(recByte, 0, recByetLen);
str = Encoding.UTF8.GetString(recByte);
ShowInfo(str);
}
错误提示出现在绿色那行,为索引超出数组界限问题。
程序可以正常通信,但通信一段时间后出现问题。
是否要清空委托的列表之类的?如果是要如何做?
望高手指教~
...全文
837 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
keyrua 2010-06-11
  • 打赏
  • 举报
回复
我的数据是固定接收到某量之后出现错误的

比如说我没2秒接收到一个串数据,接收6分钟左右后报错
keyrua 2010-06-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 casinosun 的回复:]

if (this.serialPort1.InvokeRequired)
Invoke(new ShowInformation(OnshowInformation), StrInfo);
[/Quote]

serialPort里面没有InvokeRequired的定义哦
keyrua 2010-06-11
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 casinosun 的回复:]

OnshowInformation 有没有问题呢?
[/Quote]

private void OnshowInformation(string StrInfo)
{
int i,sumTemp;
this.lBRec.Items.Add(StrInfo );
this.lBRec.SelectedIndex = this.lBRec.Items.Count - 1;
if (this.lBRec.Items.Count > 20)
{
this.lBRec.Items.Clear();
}
...
...
}
只是用来把数据显示到label或list上去,应该没问题吧
casinosun 2010-06-11
  • 打赏
  • 举报
回复
if (this.serialPort1.InvokeRequired)
Invoke(new ShowInformation(OnshowInformation), StrInfo);
casinosun 2010-06-11
  • 打赏
  • 举报
回复
OnshowInformation 有没有问题呢?
keyrua 2010-06-11
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 dengxuxing 的回复:]

byte[] recByte = new byte[recByetLen+1];
this.serialPort1.Read(recByte, 0, recByetLen);
[/Quote]

这样修改后,问题依然出现了。如果是数组接收超出范围的,应该会在接收处提示错误的吧。
dengxuxing 2010-06-11
  • 打赏
  • 举报
回复
byte[] recByte = new byte[recByetLen+1];
this.serialPort1.Read(recByte, 0, recByetLen);
keyrua 2010-06-11
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zdbb 的回复:]

单步看代码,应该是接收到empty,就是空串,所以出错。
[/Quote]

这个软件是和单片机通信的,我单片机的程序没有发送空串的。
如果是空串问题,该在那里加入防止接收空串?
keyrua 2010-06-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 computerfox 的回复:]

我觉得不在那一行出错,很可能是下面的出错 了:
this.serialPort1.Read(recByte, 0, recByetLen);
[/Quote]

错误行是VS2005提示的,具体是那行我不清楚,若是按你所说的,应该怎么改?
捷哥1999 2010-06-11
  • 打赏
  • 举报
回复
我觉得不在那一行出错,很可能是下面的出错 了:
this.serialPort1.Read(recByte, 0, recByetLen);
只在此山中 2010-06-11
  • 打赏
  • 举报
回复
单步看代码,应该是接收到empty,就是空串,所以出错。
keyrua 2010-06-11
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 wuyazhe 的回复:]

ShowInformation
进入就加try...catch包裹整个方法。试试。
[/Quote]

这样加了后,程序是可以一直跑下去了。可原来的不知为啥?
我想catch里面加个Messagebox.show()
之后要用户点击确定后,程序才往下走,这个怎么实现啊?
谢谢你了
keyrua 2010-06-11
  • 打赏
  • 举报
回复
排好了的,一提交回复,格式都没了 郁闷
keyrua 2010-06-11
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 wuyazhe 的回复:]

ShowInformation
里面的问题。具体你代码太长了。还没用
扩起来。不想看。
这么多代码没个缩进谁看的下去。
[/Quote]

不好意思,发帖比较少 排版不太会,见谅!

private void OnshowInformation(string StrInfo)
{
int i,sumTemp;
this.lBRec.Items.Add(StrInfo );
this.lBRec.SelectedIndex = this.lBRec.Items.Count - 1;
if (this.lBRec.Items.Count > 20)
{
this.lBRec.Items.Clear();
}
if ((recInt[0] == 0xAA) && (recInt[1] == 0xBB))
{
sumTemp = 0;
for (i = 0; i <= recCharLen - 2; i++)
{
sumTemp = sumTemp + recInt[i];
}
if (recInt[recCharLen - 1] == sumTemp % 256)
{
switch (recInt[2])
{
case 0x10:
if (recInt[3] == 0x01)
{
if (recInt[4] == 0x55)
{
this.label48.Text = "设定成功";
}
else if (recInt[4] == 0xAA)
{
this.label48.Text = "设定数据错误";
}
else if (recInt[4] == 0x11)
{
this.label48.Text = "设定失败";
}
}
else if (recInt[3] == 0x02)
{
if (recInt[4] == 0x55)
{
this.label49.Text = "设定成功";
}
else if (recInt[4] == 0xAA)
{
this.label49.Text = "设定数据错误";
}
else if (recInt[4] == 0x11)
{
this.label49.Text = "设定失败";
}
}
else if (recInt[3] == 0x03)
{
if (recInt[4] == 0x55)
{
this.label50.Text = "设定成功";
}
else if (recInt[4] == 0xAA)
{
this.label50.Text = "设定数据错误";
}
else if (recInt[4] == 0x11)
{
this.label50.Text = "设定失败";
}
}
break;
case 0x20:
if (recInt[3] == 0x01)
{
if (recInt[4] == 0x55)
{
this.label48.Text = "开始成功,运行中";
}
else if (recInt[4] == 0x11)
{
this.label48.Text = "开始失败";
}
}
else if (recInt[3] == 0x02)
{
if (recInt[4] == 0x55)
{
this.label49.Text = "开始成功,运行中";
}
else if (recInt[4] == 0x11)
{
this.label49.Text = "开始失败";
}
}
else if (recInt[3] == 0x03)
{
if (recInt[4] == 0x55)
{
this.label50.Text = "设定成功";
}
else if (recInt[4] == 0xAA)
{
this.label50.Text = "设定数据错误";
}
else if (recInt[4] == 0x11)
{
this.label50.Text = "设定失败";
}
}
break;
case 0x30:
if (recInt[3] == 0x01)
{
if (recInt[4] == 0x55)
{
this.label48.Text = "已暂停";
}
else if (recInt[4] == 0x11)
{
this.label48.Text = "暂停失败";
}
}
else if (recInt[3] == 0x02)
{
if (recInt[4] == 0x55)
{
this.label49.Text = "已暂停";
}
else if (recInt[4] == 0x11)
{
this.label49.Text = "暂停失败";
}
}
else if (recInt[3] == 0x03)
{
if (recInt[4] == 0x55)
{
this.label50.Text = "已暂停";
}
else if (recInt[4] == 0x11)
{
this.label50.Text = "暂停失败";
}
}
break;
case 0x40:
if (recInt[3] == 0x01)
{
if (recInt[4] == 0x55)
{
this.label48.Text = "已结束";
this.button5.Text = "开始";
}
else if (recInt[4] == 0x11)
{
this.label48.Text = "结束失败";
}
}
else if (recInt[3] == 0x02)
{
if (recInt[4] == 0x55)
{
this.label49.Text = "已结束";
this.button8.Text = "开始";
}
else if (recInt[4] == 0x11)
{
this.label49.Text = "结束失败";
}
}
else if (recInt[3] == 0x03)
{
if (recInt[4] == 0x55)
{
this.label50.Text = "已结束";
this.button11.Text = "开始";
}
else if (recInt[4] == 0x11)
{
this.label50.Text = "结束失败";
}
}
break;
case 0x90:
if (recInt[3] == 0x01)
{
this.label4.Text = (recInt[4] * 256 + recInt[5]).ToString();
}
else if (recInt[3] == 0x02)
{
this.label21.Text = (recInt[4] * 256 + recInt[5]).ToString();
}
else if (recInt[3] == 0x03)
{
this.label37.Text = (recInt[4] * 256 + recInt[5]).ToString();
}
break;
default:
break;
}
}
}
}
filec75 2010-06-11
  • 打赏
  • 举报
回复
ding
兔子-顾问 2010-06-11
  • 打赏
  • 举报
回复
ShowInformation
进入就加try...catch包裹整个方法。试试。
兔子-顾问 2010-06-11
  • 打赏
  • 举报
回复
ShowInformation
里面的问题。具体你代码太长了。还没用
扩起来。不想看。
这么多代码没个缩进谁看的下去。
keyrua 2010-06-11
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 smthgdin 的回复:]

this.serialPort1.Read(recByte, 0, recByetLen);
this.serialPort1.Read(recByte, 0, recByetLen-1);不减去1就超出了。
[/Quote]

这个我不理解,为何要-1?
smthgdin_020 2010-06-11
  • 打赏
  • 举报
回复
this.serialPort1.Read(recByte, 0, recByetLen);
this.serialPort1.Read(recByte, 0, recByetLen-1);不减去1就超出了。
keyrua 2010-06-11
  • 打赏
  • 举报
回复
现在情况是
我用串口调试软件给我的软件发数据,我的软件可以一直接收并显示;
我用串口调试软件接收我单片机的数据,数据可以正确接受完;

但用我的软件接收单片机的数据就只能接收到中途报错~

大家帮忙给个解决办法或测试方法吧
加载更多回复(4)

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧