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);
}
错误提示出现在绿色那行,为索引超出数组界限问题。
程序可以正常通信,但通信一段时间后出现问题。
是否要清空委托的列表之类的?如果是要如何做?
望高手指教~
...全文
1026 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)
代码下载地址: https://pan.quark.cn/s/068306a56640 在交通运输科学范畴内,交通流量分配是探讨交通系统内交通负荷如何在各个道路路段间进行合理分配的核心议题。交通流分配技术主要应用于模拟和推演交通系统的运行状态,旨在为城市规划人员与交通工程师提供优化道路布局的参考,进而缓解交通拥堵现象,提升道路交通的整体效能。以下将对"交通流分配技术"进行深入阐述: 1. **全部或无分配(All-or-Nothing Distribution)** 全部或无分配是一种基础的交通流分配策略。该技术依据最短路径准则,预设所有从发源地(Origin)至目的地(Destination)的车辆都将选择路程最短的路线。倘若存在多条等长路径,则采用随机方式确定其中一条。此类方法未将交通拥堵因素纳入考量,因为一旦选定路径,便假定所有交通量将沿此路径通行,即便这可能导致部分路段出现超负荷状况。 2. **逐步分配(Progressive Assignment)** 逐步分配技术顾及了交通网络的动态特征。该算法通过逐步优化流量配置,力求使网络内行驶的总代价(诸如通行时间、距离等指标)最小化。运算过程始于初始的全部或无分配状态,随后逐步修正路径选择,直至网络达成稳定状态,即每次流量修正后,总代价的变动幅度不再明显。此方法更贴近现实交通场景,因为车辆会依据即时路况选择最优路径。 3. **迭代平均分配(Iterative Average Distribution)** 迭代平均分配是一种对逐步分配技术的优化版本,其更侧重于实现网络流量的均衡化。在每一轮迭代过程中,算法不仅评估当前路径的优化程度,同时参考前一次迭代的流量配置。通过融入平均化理念,该方法能更有效地分散交通...

111,129

社区成员

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

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

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