Dear .
I have a big problem with the serial port. So I have two solution to read the serialport's message. the serial setting the same the computer. So I confirm that serial port settings is right.
But I have three serial port to working. I just receiving firstly port's info. other port can't received.
This is my part of code.
public class WindowsSerialPort : IDisposable
{
private SerialPort MySerialPort;
private Thread SerialPortThread;
private volatile bool UsingThread;
private string RecMessage;
private string CheckMessage;
private string TString;
private bool IsFullyRead;
public string PortName { set; get; }
public event SerialDataReceivedEvent SerialReceivedEventArgs;
public int Terminator { set; get; }
public int Starter { set; get; }
public bool ChkTerminator { set; get; }
public WindowsSerialPort(string _PortName, int BaudRate, Parity parity, int DataBits, StopBits stopbits, bool usingthread)
{
try
{
MySerialPort = new SerialPort();
MySerialPort.PortName = _PortName;
MySerialPort.BaudRate = BaudRate;
MySerialPort.Parity = parity;
MySerialPort.DataBits = DataBits;
MySerialPort.StopBits = stopbits;
MySerialPort.ReadTimeout = 500;
MySerialPort.WriteTimeout = 500;
PortName = _PortName;
UsingThread = usingthread;
}
catch (Exception ex)
{
new LogFileWriter("SerialPort.log").WriteExceptionLog("InitialPort", "Error", new Exception(ex.Message), "FIS", "Fis");
}
}
public void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
ReadData(sender as SerialPort);
}
void Serial_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
Clear();
}
private void ReadDataByThread()
{
while (true)
{
ReadData(MySerialPort);
}
}
public string ReadData(SerialPort port)
{
string result = string.Empty;
try
{
try
{
Thread.Sleep(100);
int count = port.BytesToRead;
if (count > 0)
{
byte[] readBuffer = new byte[count];
port.Read(readBuffer, 0, count);
if (ChkTerminator)
{
TString += System.Text.Encoding.ASCII.GetString(readBuffer);
if (TString.Contains(char.ConvertFromUtf32(Terminator)))
{
IsFullyRead = true;
if (CheckMessage.IsNotNull())
{
if (TString.Contains(CheckMessage))
RecMessage = TString;
else
IsFullyRead = false;
}
}
}
else
{
TString = System.Text.Encoding.ASCII.GetString(readBuffer);
if (TString.IsNotNull())
{
IsFullyRead = true;
if (CheckMessage.IsNotNull())
{
if (CheckMessage.IsNotNull())
{
if (TString.Contains(CheckMessage))
RecMessage = TString;
else
IsFullyRead = false;
}
}
}
}
if (IsFullyRead)
{
result = TString;
this.Clear();
string[] receivelst = result.Split(new string[] { char.ConvertFromUtf32(Terminator) }, StringSplitOptions.RemoveEmptyEntries);
Delegate[] delegateAry = SerialReceivedEventArgs.GetInvocationList();
if (delegateAry.Length > 0)
{
foreach (string str in receivelst)
{
string message = RemoveNonAsciiChars(str);
foreach (SerialDataReceivedEvent handler in delegateAry)
{
handler.BeginInvoke(this, message, new AsyncCallback((iar) =>
{
SerialDataReceivedEvent thisDelegate = (SerialDataReceivedEvent)iar.AsyncState;
thisDelegate.EndInvoke(iar);
}), handler);
}
}
}
}
}
}
catch (Exception ex)
{
new LogFileWriter("SerialPort.log").WriteExceptionLog("ReadPort", "Error", new Exception(ex.Message), "FIS", "Fis");
}
}
catch (TimeoutException ex)
{
new LogFileWriter("SerialPort.log").WriteExceptionLog("ReadPortTimeout", "Error", new Exception(ex.Message), "FIS", "Fis");
}
return result;
}Plse help to me . thanks