|
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Controls;
-
- namespace BPASmartClient.MorkCL.Server
- {
- /// <summary>
- /// 其它服务类
- /// </summary>
- internal class OtherServer : OtherDeviceSet, IModbus
- {
- public void Init(string IP = "", int Port = 502, string PortName = "")
- {
- MyModbus = new ModbusRtu();
- MyModbus.WithModbusRtu(PortName).UseConnected(() =>
- {
- ModbusRtu myDevice = (ModbusRtu)MyModbus;
- myDevice.master.Transport.ReadTimeout = 3000;
- myDevice.master.Transport.WriteTimeout = 3000;
-
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- //设置站号
- myDevice.SlaveAddress = 1;
-
- //myDevice.Read<bool[]>("LB100".ToModbusAdd(), 4).OnSuccess(s =>
- //{
- // for (byte i = 0; i < s.Length; i++) BoxDetection[i] = s[i];
- //});
-
- myDevice.Read<ushort>("LW630".ToModbusAdd()).OnSuccess(s =>
- {
- for (byte i = 0; i < 4; i++)
- {
- BoxDetection[i] = s.GetBitValue((byte)(i+1));
- }
- });
- Thread.Sleep(50);
- myDevice.SlaveAddress = 2;
-
- myDevice.Read<float>("LW6".ToModbusAdd()).OnSuccess(s => { CurrentWeight = s; });
-
- Thread.Sleep(50);
- }), $"其它外部设备-{PortName}", true);
- });
- }
-
- public void WriteValue<T>(string address, T value)
- {
- throw new NotImplementedException();
- }
- /// <summary>
- /// 除皮
- /// </summary>
- /// <returns></returns>
- public bool WeigherTare()
- {
- if (MyModbus is not null && MyModbus.IsConnected())
- {
- ModbusRtu weigher = (ModbusRtu)MyModbus;
- weigher.SlaveAddress = 2;
- return weigher.Write<bool>("LB4".ToModbusAdd(), true).IsSuccess;
- }
- else
- return false;
- }
- /// <summary>
- /// 置零
- /// </summary>
- /// <returns></returns>
- public bool WeigherZero()
- {
- if (MyModbus is not null && MyModbus.IsConnected())
- {
- ModbusRtu weigher = (ModbusRtu)MyModbus;
- weigher.SlaveAddress = 2;
- return weigher.Write<bool>("LB3".ToModbusAdd(), true).IsSuccess;
- }
- else
- return false;
- }
- }
- }
|