/// <summary>
/// Read PLC via SerialPort
/// </summary>
/// <param name="PlcPort">Serial Port address</param>
/// <returns></returns>
public static string ReadPLC(System.IO.Ports.SerialPort PlcPort)
{
string FCS_rxd;
string FCS = "";
string RXD = "";
string data = "";
Stopwatch stopwatch = new Stopwatch();
try
{
RXD = "";
data = "";
PlcPort.ReadTimeout = 1000;
RXD = PlcPort.ReadTo("\r");
}
catch (Exception)
{
return(null);
}
FCS_rxd = RXD.Substring(RXD.Length - 3, 2);
if (RXD.Substring(0, 1) == "@" && RXD.Length > 3) //response has data
{
RXD = RXD.Substring(0, RXD.Length - 3);
}
/*else if (RXD.Substring(0, 1) == "@" && RXD.Length == 11)//reponse has no data but error
* {
* MessageBox.Show("PLC comm error");
* return data;
* }*/
else if (RXD == "")
{
return(null);
}
FCS = GetFCS(RXD);
if (FCS == FCS_rxd)
{
data = RXD.Substring(23, RXD.Length - 23);
return(data);
}
else
{
return(null);
}
}