public static bool PlcWrite(int adres int count string SendData)
{
if (SysPlcComUse == )
return true;
string s_adres = (adres + SysPlcBaseAddress)ToString(#####);
string s_count = countToString(X)PadLeft( );
string hdr = FFFF;
string s_data = AD* + s_adres + s_count + SendData;
string n = (s_dataLengthToString(X))PadLeft( );
string dtfram = (hdr + n + s_data);
byte[] msg = EncodingASCIIGetBytes(dtfram);
plcComtxtSendText = ABC;
ApplicationDoEvents();
if (SysDeb != )
return true;
int byteSend = SockSend(msg);
SystemTextEncoding encoding = SystemTextEncodingUTF;
string smsg = encodingGetString(msg);
SystemDateTime Stime = DateTimeNow;
TimeSpan Ctime = new TimeSpan();
do
{
TimeSpan Ptime = (DateTimeNow Stime);
if (Ptime > Ctime)
{
MessageBoxShow(PLC comm time out error);
return false;
}
ApplicationDoEvents();
} while (SockAvailable == );
int byteRec = SockReceive(RcvBytes);
string rcvstr = SystemTextEncodingUTFGetString(RcvBytes);
plcComtxtRcvText = rcvstr;
string emsg = rcvstrSubstring( );
if (emsg != )
{
plcComtxtRcvText += error code + emsg;
MessageBoxShow(PLC comm data error + emsg);
}
return true;
}
二读取PLC数据
view plainprint?
public static bool PlcRead(int adres int count ref string ReadData)
{
if (SysPlcComUse == )
return true;
PlcReadBusy = true;
string s_adres = (adres + SysPlcBaseAddress)ToString(#####);
string s_count = countToString(###);
string hdr = FFFF;
string s_data = AD* + s_adres + s_count;
string n = (s_dataLengthToString(X))PadLeft( );
string dtfram = (hdr + n + s_data);
byte[] msg = EncodingASCIIGetBytes(dtfram);
if (SysDeb != )
{
PlcReadBusy = false;
return true;
}
int byteSend = SockSend(msg);
SystemTextEncoding encoding = SystemTextEncodingUTF;
string smsg = encodingGetString(msg);
SystemDateTime Stime = DateTimeNow;
TimeSpan Ctime = new TimeSpan();
do
{
TimeSpan Ptime = (DateTimeNow Stime);
if (Ptime > Ctime)
{
MessageBoxShow(PLC comm time out error);
PlcReadBusy = false;
return false;
}
ApplicationDoEvents();
} while (SockAvailable == );
int byteRec = SockReceive(RcvBytes);
ReadData = SystemTextEncodingUTFGetString(RcvBytes);
plcComtxtRcvText = ReadData;
string emsg = ReadDataSubstring( );
if (emsg != )
{
plcComtxtRcvText += error code + emsg;
PlcReadBusy = false;
return false;
}
ReadData = ReadDataSubstring();
PlcReadBusy = false;
return true;
}
}