1. Write PLC Data
[Csharp]
Public static bool PlcWrite (int adres, int count, string SendData)
{
If (Sys. PlcComUse = 0)
Return true;
String s_adres = (adres + Sys. PlcBaseAddress). ToString ("0 #####");
String s_count = count. ToString ("X"). PadLeft (4, '0 ');
String hdr = "500000FF03FF00 ";
String s_data = "000a1401_d *" + s_adres + s_count + SendData;
String n = (s_data.Length.ToString ("X"). PadLeft (4, '0 ');
String dtfram = (hdr + n + s_data );
Byte [] msg = Encoding. ASCII. GetBytes (dtfram );
PlcCom.txt Send. Text = "ABC ";
Application. DoEvents ();
If (Sys. Deb! = 0)
Return true;
Int byteSend = Sock. Send (msg );
System. Text. Encoding encoding = System. Text. Encoding. UTF8;
String smsg = encoding. GetString (msg );
System. DateTime Stime = DateTime. Now;
TimeSpan Ctime = new TimeSpan (10000000 );
Do
{
TimeSpan Ptime = (DateTime. Now-Stime );
If (Ptime> Ctime)
{
MessageBox. Show ("PLC comm. time out error .");
Return false;
}
Application. DoEvents ();
} While (Sock. Available = 0 );
Int byteRec = Sock. Receive (RcvBytes );
String rcvstr = System. Text. Encoding. UTF8.GetString (RcvBytes );
PlcCom.txt Rcv. Text = rcvstr;
String emsg = rcvstr. Substring (18, 4 );
If (emsg! = "0000 ")
{
PlcCom.txt Rcv. Text + = "error code" + emsg;
MessageBox. Show ("PLC comm. data error." + emsg );
}
Return true;
}
Public static bool PlcWrite (int adres, int count, string SendData)
{
If (Sys. PlcComUse = 0)
Return true;
String s_adres = (adres + Sys. PlcBaseAddress). ToString ("0 #####");
String s_count = count. ToString ("X"). PadLeft (4, '0 ');
String hdr = "500000FF03FF00 ";
String s_data = "000a1401_d *" + s_adres + s_count + SendData;
String n = (s_data.Length.ToString ("X"). PadLeft (4, '0 ');
String dtfram = (hdr + n + s_data );
Byte [] msg = Encoding. ASCII. GetBytes (dtfram );
PlcCom.txt Send. Text = "ABC ";
Application. DoEvents ();
If (Sys. Deb! = 0)
Return true;
Int byteSend = Sock. Send (msg );
System. Text. Encoding encoding = System. Text. Encoding. UTF8;
String smsg = encoding. GetString (msg );
System. DateTime Stime = DateTime. Now;
TimeSpan Ctime = new TimeSpan (10000000 );
Do
{
TimeSpan Ptime = (DateTime. Now-Stime );
If (Ptime> Ctime)
{
MessageBox. Show ("PLC comm. time out error .");
Return false;
}
Application. DoEvents ();
} While (Sock. Available = 0 );
Int byteRec = Sock. Receive (RcvBytes );
String rcvstr = System. Text. Encoding. UTF8.GetString (RcvBytes );
PlcCom.txt Rcv. Text = rcvstr;
String emsg = rcvstr. Substring (18, 4 );
If (emsg! = "0000 ")
{
PlcCom.txt Rcv. Text + = "error code" + emsg;
MessageBox. Show ("PLC comm. data error." + emsg );
}
Return true;
}
Ii. Reading PLC Data
[Csharp]
Public static bool PlcRead (int adres, int count, ref string ReadData)
{
If (Sys. PlcComUse = 0)
Return true;
PlcReadBusy = true;
String s_adres = (adres + Sys. PlcBaseAddress). ToString ("0 #####");
String s_count = count. ToString ("0 ###");
String hdr = "500000FF03FF00 ";
String s_data = "000a0401_d *" + s_adres + s_count;
String n = (s_data.Length.ToString ("X"). PadLeft (4, '0 ');
String dtfram = (hdr + n + s_data );
Byte [] msg = Encoding. ASCII. GetBytes (dtfram );
If (Sys. Deb! = 0)
{
PlcReadBusy = false;
Return true;
}
Int byteSend = Sock. Send (msg );
System. Text. Encoding encoding = System. Text. Encoding. UTF8;
String smsg = encoding. GetString (msg );
System. DateTime Stime = DateTime. Now;
TimeSpan Ctime = new TimeSpan (10000000 );
Do
{
TimeSpan Ptime = (DateTime. Now-Stime );
If (Ptime> Ctime)
{
MessageBox. Show ("PLC comm. time out error .");
PlcReadBusy = false;
Return false;
}
Application. DoEvents ();
} While (Sock. Available = 0 );
Int byteRec = Sock. Receive (RcvBytes );
ReadData = System. Text. Encoding. UTF8.GetString (RcvBytes );
PlcCom.txt Rcv. Text = ReadData;
String emsg = ReadData. Substring (18, 4 );
If (emsg! = "0000 ")
{
PlcCom.txt Rcv. Text + = "error code" + emsg;
PlcReadBusy = false;
Return false;
}
ReadData = ReadData. Substring (22 );
PlcReadBusy = false;
Return true;
}
}
From # Define