一个serialport串口控件的问题

liutianqian 2008-12-01 07:36:56
我在定程序的时候,发现用serialport.write(byte[] sendmessage,int a,int count)发送数据后,再用serialport.read(byte[] receivemessage,int b,int count)接收数据后,receivemessage不能用int anotherint=receivemessage[0]这种方式来给别的值付值。
比如:
byte[] sendmessage={1,2,3,4,5,6,7};
serialport1.write(sendmessage,0,sendmessage.length);
int relength=serialport1.bytestoread;
byte[] receivemessage=new byte[relength];
serialport1.read(receivemessage,0,relength);
byte k=1;
if(k==receivemessage[0]) //在这里就会发生一个错误,好像是“索引超出了数组范围”
{
byte j=receivemessage[1]; //这里也会发生同样的错误
}
for(int i=0;i<7;i++)
{
byte l=receivemessage[i]; //这里却 不会 发生这个问题


也就是说不能指定为数组的某一个值。
请教各位大侠怎么处理这个问题。。。。。。。。
...全文
48 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
liutianqian 2008-12-01
  • 打赏
  • 举报
回复
读到数据的,因为后面:
for(int i=0;i <7;i++)
{
byte l=receivemessage[i]; //这里却 不会 发生这个问题

是没有问题的。在我的程序里,循环里的语句是:textBox1.Text=textBox1.Text+receivemessage[i].toString()+"";
在运行时,textbox1能把所有数据都显示出来。
CraxyMouse 2008-12-01
  • 打赏
  • 举报
回复
/******************************************************************
* Copyright(c) : Tangxu
* Description :
* CreateDate : 2006-9-01 04:53:08
* Creater : Tang.xu
* LastChangeDate:
* LastChanger :
* Version Info : 1.0.0
* ******************************************************************/
using System;
using System.IO.Ports;
using System.Threading;
using System.Text;

namespace Tangxu.Common
{
public class ReadCom
{
public ReadCom()
{
_ReadConfig = new ReadConfigure(System.Environment.CurrentDirectory + "\\Com_Info.xml");
}

public ReadCom(string sCom,int nBaud):this()
{

}

private byte[] _ReadBuffer;
private SerialPort ss_port = new SerialPort();
private static int nReadCount = 0;
private ReadConfigure _ReadConfig;

#region Initialize com port

public bool InitCom()//初始化建串口类实例
{
// return true;
try
{
ss_port.PortName = _ReadConfig.GetNodeValue("PORT");// _sComPort;
ss_port.BaudRate = int.Parse(_ReadConfig.GetNodeValue("BAUD"));//_nBaud;
ss_port.ReadBufferSize = 10240;
ss_port.DataBits = int.Parse(_ReadConfig.GetNodeValue("DATA"));//8;
switch (_ReadConfig.GetNodeValue("PARITY"))
{
case "None":
ss_port.Parity = Parity.None;
break;
case "Even":
ss_port.Parity = Parity.Even;
break;
case "Mark":
ss_port.Parity = Parity.Mark;
break;
case "Odd":
ss_port.Parity = Parity.Odd;
break;
case "Space":
ss_port.Parity = Parity.Mark;
break;
}
switch (_ReadConfig.GetNodeValue("STOP"))
{
case "1":
ss_port.StopBits = StopBits.One;
break;
case "1.5":
ss_port.StopBits = StopBits.OnePointFive;
break;
case "2":
ss_port.StopBits = StopBits.Two;
break;
}
ss_port.ReadTimeout = 600;
ss_port.WriteTimeout = 700;

ss_port.Open();//打开串口
return true;
}
catch (Exception ex)
{
throw new Exception("打开串口失败!\r\n错误信息:" + ex.Message);
}
}
#endregion

#region FreeDrv
/// <summary>
/// free opw
/// </summary>
public void FreeDrv()
{
try
{
if (ss_port != null)
{
ss_port.Close();
}
}
catch
{ }
}
#endregion

#region Write command to OPW
/// <summary>
/// 发操作命令给OPW设备
/// 并返回状态
/// </summary>
/// <param name="sCommand"> </param>
/// <returns> </returns>
public string WriteCommand(string sCommand)
{
StringBuilder sb = new StringBuilder();
bool bRead = true;
try
{
ss_port.DiscardInBuffer();
ss_port.Write(sCommand);
Thread.Sleep(1500);
while (bRead)
{
_ReadBuffer = new byte[ss_port.BytesToRead];
ss_port.Read(_ReadBuffer, 0, _ReadBuffer.Length);
sb.Append(Encoding.ASCII.GetString(_ReadBuffer));
Thread.Sleep(500);
if (ss_port.BytesToRead <= 0)
{
bRead= false;
}
}
if (sb.ToString().Length== 0)
{
nReadCount++;
}

if (nReadCount == 3)
{
nReadCount = 0;
throw new Exception("设置不正确或没有联接设备!");
}
}
catch (Exception ex)
{
throw new Exception("从设备获取数据失败!\r\n错误信息:" + ex.Message);
}
return sb.ToString(); ;
}

public string WriteCommand(byte[] bCommand)
{
StringBuilder sb = new StringBuilder();
bool bRead = true;
try
{
ss_port.DiscardInBuffer();
ss_port.Write(bCommand,0,bCommand.Length);
Thread.Sleep(1500);
while (bRead)
{
_ReadBuffer = new byte[ss_port.BytesToRead];
ss_port.Read(_ReadBuffer, 0, _ReadBuffer.Length);
sb.Append(Encoding.ASCII.GetString(_ReadBuffer));
Thread.Sleep(500);
if (ss_port.BytesToRead <= 0)
{
bRead = false;
}
}
if (sb.ToString().Length == 0)
{
nReadCount++;
}

if (nReadCount == 3)
{
nReadCount = 0;
throw new Exception("设置不正确或没有联接设备!");
}
}
catch (Exception ex)
{
throw new Exception("从设备获取数据失败!\r\n错误信息:" + ex.Message);
}
return sb.ToString();
}
#endregion

#region Get All COM Port
public string[] GetAllComPort()
{
string[] sAllPort = null;
try
{
sAllPort = SerialPort.GetPortNames();
}
catch (Exception ex)
{
throw new Exception("获取计算机COM口列表失败!\r\n错误信息:" + ex.Message);
}
return sAllPort;
}
#endregion
}

}
CraxyMouse 2008-12-01
  • 打赏
  • 举报
回复
receivemessage
这个数组是不是空的呀!
你检查一下你看看你有没有读到数据!
根据你的描述应该是发一条命令给设备,设备返回数据?

110,533

社区成员

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

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

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