|
- using BPA.Communication;
- using BPA.Helper;
- using Microsoft.AspNetCore.Http;
-
- namespace CommcationLibray
- {
- /// <summary>
- /// modbusTcp连接基类
- /// </summary>
- public class ModbusTCP
- {
-
- ICommModbus commHelper;
- /// <summary>
- /// 连接modbusTcp
- /// </summary>
- /// <param name="ip">IP</param>
- /// <param name="Port">端口</param>
- /// <returns></returns>
- public async Task<Result> ConnectModbusTcp(string ip, int Port)
- {
- Result results = new Result();
- await Task.Run( new Action(() =>
- {
- CommHelper.CreateModbusTcp(ip, Port).OnSuccess(s =>
- {
- results.State = s.IsSuccess;
- results.Exception = s.Message;
- commHelper = s.Content;
- }).OnFailure(s =>
- {
- results.State = s.IsSuccess;
- results.Exception = s.Message;
- });
- }));
- return results;
- }
- /// <summary>
- /// 获取订单号
- /// </summary>
- /// <param name="address">读取保持寄存器 的地址</param>
- /// <typeparam name="T">需要返回的数据类型</typeparam>
- /// <param name="length">数据长度</param>
- /// <param name="format">数据格式</param>
- /// <returns></returns>
- public Result GetOrders<T>(string address, ushort length = 1, BPADataFormat format = BPADataFormat.ABCD)
- {
- Result results = new Result();
- try
- {
- if (commHelper != null)
- {
- var GetOrdersData = commHelper.ReadHoldRegister<T>(address, length,format);
- results.State = GetOrdersData.IsSuccess;
- results.Exception = GetOrdersData.Message;
- results.Datas = GetOrdersData.Content;
- }
- else
- {
- results.State = false;
- results.Exception = "ICommModbus 对象为空!!!";
- }
-
- }
- catch (Exception ex)
- {
- results.State = false;
- results.Exception = ex.Message;
- }
- return results;
- }
-
- /// <summary>
- /// 写入配方编号
- /// </summary>
- /// <typeparam name="T">写入的数据类型</typeparam>
- /// <param name="address">寄存器地址</param>
- /// <param name="value">写入的值</param>
- /// <param name="len">地址长度</param>
- /// <param name="format">数据格式</param>
- /// <returns></returns>
- public Result WriteRecipeNumber<T>(string address, T value, int len = 0, BPADataFormat format = BPADataFormat.ABCD)
- {
- Result results = new Result();
- try
- {
- if (commHelper != null)
- {
- var WriteState = commHelper.WriteHoldRegister<T>(address, value, len, format);
- results.State = WriteState.IsSuccess;
- results.Exception = WriteState.Message;
- }
- else
- {
- results.State = false;
- results.Exception = "ICommModbus 对象为空!!!";
- }
- }
- catch (Exception ex)
- {
- results.State = false;
- results.Exception = ex.Message;
- }
- return results;
- }
-
- /// <summary>
- /// 确定写入编号
- /// </summary>
- /// <param name="address">线圈地址</param>
- /// <param name="value">写入的值</param>
- /// <returns></returns>
- public Result DetermineWriteNumberCommand(string address, bool value)
- {
- return WriteCoilData(address, value);
- }
-
- /// <summary>
- /// 写入取消配料的点位
- /// </summary>
- /// <param name="address">线圈地址</param>
- /// <param name="value">写入线圈的值</param>
- /// <returns></returns>
- public Result WriteCancelIngredients(string address, bool value)
- {
- return WriteCoilData(address,value);
- }
-
- /// <summary>
- /// 读取取消配料的点位
- /// </summary>
- /// <param name="address">读取线圈的地址</param>
- /// <returns></returns>
- public Result ReadCancelIngredients(string address)
- {
- return ReadCoilData(address);
- }
-
- /// <summary>
- /// 获取设备状态
- /// </summary>
- /// <param name="address">读取线圈的地址</param>
- /// <returns></returns>
- public Result ReadDeviceStatus(string address)
- {
- return ReadCoilData(address);
- }
-
- /// <summary>
- /// 读取线圈数据
- /// </summary>
- /// <param name="address">读取线圈的地址</param>
- /// <returns></returns>
- private Result ReadCoilData(string address)
- {
- Result results = new Result();
- try
- {
- if (commHelper != null)
- {
- var readState = commHelper.ReadCoil(address);
- results.State = readState.IsSuccess;
- results.Datas = readState.Content;
- results.Exception = readState.Message;
- }
- else
- {
- results.State = false;
- results.Exception = "ICommModbus 对象为空!!!";
- }
- }
- catch (Exception ex)
- {
- results.State = false;
- results.Exception = ex.Message;
- }
-
- return results;
-
- }
-
- /// <summary>
- /// 写入线圈数据
- /// </summary>
- /// <param name="address"></param>
- /// <param name="value">写入线圈的值</param>
- /// <returns></returns>
- private Result WriteCoilData(string address,bool value)
- {
- Result results = new Result();
- try
- {
- if (commHelper != null)
- {
- var WriteState = commHelper.WriteCoil(address, value);
- results.State = WriteState.IsSuccess;
- results.Exception = WriteState.Message;
- }
- else
- {
- results.State = false;
- results.Exception = "ICommModbus 对象为空!!!";
- }
-
- }
- catch (Exception ex)
- {
- results.State = false;
- results.Exception = ex.Message;
- }
- return results;
- }
-
- }
- }
|