熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2022-06-13   來源: .NET編程 

  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;
        }
    }


From:http://tw.wingwit.com/Article/program/net/201311/13889.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.