C # Reading Mitsubishi PLC data through a network cable

Source: Internet
Author: User

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
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.