139 lines
5.5 KiB
C#
139 lines
5.5 KiB
C#
|
|
using Sharp7;
|
|||
|
|
using Sharp7.Rx;
|
|||
|
|
using Sharp7.Rx.Enums;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Reactive.Disposables;
|
|||
|
|
using System.Reactive.Linq;
|
|||
|
|
using System.Reactive.Threading.Tasks;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
|
|||
|
|
namespace LangGuan.Command.Extension
|
|||
|
|
{
|
|||
|
|
public class PlcHelper2
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
public PlcHelper2() { }
|
|||
|
|
/// <summary>
|
|||
|
|
/// 设定设备值
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="ip"></param>
|
|||
|
|
/// <param name="key"></param>
|
|||
|
|
/// <param name="val"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task startDevice(string ip, string key, Int16 val)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (var disposables = new CompositeDisposable())
|
|||
|
|
{
|
|||
|
|
Console.WriteLine($"开始连接设备,ip:{ip},");
|
|||
|
|
// create a new PLC
|
|||
|
|
var plc = new Sharp7Plc(ip, 0, 1);
|
|||
|
|
disposables.Add(plc);
|
|||
|
|
// initialize the plc
|
|||
|
|
await plc.InitializeAsync();
|
|||
|
|
var t = await plc.ConnectionState.FirstAsync(c => c == ConnectionState.Connected).ToTask();
|
|||
|
|
await plc.SetValue<Int16>(key, val); // set a bit
|
|||
|
|
var result = await plc.GetValue<Int16>(key);
|
|||
|
|
Console.WriteLine($"监控写入结果:----- {result}");
|
|||
|
|
Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm ss")}----plc连接状态:{t}");
|
|||
|
|
// Console.WriteLine($"plc连接状态:{plc.ConnectionState.FirstAsync(c => c == ConnectionState.Connected).Any()}");
|
|||
|
|
// await plc.SetValue<bool>("DB2.DBX0.4", true); // set a bit
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine(ex.Message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public async Task startDevice502(string ip, string key, Int16 val)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
using (var disposables = new CompositeDisposable())
|
|||
|
|
{
|
|||
|
|
Console.WriteLine($"502开始连接设备,ip:{ip},");
|
|||
|
|
// create a new PLC
|
|||
|
|
var plc = new Sharp7Plc(ip, 0, 1,502);
|
|||
|
|
disposables.Add(plc);
|
|||
|
|
// initialize the plc
|
|||
|
|
await plc.InitializeAsync();
|
|||
|
|
var t = await plc.ConnectionState.FirstAsync(c => c == ConnectionState.Connected).ToTask();
|
|||
|
|
await plc.SetValue<Int16>(key, val); // set a bit
|
|||
|
|
var result = await plc.GetValue<Int16>(key);
|
|||
|
|
Console.WriteLine($"502监控写入结果:----- {result}");
|
|||
|
|
Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm ss")}--502--plc连接状态:{t}");
|
|||
|
|
// Console.WriteLine($"plc连接状态:{plc.ConnectionState.FirstAsync(c => c == ConnectionState.Connected).Any()}");
|
|||
|
|
// await plc.SetValue<bool>("DB2.DBX0.4", true); // set a bit
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine(ex.Message);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public async Task SetVal(string ip, string key, int val)
|
|||
|
|
{
|
|||
|
|
using (var disposables = new CompositeDisposable())
|
|||
|
|
{
|
|||
|
|
// create a new PLC
|
|||
|
|
var plc = new Sharp7Plc(ip, 0, 2);
|
|||
|
|
disposables.Add(plc);
|
|||
|
|
|
|||
|
|
// initialize the plc
|
|||
|
|
await plc.InitializeAsync();
|
|||
|
|
|
|||
|
|
//wait for the plc to get connected
|
|||
|
|
await plc.ConnectionState
|
|||
|
|
.FirstAsync(c => c == ConnectionState.Connected)
|
|||
|
|
.ToTask();
|
|||
|
|
|
|||
|
|
// await plc.SetValue<bool>("DB2.DBX0.4", true); // set a bit
|
|||
|
|
await plc.SetValue<int>(key, val); // set a bit
|
|||
|
|
// var bit = await plc.GetValue<int>("DB2.int4"); // get a bit
|
|||
|
|
|
|||
|
|
// create a nofication for data change in the plc
|
|||
|
|
//var notification = plc.CreateNotification<bool>("DB1.DBX0.2", TransmissionMode.OnChange, TimeSpan.FromMilliseconds(100))
|
|||
|
|
// .Where(b => b) //select rising edge
|
|||
|
|
// .Do(_ => doStuff())
|
|||
|
|
// .Subscribe();
|
|||
|
|
//disposables.Add(notification);
|
|||
|
|
|
|||
|
|
//wait for enter before ending the program
|
|||
|
|
// Console.ReadLine();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 取值
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"></typeparam>
|
|||
|
|
/// <param name="ip"></param>
|
|||
|
|
/// <param name="key"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public async Task<T> GetVal<T>(string ip, string key)
|
|||
|
|
{
|
|||
|
|
using (var disposables = new CompositeDisposable())
|
|||
|
|
{
|
|||
|
|
// create a new PLC
|
|||
|
|
var plc = new Sharp7Plc(ip, 0, 2);
|
|||
|
|
disposables.Add(plc);
|
|||
|
|
|
|||
|
|
// initialize the plc
|
|||
|
|
await plc.InitializeAsync();
|
|||
|
|
|
|||
|
|
//wait for the plc to get connected
|
|||
|
|
await plc.ConnectionState
|
|||
|
|
.FirstAsync(c => c == ConnectionState.Connected)
|
|||
|
|
.ToTask();
|
|||
|
|
|
|||
|
|
// await plc.SetValue<bool>("DB2.DBX0.4", true); // set a bit
|
|||
|
|
var bit = await plc.GetValue<T>(key); // get a bit
|
|||
|
|
return bit;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|