I have written an app that sends messages on and listens for messages on Com ports. The sending is working fine, but recently some hardware was setup that supposedly mirrors the sent message right back to me (the "perp" explains: "I have a loop back
connector on the COM1 RS232 port so that transmitted data on COM1 is received on COM1."). The "data received" event does fire, but then the app blows up without even giving me an err msg or logging the message. Apparently it is an "Invalid Operation Exception."
Sounds pretty vague to me...
Does anybody have any idea whats causing this/how it can be circumvented?
Some of my code (hopefully, the most pertinent) is below:
SerialPort port1;
. . .
// This, the sending of a message on the port, works:<br/>
private void SendToRS232_1(string AStringToSend)<br/>
{<br/>
// This should never happen, as if this is the case, none of the buttons should<br/>
// be enabled, meaning the user can do nothing but exit the app<br/>
if (sArrCOMPorts.Length < 1)<br/>
{<br/>
Log(LogMsgType.Warning, "Nothing in sArrCOMPorts (SendToRS232_1())");<br/>
return;<br/>
}
// This method is called multiple times, but only want to instantiate port1<br/>
// and set up its event handlers and properties once<br/>
if (port1 == null)<br/>
{<br/>
port1 = new SerialPort(sArrCOMPorts[0], 9600, Parity.None, 8, StopBits.One);<br/>
// This method will be called when there is data waiting in the ports buffer<br/>
port1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived1);<br/>
port1.Handshake = Handshake.None; //Needed? Desirable?<br/>
try<br/>
{<br/>
port1.Open();<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
Log(LogMsgType.Error, string.Format(<br/>
"Error ({0}), StackTrace: ({1}), Inner Exception: ({2}) encountered while trying to open port 1 in SendToRS232_1() at
{3}",<br/>
ex.Message, ex.StackTrace, ex.InnerException, DateTime.Now.ToLocalTime()));<br/>
}<br/>
}
<br/>
// This event fires when there is data waiting in the ports buffer. See SendToRS232_1()<br/>
private void port_DataReceived1(object sender, SerialDataReceivedEventArgs e)<br/>
{<br/>
Log(LogMsgType.Normal, string.Format("port_DataReceived1() was called at {0}", DateTime.Now.ToLocalTime()));<br/>
try<br/>
{<br/>
// If the com port has been closed, do nothing<br/>
if (!port1.IsOpen)<br/>
{<br/>
Log(LogMsgType.Warning, string.Format("{0} was found to be closed at {1}, attempting to reopen...",<br/>
sArrCOMPorts[0], DateTime.Now.ToLocalTime()));<br/>
try<br/>
{<br/>
port1.Open();<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
Log(LogMsgType.Error, string.Format(<br/>
"Error encountered attempting to open port1: {0}, StackTrace: ({1}), Inner Exception: ({2}) at
{3}",<br/>
ex.Message, ex.StackTrace, ex.InnerException, DateTime.Now.ToLocalTime()));<br/>
return;<br/>
}<br/>
}<br/>
// Read all the data waiting in the buffer<br/>
string sData = port1.ReadExisting();
Log(LogMsgType.IncomingCom1, sData);
// If an Ack Msg, those of type "N" (negative, not received successfully) should<br/>
// be resent at least twice (so, a total of 3 times). If considered critical, can<br/>
// continue to be resent, but after third failure, a msg type "E" (error) is sent.<br/>
// Im deducing, then, that it is the recipients duty to keep track of how many<br/>
// times it has sent "N" - we dont have to on this end. As long as its "N"<br/>
// we keep trying to resend; if "E", we only try to resend if it is a "Critical" msg<br/>
if (ssu.IsAcknowledgementMessage(sData))<br/>
{<br/>
if (sData[SortSimUtils.ACK_MSG_TYPE_POS].ToString().Equals("N"))<br/>
{<br/>
// Can we assume that the msg to resend is the last one with the given
<br/>
// sequence number? Will the "reply" come that quickly?<br/>
int iSeqNum = ssu.GetSequenceNumber(sData);<br/>
SendToRS232_1(ssu.GetLastAckMsgWithSeqNum(iSeqNum));<br/>
}<br/>
else if (sData[SortSimUtils.ACK_MSG_TYPE_POS].ToString().Equals("E"))<br/>
{<br/>
//if critical, resend, otherwise ignore (log)<br/>
if (ssu.IsCriticalAckMsg(sData))<br/>
{<br/>
SendToRS232_1(sData);<br/>
}<br/>
else<br/>
{<br/>
Log(LogMsgType.Warning, string.Format(<br/>
"Non-critical Ack msg returned Error code: {0}", sData));<br/>
}<br/>
}<br/>
}<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
Log(LogMsgType.Error, ex.Message);<br/>
}<br/>
} <hr class="sig Photographer/Writer/Drummer: www.iceagetrail.posterous.com
View the full article
connector on the COM1 RS232 port so that transmitted data on COM1 is received on COM1."). The "data received" event does fire, but then the app blows up without even giving me an err msg or logging the message. Apparently it is an "Invalid Operation Exception."
Sounds pretty vague to me...
Does anybody have any idea whats causing this/how it can be circumvented?
Some of my code (hopefully, the most pertinent) is below:
SerialPort port1;
. . .
// This, the sending of a message on the port, works:<br/>
private void SendToRS232_1(string AStringToSend)<br/>
{<br/>
// This should never happen, as if this is the case, none of the buttons should<br/>
// be enabled, meaning the user can do nothing but exit the app<br/>
if (sArrCOMPorts.Length < 1)<br/>
{<br/>
Log(LogMsgType.Warning, "Nothing in sArrCOMPorts (SendToRS232_1())");<br/>
return;<br/>
}
// This method is called multiple times, but only want to instantiate port1<br/>
// and set up its event handlers and properties once<br/>
if (port1 == null)<br/>
{<br/>
port1 = new SerialPort(sArrCOMPorts[0], 9600, Parity.None, 8, StopBits.One);<br/>
// This method will be called when there is data waiting in the ports buffer<br/>
port1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived1);<br/>
port1.Handshake = Handshake.None; //Needed? Desirable?<br/>
try<br/>
{<br/>
port1.Open();<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
Log(LogMsgType.Error, string.Format(<br/>
"Error ({0}), StackTrace: ({1}), Inner Exception: ({2}) encountered while trying to open port 1 in SendToRS232_1() at
{3}",<br/>
ex.Message, ex.StackTrace, ex.InnerException, DateTime.Now.ToLocalTime()));<br/>
}<br/>
}
<br/>
// This event fires when there is data waiting in the ports buffer. See SendToRS232_1()<br/>
private void port_DataReceived1(object sender, SerialDataReceivedEventArgs e)<br/>
{<br/>
Log(LogMsgType.Normal, string.Format("port_DataReceived1() was called at {0}", DateTime.Now.ToLocalTime()));<br/>
try<br/>
{<br/>
// If the com port has been closed, do nothing<br/>
if (!port1.IsOpen)<br/>
{<br/>
Log(LogMsgType.Warning, string.Format("{0} was found to be closed at {1}, attempting to reopen...",<br/>
sArrCOMPorts[0], DateTime.Now.ToLocalTime()));<br/>
try<br/>
{<br/>
port1.Open();<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
Log(LogMsgType.Error, string.Format(<br/>
"Error encountered attempting to open port1: {0}, StackTrace: ({1}), Inner Exception: ({2}) at
{3}",<br/>
ex.Message, ex.StackTrace, ex.InnerException, DateTime.Now.ToLocalTime()));<br/>
return;<br/>
}<br/>
}<br/>
// Read all the data waiting in the buffer<br/>
string sData = port1.ReadExisting();
Log(LogMsgType.IncomingCom1, sData);
// If an Ack Msg, those of type "N" (negative, not received successfully) should<br/>
// be resent at least twice (so, a total of 3 times). If considered critical, can<br/>
// continue to be resent, but after third failure, a msg type "E" (error) is sent.<br/>
// Im deducing, then, that it is the recipients duty to keep track of how many<br/>
// times it has sent "N" - we dont have to on this end. As long as its "N"<br/>
// we keep trying to resend; if "E", we only try to resend if it is a "Critical" msg<br/>
if (ssu.IsAcknowledgementMessage(sData))<br/>
{<br/>
if (sData[SortSimUtils.ACK_MSG_TYPE_POS].ToString().Equals("N"))<br/>
{<br/>
// Can we assume that the msg to resend is the last one with the given
<br/>
// sequence number? Will the "reply" come that quickly?<br/>
int iSeqNum = ssu.GetSequenceNumber(sData);<br/>
SendToRS232_1(ssu.GetLastAckMsgWithSeqNum(iSeqNum));<br/>
}<br/>
else if (sData[SortSimUtils.ACK_MSG_TYPE_POS].ToString().Equals("E"))<br/>
{<br/>
//if critical, resend, otherwise ignore (log)<br/>
if (ssu.IsCriticalAckMsg(sData))<br/>
{<br/>
SendToRS232_1(sData);<br/>
}<br/>
else<br/>
{<br/>
Log(LogMsgType.Warning, string.Format(<br/>
"Non-critical Ack msg returned Error code: {0}", sData));<br/>
}<br/>
}<br/>
}<br/>
}<br/>
catch (Exception ex)<br/>
{<br/>
Log(LogMsgType.Error, ex.Message);<br/>
}<br/>
} <hr class="sig Photographer/Writer/Drummer: www.iceagetrail.posterous.com
View the full article