using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace BPASmartClient.MorkCL.Server
{
///
/// 其它服务类
///
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("LB100".ToModbusAdd(), 4).OnSuccess(s =>
//{
// for (byte i = 0; i < s.Length; i++) BoxDetection[i] = s[i];
//});
MyModbus.SlaveAddress = 1;
MyModbus.Read("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("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("LB100".ToModbusAdd(), 4).OnSuccess(s =>
// //{
// // for (byte i = 0; i < s.Length; i++) BoxDetection[i] = s[i];
// //});
// myDevice.Read("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("LW6".ToModbusAdd()).OnSuccess(s => { CurrentWeight = s; });
// Thread.Sleep(250);
// }), $"其它外部设备-{PortName}", true);
//});
}
public void WriteValue(string address, T value)
{
throw new NotImplementedException();
}
///
/// 除皮
///
///
public bool WeigherTare()
{
if (MyModbus is not null && MyModbus.IsConnected())
{
//ModbusRtu weigher = (ModbusRtu)MyModbus;
//MyModbus.SlaveAddress = 2;
MyModbus.SlaveAddress = 2;
return MyModbus.Write("LB4".ToModbusAdd(), true).IsSuccess;
}
else
return false;
}
///
/// 置零
///
///
public bool WeigherZero()
{
if (MyModbus is not null && MyModbus.IsConnected())
{
//ModbusRtu weigher = (ModbusRtu)MyModbus;
//weigher.SlaveAddress = 2;
MyModbus.SlaveAddress = 2;
return MyModbus.Write("LB3".ToModbusAdd(), true).IsSuccess;
}
else
return false;
}
}
}