Before this question I post similar question at SerialPort DataReceived Event.
So, first of all , let me apologize first for making duplicate question.
But if you see below coding of me, you will found that I trying multiple ways (I mean, slightly different approach) to get the best solution which need to interact with serial ports.
So, Let's say that I have one console application project.
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
MyTest obj = new MyTest();
}
}
}
Then , two classes called MyTest and MyTest2 ...
MyTest
namespace ConsoleApplication2
{
public class MyTest
{
public SerialPort SPCOM4;
public MyTest()
{
SPCOM4 = new SerialPort();
SPCOM4.DataReceived += new SerialDataReceivedEventHandler(SPCOM4_DataReceived);
if(this.SerialPortOpen(SPCOM4, "4"))
{
this.SendToPort(SPCOM4, "[Class Name]MyTest1...");
MyTest2 objMyTest2 = new MyTest2();
Console.ReadLine();
}
}
void SPCOM4_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string data = SPCOM4.ReadExisting();
Console.Write(data.Replace("\r", "\n"));
}
private bool SerialPortOpen(System.IO.Ports.SerialPort objCom, string portName)
{
bool blnOpenStatus = false;
try
{
objCom.PortName = "COM" + portName;
objCom.BaudRate = 9600;
objCom.DataBits = 8;
int SerParity = 2;
int SerStop = 0;
switch (SerParity)
{
case 0:
objCom.Parity = System.IO.Ports.Parity.Even;
break;
case 1:
objCom.Parity = System.IO.Ports.Parity.Odd;
break;
case 2:
objCom.Parity = System.IO.Ports.Parity.None;
break;
case 3:
objCom.Parity = System.IO.Ports.Parity.Mark;
break;
}
switch (SerStop)
{
case 0:
objCom.StopBits = System.IO.Ports.StopBits.One;
break;
case 1:
objCom.StopBits = System.IO.Ports.StopBits.Two;
break;
}
objCom.RtsEnable = false;
objCom.DtrEnable = false;
objCom.Handshake = System.IO.Ports.Handshake.XOnXOff;
objCom.Open();
blnOpenStatus = true;
}
catch (Exception ex)
{
throw ex;
}
return blnOpenStatus;
}
private bool SendToPort(System.IO.Ports.SerialPort objCom, string strText)
{
try
{
int STX = 0x2;
int ETX = 0x3;
if (objCom.IsOpen && strText != "")
{
objCom.Write(Char.ConvertFromUtf32(STX) + strText + Char.ConvertFromUtf32(ETX));
}
}
catch (Exception ex)
{
throw ex;
}
return true;
}
}
}
MyTest2
namespace ConsoleApplication2
{
public class MyTest2
{
public SerialPort SPCOM4;
public MyTest2()
{
SPCOM4 = new SerialPort();
SPCOM4.DataReceived += new SerialDataReceivedEventHandler(SPCOM4_DataReceived);
if(this.SerialPortOpen(SPCOM4, "4"))
{
this.SendToPort(SPCOM4, "[Class Name]MyTest2...");
Console.ReadLine();
}
}
void SPCOM4_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string data = SPCOM4.ReadExisting();
Console.Write(data.Replace("\r", "\n"));
}
private bool SerialPortOpen(System.IO.Ports.SerialPort objCom, string portName)
{
bool blnOpenStatus = false;
try
{
objCom.PortName = "COM" + portName;
objCom.BaudRate = 9600;
objCom.DataBits = 8;
int SerParity = 2;
int SerStop = 0;
switch (SerParity)
{
case 0:
objCom.Parity = System.IO.Ports.Parity.Even;
break;
case 1:
objCom.Parity = System.IO.Ports.Parity.Odd;
break;
case 2:
objCom.Parity = System.IO.Ports.Parity.None;
break;
case 3:
objCom.Parity = System.IO.Ports.Parity.Mark;
break;
}
switch (SerStop)
{
case 0:
objCom.StopBits = System.IO.Ports.StopBits.One;
break;
case 1:
objCom.StopBits = System.IO.Ports.StopBits.Two;
break;
}
objCom.RtsEnable = false;
objCom.DtrEnable = false;
objCom.Handshake = System.IO.Ports.Handshake.XOnXOff;
objCom.Open();
blnOpenStatus = true;
}
catch (Exception ex)
{
throw ex;
}
return blnOpenStatus;
}
private bool SendToPort(System.IO.Ports.SerialPort objCom, string strText)
{
try
{
int STX = 0x2;
int ETX = 0x3;
if (objCom.IsOpen && strText != "")
{
objCom.Write(Char.ConvertFromUtf32(STX) + strText + Char.ConvertFromUtf32(ETX));
}
}
catch (Exception ex)
{
throw ex;
}
return true;
}
}
}
After I run my application, I found that the output message which is not like the way as I expected.
"[Class Name]MyTest2..."
Only that message come out.
My Question is why below code did not run correctly. Why DataReceived Event do not fire for first method.
this.SendToPort(SPCOM4, "[Class Name]MyTest1...");
Please , let me get suggestion.