代码是从网上下来,调用了本人编写数据处理的类的函数,软件接受一会儿数据就会报出索引超出了数据界限错误,假如去除本人调用的方法,软件就可以正常运行
一下是部分代码:
/// <summary>
/// 侦听接受线程
/// </summary>
private void AcceptClient()
{
try
{
tcpClient = tcpListener.AcceptTcpClient();
ns = tcpClient.GetStream();
while (true)
{
int readlen = tcpClient.Available;
if (readlen > 0)
{
byte[] getData =new byte[37];
ns.Read(getData,0,getData.Length);
string getMsg = Encoding.Default.GetString(getData); // 将字节数组转换为文本形式
decompose1.extact(getMsg);
lstMsg.Invoke(showMsgCallBack, getMsg);// 将消息添加到接收消息列表中
ns.Write(getData, 0, getData.Length);// 返回相同消息给客户端
}
}
}
catch(ThreadAbortException)
{
}
catch (Exception ex)
{
// 连接发生异常
MessageBox.Show(ex.Message);
// 释放相关系统资源
if (tcpClient != null)
tcpClient.Close();
if (ns != null)
ns.Dispose();
}
}
下面是数据解析类Decompose
class Decompose1
{
public double[] Parameter_Monitor;
public double[] Parameter_Battery;
public double[] Parameter_Temprature;
public double[] Parameter_GeneralState = { 2000, 30, 20, 60, 70, 65, 90, 4, 80, 6, 95, 3, 82, 5, 90, 4, 80, 6, 90, 4, 80, 6, 0 };
public Decompose1()
{
Parameter_Monitor = new double[4];
Parameter_Battery = new double[4];
Parameter_Temprature = new double[4];
}
public void extact(string gprsmsg)
{
一下是部分代码:
/// <summary>
/// 侦听接受线程
/// </summary>
private void AcceptClient()
{
try
{
tcpClient = tcpListener.AcceptTcpClient();
ns = tcpClient.GetStream();
while (true)
{
int readlen = tcpClient.Available;
if (readlen > 0)
{
byte[] getData =new byte[37];
ns.Read(getData,0,getData.Length);
string getMsg = Encoding.Default.GetString(getData); // 将字节数组转换为文本形式
decompose1.extact(getMsg);
lstMsg.Invoke(showMsgCallBack, getMsg);// 将消息添加到接收消息列表中
ns.Write(getData, 0, getData.Length);// 返回相同消息给客户端
}
}
}
catch(ThreadAbortException)
{
}
catch (Exception ex)
{
// 连接发生异常
MessageBox.Show(ex.Message);
// 释放相关系统资源
if (tcpClient != null)
tcpClient.Close();
if (ns != null)
ns.Dispose();
}
}
下面是数据解析类Decompose
class Decompose1
{
public double[] Parameter_Monitor;
public double[] Parameter_Battery;
public double[] Parameter_Temprature;
public double[] Parameter_GeneralState = { 2000, 30, 20, 60, 70, 65, 90, 4, 80, 6, 95, 3, 82, 5, 90, 4, 80, 6, 90, 4, 80, 6, 0 };
public Decompose1()
{
Parameter_Monitor = new double[4];
Parameter_Battery = new double[4];
Parameter_Temprature = new double[4];
}
public void extact(string gprsmsg)
{
string[] canArry = gprsmsg.Split(new char[1] { ” ” }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < 4; i++)
{
Parameter_Monitor[i] = Convert.ToDouble(canArry[i]);
Parameter_Battery[i] = Convert.ToDouble(canArry[i + 4]);
Parameter_Temprature[i] = Convert.ToDouble(canArry[i + 8]);
}
}
}
}
解决方案
20
要谁回复请引用,此人能收到你针对的回复。
你是在decompose1.extact(getMsg);之后打印的吗?要放在他前面或暂时去掉此句!
你是在decompose1.extact(getMsg);之后打印的吗?要放在他前面或暂时去掉此句!