我的程序中有这段代码,有时候,比如每周一次,不断退出这个问题,但我真的不能忽略代码的哪一部分是错误的。有人可以分析它并帮助我解决这个问题吗?我必须做一个"电源循环"让它停止的程序。
以下是代码:
请注意多播和无线电模式始终为假。
public void ComThread()
{
byte[] buffer, data, remotebytes, recvbytes, attatched;
int port;
uint portindex;
string a, b, agvno;
string debugstring;
int agvindex = 0;
System.Net.IPEndPoint recvendpoint;
System.Net.EndPoint ep;
mw.Log(MhcsLib.DebugZones.StartupShutdown, DEVCOM_UDP_RECV_STARTING);
recvendpoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
ep = (System.Net.EndPoint)(recvendpoint);
remotebytes = remoteendpoint.Address.GetAddressBytes();
attatched = new byte[44];
while (true)
{
try
{
recvendpoint.Address = System.Net.IPAddress.Any;
if (multicast)
{
buffer = multicastclient.Receive(ref recvendpoint);
}
else
{
// start listening from ANY IP address and put in buffer the result if there is any
buffer = UdpClient.Receive(ref recvendpoint);
debugstring = ByteArrayToString(buffer);
//mw.Log(MhcsLib.DebugZones.Events, AGVCTL_LOG, debugstring);
//mw.Log(MhcsLib.DebugZones.Events,)
}
recvbytes = recvendpoint.Address.GetAddressBytes(); // get the bytes of the source IP address
port = recvendpoint.Port; // get the port of the source IP
if (buffer.Length == 32)
{
Array.Copy(buffer, 0, attatched, 0, buffer.Length);
}
else if (buffer.Length == 12 && attatched[0] != 0)
{
Array.Copy(buffer, 0, attatched, 32, buffer.Length);
}
if (radiomode && buffer.Length != 4)
{
if (buffer.Length > 6 && buffer.Length != 32 & attatched[43] == 0)
{
// get the AGV ID from the response polling message of the AGV in agvindex
a = System.Text.Encoding.ASCII.GetString(new byte[] { buffer[4] });
b = System.Text.Encoding.ASCII.GetString(new byte[] { buffer[5] });
agvno = a + b;
agvindex = Int32.Parse(agvno, System.Globalization.NumberStyles.HexNumber);
// if system in radiomode then call the message received method in AgvCtl with the index from the polling
portindex = (uint)agvindex - 1;
data = new byte[buffer.Length];
Array.Copy(buffer, 0, data, 0, buffer.Length);
if (portindex < nagv && portindex >= 0) // check if the agvindex is compatible
{
MessageReceived(portindex, data); // portindex is used to chose witch VCB record it should be opened
}
}
else if (buffer.Length == 12 && attatched[0] != 0)
{
// get the AGV ID from the response polling message of the AGV in agvindex
a = System.Text.Encoding.ASCII.GetString(new byte[] { attatched[4] });
b = System.Text.Encoding.ASCII.GetString(new byte[] { attatched[5] });
agvno = a + b;
agvindex = Int32.Parse(agvno, System.Globalization.NumberStyles.HexNumber);
// if system in radiomode then call the message received method in AgvCtl with the index from the polling
portindex = (uint)agvindex - 1;
data = new byte[attatched.Length];
Array.Copy(attatched, 0, data, 0, attatched.Length);
MessageReceived(portindex, data);
Array.Clear(attatched, 0, attatched.Length);
}
}
else
{
if (buffer.Length == 12 && attatched[0] != 0)
{
// get the AGV ID from the response polling message of the AGV in agvindex
a = System.Text.Encoding.ASCII.GetString(new byte[] { attatched[4] });
b = System.Text.Encoding.ASCII.GetString(new byte[] { attatched[5] });
agvno = a + b;
agvindex = Int32.Parse(agvno, System.Globalization.NumberStyles.HexNumber);
// if system in radiomode then call the message received method in AgvCtl with the index from the polling
portindex = (uint)agvindex - 1;
data = new byte[attatched.Length];
Array.Copy(attatched, 0, data, 0, attatched.Length);
MessageReceived(portindex, data); // portindex is used to chose witch VCB record it should be opened
Array.Clear(attatched, 0, attatched.Length);
}
// if system not in radiomode then get the portindex from the source IP address
if ((recvbytes[0] == remotebytes[0]) && (recvbytes[1] == remotebytes[1]) && (recvbytes[2] == remotebytes[2]) &&
(recvbytes[3] >= remotebytes[3]) && (recvbytes[3] < (remotebytes[3] + ncomport)))
{
portindex = (uint)(recvbytes[3] - remotebytes[3]);
data = new byte[buffer.Length];
Array.Copy(buffer, 0, data, 0, buffer.Length);
MessageReceived(portindex, data);
}
else
{
//mw.Log(MhcsLib.DebugZones.Errors, DEVCOM_UDP_RECV_REJECTED, recvendpoint.Address, recvendpoint.Port);
}
}
}
catch (System.Net.Sockets.SocketException ex)
{
mw.Log(MhcsLib.DebugZones.Errors, DEVCOM_ERR_UDP_RECV, ex.Message);
if (ex.NativeErrorCode != 10054)
{
//break;
}
}
catch (Exception ex)
{
var st = new StackTrace(ex, true);
mw.Log(MhcsLib.DebugZones.Errors, DEVCOM_ERR_UDP_RECV, ex.Message);
//break;
}
}
mw.Log(MhcsLib.DebugZones.StartupShutdown, DEVCOM_UDP_RECV_EXITING);
}