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