12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- using System.Net.NetworkInformation;
- using System.Text;
- namespace WCS.Core
- {
- public class PLC : EntityEx<PLCInfo>
- {
- public bool Ping { get; private set; }
- public PLC(PLCInfo ent) : base(ent)
- {
- if (Configs.PLCAccessorCreater != null)
- {
- Accessor = Configs.PLCAccessorCreater.Create(ent);
- }
- else
- throw new Exception("Configs.PLCAccessorCreater未赋值");
- Task.Run(() =>
- {
- while (true)
- {
- Ping = ping();
- Task.Delay(1000);
- }
- });
- }
- public IPLCAccessor Accessor { get; private set; }
- private bool ping(int timeout = 100)
- {
- if (Entity.IP == "1") return false;
- try
- {
- var objPingSender = new Ping();
- var objPinOptions = new PingOptions
- {
- DontFragment = true
- };
- const string data = "";
- var buffer = Encoding.UTF8.GetBytes(data);
- var objPinReply = objPingSender.Send(Entity.IP, timeout, buffer, objPinOptions);
- var strInfo = objPinReply.Status.ToString();
- return strInfo == "Success";
- }
- catch (Exception)
- {
- return false;
- }
- //var p = new Ping();
- //var res = p.Send(Entity.IP, timeout);
- //return res.Status == IPStatus.Success;
- ////return true;
- }
- }
- public interface IPLCAccessorCreater
- {
- IPLCAccessor Create(PLCInfo data);
- }
- public interface IPLCAccessor
- {
- void WriteBytes(ushort db, ushort address, byte[] data);
- byte[] ReadBytes(ushort db, ushort address, ushort length);
- }
- }
|