|
- 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 OtherServer()
- {
- //MyModbus = new ModbusRtu();
- }
- public void Init(string IP = "", int Port = 502, string PortName = "")
- {
- //MyModbus = new ModbusRtu();
- CommHelper.CreateModbusRtu(PortName).OnSuccess(s =>
- {
- MyModbus = s.Content;
- TaskManage.GetInstance.StartLong(new Action(() =>
- {
- //设置站号
- //(ModbusCommBase).SlaveAddress = 1;
-
- //myDevice.Read<bool[]>("LB100".ToModbusAdd(), 4).OnSuccess(s =>
- //{
- // for (byte i = 0; i < s.Length; i++) BoxDetection[i] = s[i];
- //});
- MyModbus.SlaveAddress = 1;
- MyModbus.Read<ushort>("LW630".ToModbusAdd()).OnSuccess(s =>
- {
- for (byte i = 0; i < 4; i++)
- {
- BoxDetection[i] = s.Content.GetBitValue((byte)(i + 1));
- }
- });
- Thread.Sleep(250);
- //myDevice.SlaveAddress = 2;
- MyModbus.SlaveAddress = 2;
- MyModbus.Read<float>("LW6".ToModbusAdd()).OnSuccess(s => { CurrentWeight = s.Content; });
-
- Thread.Sleep(250);
- }), $"其它外部设备-{PortName}", true);
- });
-
- //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(250);
- // myDevice.SlaveAddress = 2;
-
- // myDevice.Read<float>("LW6".ToModbusAdd()).OnSuccess(s => { CurrentWeight = s; });
-
- // Thread.Sleep(250);
- // }), $"其它外部设备-{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;
- //MyModbus.SlaveAddress = 2;
- MyModbus.SlaveAddress = 2;
- return MyModbus.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;
- MyModbus.SlaveAddress = 2;
- return MyModbus.Write<bool>("LB3".ToModbusAdd(), true).IsSuccess;
- }
- else
- return false;
- }
- }
- }
|