一、寫入PLC數據
[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 = "000A14010000D*" + 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.txtSend.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.txtRcv.Text = rcvstr;
string emsg = rcvstr.Substring(18, 4);
if (emsg != "0000")
{
plcCom.txtRcv.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 = "000A14010000D*" + 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.txtSend.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.txtRcv.Text = rcvstr;
string emsg = rcvstr.Substring(18, 4);
if (emsg != "0000")
{
plcCom.txtRcv.Text += "error code " + emsg;
MessageBox.Show("PLC comm. data error. " + emsg);
}
return true;
}
二、讀取PLC數據
[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 = "000A04010000D*" + 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.txtRcv.Text = ReadData;
string emsg = ReadData.Substring(18, 4);
if (emsg != "0000")
{
plcCom.txtRcv.Text += "error code " + emsg;
PlcReadBusy = false;
return false;
}
ReadData = ReadData.Substring(22);
PlcReadBusy = false;
return true;
}
}
摘自 #Define