Esempio n. 1
0
 /// <summary>
 /// 读取大块存储器
 /// </summary>
 /// <param name="DBNumber"></param>
 /// <param name="start"></param>
 /// <param name="size"></param>
 /// <param name="buf"></param>
 /// <returns></returns>
 public bool ReadData(int DBNumber, int start, int size, byte[] buf)
 {
     lock (syncObj)
     {
         if (_s7Client.Connected())
         {
             int result = _s7Client.DBRead(DBNumber, start, size, buf);
             PlcUILinker.Log($"ReadData DB{DBNumber} off{start}-Read", _s7Client.ErrorText(result));
             Thread.Sleep(200); // 等待数据交换
             if (result != 0)
             {
                 Trace.Write("PLC 读取错误");
                 return(false);
             }
         }
     }
     return(true);
 }
Esempio n. 2
0
 /// <summary>
 /// 写入大块存储器
 /// </summary>
 /// <param name="dbNb"></param>
 /// <param name="start"></param>
 /// <param name="size"></param>
 /// <param name="buf"></param>
 /// <returns></returns>
 public bool WriteData(int dbNb, int start, int size, byte[] buf)
 {
     lock (syncObj)
     {
         if (_s7Client.Connected())
         {
             int result = _s7Client.DBWrite(dbNb, start, size, buf);
             PlcUILinker.Log($"WriteData DB{dbNb} off{start}-Write", _s7Client.ErrorText(result));
             Thread.Sleep(200); // 等待数据交换
             if (result != 0)
             {
                 Trace.Write("PLC 写入错误");
                 return(false);
             }
         }
     }
     return(true);
 }
Esempio n. 3
0
        public bool SetPartID(int clientId, string partId)
        {
            int result = 1;

            byte[] buf = new byte[256];
            S7.SetStringAt(buf, 0, 256, partId);
            lock (syncObj)
            {
                if (_s7Client.Connected())
                {
                    result = _s7Client.DBWrite(clientId, 0, 256, buf);
                    PlcUILinker.Log($"SetPartID DB{clientId} off0-256DBWrite", _s7Client.ErrorText(result));
                    if (result != 0)
                    {
                        return(false);
                    }
                    Thread.Sleep(200); // 等待数据交换
                }
            }
            return(result == 0);
        }
Esempio n. 4
0
 /// <summary>
 /// 写PLC标志
 /// </summary>
 /// <param name="serverID">存储器号</param>
 /// <param name="offset">标志位字节偏置</param>
 /// <param name="value">设置或者复位</param>
 /// <param name="pos">要设置的字节位位置</param>
 /// <returns></returns>
 private bool WritePlcFlags(int serverID, int offset, bool value, params int[] pos)
 {
     lock (syncObj)
     {
         byte[] buf    = new byte[1];
         int    result = _s7Client.DBRead(serverID, offset, 1, buf);
         PlcUILinker.Log($"WritePlcFlags DB{serverID} off{offset}-Read", _s7Client.ErrorText(result));
         Thread.Sleep(100);
         if (result != 0)
         {
             return(false);
         }
         foreach (int index in pos)
         {
             S7.SetBitAt(ref buf, 0, index, value);
         }
         result = _s7Client.DBWrite(serverID, offset, 1, buf);
         PlcUILinker.Log($"WritePlcFlags DB{serverID} off{offset}-Write", _s7Client.ErrorText(result));
         Thread.Sleep(100);
         //Debug.Assert(result == 0);
         return(result == 0);
     }
 }