在此问题发出之前,我在SerialPort DataReceived Event发布了类似的问题。
所以,首先,请先让我道歉,提出重复的问题 但是如果你看到下面我的编码,你会发现我尝试了多种方式(我的意思是,稍微不同的方法)来获得需要与串口交互的最佳解决方案。
所以,假设我有一个控制台应用程序项目。
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
MyTest obj = new MyTest();
}
}
}
然后,两个名为MyTest和MyTest2的类......
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;
}
}
}
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;
}
}
}
运行我的应用程序后,我发现输出消息不像我预期的那样。
"[Class Name]MyTest2..."
只有那条消息出来了。
我的问题是以下代码无法正常运行的原因。 为什么DataReceived事件不会为第一种方法触发。
this.SendToPort(SPCOM4, "[Class Name]MyTest1...");
请让我得到建议。