程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#通過網線讀取三菱PLC數據

c#通過網線讀取三菱PLC數據

編輯:C#入門知識

一、寫入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
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved