using BPASmartClient.MorkCL.Model.Control;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BPASmartClient.MorkCL.Server
{
///
/// 机器人服务类
///
internal class RobotServer : RobotSet, IModbus
{
public RobotServer()
{
//MyModbus = new ModbusTcp();
}
public void Init(string IP = "", int Port = 502, string PortName = "")
{
//MyModbus = new ModbusTcp();
CommHelper.CreateModbusTcp(IP, Port).OnSuccess(S =>
{
MyModbus = S.Content;
TaskManage.GetInstance.StartLong(new Action(() =>
{
MyModbus.ReadCoil("GM600".ToModbusAdd(), 9).OnSuccess(s =>
{
for (byte i = 0; i < 3; i++)
{
MaterialPouringRequest[i] = s.Content[i * 3];
MaterialPouringComplete[i] = s.Content[i * 3 + 1];
DiningOutRequest[i] = s.Content[i * 3 + 2];
}
});
Thread.Sleep(20);
MyModbus.ReadCoil("GM620".ToModbusAdd()).OnSuccess((b) => { Home = b.Content; });
Thread.Sleep(20);
MyModbus.ReadCoil("GM630".ToModbusAdd(), 12).OnSuccess(s =>
{
for (byte i = 0; i < s.Content.Length; i++)
WarehousingComplete[i] = s.Content[i];
});
Thread.Sleep(20);
MyModbus.ReadHoldRegister("GI5".ToModbusAdd(), 1).OnSuccess(s => { TaskFeedback = (RobotMainTask)s.Content; });
IsIdle = (TaskFeedback == RobotMainTask.无任务 && Home);
Thread.Sleep(20);
MyModbus.ReadCoil("GM130".ToModbusAdd()).OnSuccess((b) => { RemoteMode = b.Content; });
Thread.Sleep(20);
MyModbus.ReadCoil("GM650".ToModbusAdd()).OnSuccess((b) => { AtCleanLoc = b.Content; });
Thread.Sleep(20);
}), $"机器人服务-{IP}", true);
});
//MyModbus.WithModbusTcp(IP, Port).UseConnected(() =>
//{
// ((ModbusTcp)MyModbus).master.Transport.ReadTimeout = 3000;
// ((ModbusTcp)MyModbus).master.Transport.WriteTimeout = 3000;
// TaskManage.GetInstance.StartLong(new Action(() =>
// {
// MyModbus.Read("GM600".ToModbusAdd(), 9).OnSuccess(s =>
// {
// for (byte i = 0; i < 3; i++)
// {
// MaterialPouringRequest[i] = s[i * 3];
// MaterialPouringComplete[i] = s[i * 3 + 1];
// DiningOutRequest[i] = s[i * 3 + 2];
// }
// });
// Thread.Sleep(20);
// MyModbus.Read("GM620".ToModbusAdd()).OnSuccess((b) => { Home = b; });
// Thread.Sleep(20);
// MyModbus.Read("GM630".ToModbusAdd(), 12).OnSuccess(s =>
// {
// for (byte i = 0; i < s.Length; i++)
// WarehousingComplete[i] = s[i];
// });
// Thread.Sleep(20);
// MyModbus.Read("GI5".ToModbusAdd(), 1).OnSuccess(s => { TaskFeedback = (RobotMainTask)s; });
// IsIdle = (TaskFeedback == RobotMainTask.无任务 && Home);
// Thread.Sleep(20);
// MyModbus.Read("GM130".ToModbusAdd(), 1).OnSuccess((b) => { RemoteMode = b; });
// Thread.Sleep(20);
// MyModbus.Read("GM650".ToModbusAdd(), 1).OnSuccess((b) => { AtCleanLoc = b; });
// Thread.Sleep(20);
// }), $"机器人服务-{IP}", true);
//});
}
public void WriteValue(string address, T value)
{
throw new NotImplementedException();
}
private void RobotInit()
{
if (EnableState && ProgramRuning) return;
//if (!CR_Data.Home)
//{
// Noticer.GetInstance.Show("煮面机器人初始化失败,机器人不在原点");
// return;
//}
while (!EnableState || !ProgramRuning)
{
//复位
RobotReset = true;
Thread.Sleep(1000);
RobotReset = false;
Thread.Sleep(500);
if (!EnableState)
{
//上使能
RobotEnable = true;
Thread.Sleep(1000);
RobotEnable = false;
Thread.Sleep(500);
}
//启动
RobotStart = true;
Thread.Sleep(1000);
RobotStart = false;
Thread.Sleep(1000);
}
}
public bool InitVariable()
{
if (MyModbus is not null && MyModbus.IsConnected())
{
try
{
MyModbus.WriteHoldRegister("GI0".ToModbusAdd(), 0);
MyModbus.WriteHoldRegister("GI1".ToModbusAdd(), 0);
for (int i = 0; i <= 8; i++)
{
MyModbus.WriteCoil(("GM" + (500 + i).ToString()).ToModbusAdd(), false);
}
return true;
}
catch (Exception)
{
return false;
}
}
else
return false;
}
}
}