public bool WriteData(string msg)
{
bool flag = true;
if (!ComPort.IsOpen && !OpenPort())
return false;
switch (CurrentTransmissionType)
{
case CommManager.TransmissionType.Text:
try
{
ComPort.Write(msg);
if (DisplayMsgCallback != null)
DisplayMsgCallback(SharedAppObjs.MsgType.Outgoing, msg + "\n");
}
catch (Exception ex)
{
msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, string.Format("Error Writing To {0:S} (Text)\n" + ex.Message + "\n", ComPort.PortName));
flag = false;
}
break;
case CommManager.TransmissionType.Hex:
try
{
byte[] buffer = HexToByte(msg);
try
{
ComPort.Write(buffer, 0, buffer.Length);
}
catch (Exception ex)
{
string msg_ex = string.Format("Error Writing To {0:S} (Hex)\n" + ex.Message + "\n", ComPort.PortName);
if (DisplayMsgCallback != null)
DisplayMsgCallback(SharedAppObjs.MsgType.Error, msg_ex);
msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg_ex);
flag = false;
}
}
catch (Exception ex)
{
string msg_ex = string.Format("Com Port Error\n Port Number = {0:S} (Hex)\n" + ex.Message + "\n", ComPort.PortName);
if (DisplayMsgCallback != null)
DisplayMsgCallback(SharedAppObjs.MsgType.Error, msg_ex);
msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg_ex);
flag = false;
}
break;
default:
try
{
ComPort.Write(msg);
}
catch (Exception ex)
{
string msg_ex = string.Format("Error Writing To {0:S} (Default)\n" + ex.Message + "\n", ComPort.PortName);
if (DisplayMsgCallback != null)
DisplayMsgCallback(SharedAppObjs.MsgType.Error, msg_ex);
msgBox.UserMsgBox(SharedObjects.MainWin, MsgBox.MsgTypes.Error, msg_ex);
flag = false;
}
break;
}
return flag;
}