当前位置:编程学习 > C#/ASP.NET >>

c#通过网线读取三菱PLC数据

一、写入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")
            {
     &nbs

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,