Files
cms-core-active/CMS_CORE_Application/Form1.cs
T
Lucio Maranta 18b4e5452b
2018-02-02 16:25:24 +00:00

333 lines
11 KiB
C#

using CMS_CORE;
using CMS_CORE.Demo;
using CMS_CORE.Fanuc;
using CMS_CORE.Osai;
using CMS_CORE.Siemens;
using CMS_CORE_Application.ToolDatabase;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using static CMS_CORE_Application.ToolDatabase.ToolModels;
using static CMS_CORE_Library.DataStructures;
namespace CMS_CORE_Application
{
public partial class Form1 : Form
{
static Nc N;
static String NCType;
static String NCIp;
static ushort NCPort;
Thread t;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void connectDemo_Click(object sender, EventArgs e)
{
t = new Thread(new ThreadStart(ReadBasicData));
Thread toolThread = new Thread(new ThreadStart(ChangeTool));
if (NcCombo.SelectedItem == null)
return;
NCType = NcCombo.SelectedItem.ToString().Trim();
NCIp = TXTip.Text.Trim();
NCPort = ushort.Parse(TXTport.Text.Trim());
if (NCType != "")
{
Connect.Enabled = false;
Disconnect.Enabled = true;
t.Start();
toolThread.Start();
}
}
private void ReadBasicData()
{
Nc.PROC_MODE mode = Nc.PROC_MODE.AUTO;
Nc.PROC_STATUS status = Nc.PROC_STATUS.ERROR;
ushort procnum = 0;
String MachNumber = "";
String ModelName = "";
String SFTVersion = "";
String PPName = "";
DateTime NcTime = new DateTime();
Dictionary<String, double> Axes = new Dictionary<String, double>();
Stopwatch sw = new Stopwatch();
List<AlarmModel> procAlarms = new List<AlarmModel>();
List<AlarmModel> Msg = new List<AlarmModel>();
List<AlarmModel> ncAlarms = new List<AlarmModel>();
List<String> Lines = new List<string>();
PostPowerOnModel postPowerOn = new PostPowerOnModel();
N = SetNcByType();
CmsError cmsError = N.NC_Connect();
cmsError = N.NC_RModelName(ref ModelName);
N.NC_RProcessesNum(ref procnum);
N.NC_RMachineNumber(ref MachNumber);
bool error = false;
while (true)
{
sw.Restart();
cmsError = N.PROC_RMode(1, ref mode);
if (cmsError.IsError())
{
error = true;
SetError(Lines, cmsError);
}
cmsError = N.PROC_RStatus(1, ref status);
if (cmsError.IsError())
{
error = true;
SetError(Lines, cmsError);
}
cmsError = N.PROC_RActiveAlarms(1, ref procAlarms);
if (cmsError.IsError())
{
error = true;
SetError(Lines, cmsError);
}
cmsError = N.NC_RActiveAlarms(ref ncAlarms);
if (cmsError.IsError())
{
error = true;
SetError(Lines, cmsError);
}
cmsError = N.PLC_RActiveMessages(ref Msg);
if (cmsError.IsError())
{
error = true;
SetError(Lines, cmsError);
}
cmsError = N.NC_RSoftwareVersion(ref SFTVersion);
if (cmsError.IsError())
{
error = true;
SetError(Lines, cmsError);
}
//cmsError = N.PROC_RPPLines(1, ref Lines);
//if (cmsError.IsError())
//{
// error = true;
// SetError(Lines, cmsError);
//}
//cmsError = N.PROC_RPPName(1, ref PPName);
//if (cmsError.IsError())
//{
// error = true;
// SetError(Lines, cmsError);
//}
ProcessPartProgramData process = new ProcessPartProgramData();
cmsError = N.PROC_RPartProgramData(1, ref process);
cmsError = N.NC_RDateTime(ref NcTime);
if (cmsError.IsError())
{
error = true;
SetError(Lines, cmsError);
}
PreAndPostPowerOnModel preAndPostPowerOnModel = new PreAndPostPowerOnModel();
cmsError = N.Nc_RPowerOnData(ref preAndPostPowerOnModel);
if (cmsError.IsError())
{
error = true;
SetError(Lines, cmsError);
}
cmsError = N.Nc_WPowerOnData(10, true);
if (cmsError.IsError())
{
error = true;
SetError(Lines, cmsError);
}
if (!this.IsDisposed && this.InvokeRequired)
{
this.Invoke((ThreadStart)delegate ()
{
TXTPath1.Text = mode.ToString();
TXTStat1.Text = status.ToString();
TXTNProcess.Text = procnum.ToString();
TXTName.Text = ModelName;
TXTMachNum.Text = MachNumber;
TXTSft.Text = SFTVersion;
TXTTime.Text = sw.ElapsedMilliseconds.ToString() + " mS";
TXTAlm.Text = String.Join(Environment.NewLine, procAlarms.Select(x => x.message));
TXTAlmNc.Text = String.Join(Environment.NewLine, ncAlarms.Select(x => x.message));
TXTMsg.Text = String.Join(Environment.NewLine, Msg.Select(x => x.message));
TXTPPName.Text = PPName;
TXTNow.Text = NcTime.ToString();
if (error == false)
Error.Text = "";
TXTPPLines.Text = String.Join(Environment.NewLine, Lines);
Connect.Enabled = false;
Disconnect.Enabled = true;
});
}
error = false;
//sw.Stop();
if ((200 - (int)sw.ElapsedMilliseconds) < 0)
Debug.WriteLine("Ritardo " + sw.ElapsedMilliseconds);
Thread.Sleep(200);
}
}
public void ChangeTool()
{
if (NCType != "Fanuc")
return;
N = SetNcByType();
ToolModel toola = RicercaPerManina(1);
CmsError cmsError = N.NC_Connect();
byte id = 0;
Stopwatch stopwatch = new Stopwatch();
while (true)
{
stopwatch.Restart();
// Read next tool area
N.MEM_RWByte(Nc.R, 0, Nc.MEMORY_Type.Fanuc_R, 8000, 1, ref id);
// if not empty
if (id != 0)
{
ToolModel tool = RicercaPerManina(id);
List<int> list = new List<int>() { id, tool.FamilyId, tool.ShankId};
N.MEM_RWIntegerList(Nc.W, 0, Nc.MEMORY_Type.Fanuc_R, 8001, 0, ref list);
id = 0;
N.MEM_RWByte(Nc.W, 0, Nc.MEMORY_Type.Fanuc_R, 8000, 1, ref id);
stopwatch.Stop();
Debug.WriteLine(stopwatch.ElapsedMilliseconds.ToString());
}
Thread.Sleep(200);
}
}
private ToolModel RicercaPerManina(int maninaId)
{
ToolDatabaseContext dbCtx = new ToolDatabaseContext();
int idManina = Convert.ToInt32(maninaId);
// Find Tool by manina id
ToolModel tools = dbCtx
.Shanks
.Where(x => x.ManinaId == idManina)
.Join(dbCtx.Tools, m => m.ShankId, u => u.ShankId, (m, u) => u)
.FirstOrDefault();
return tools;
}
private void SetError(List<String> Lines, CmsError cmsError)
{
if (!this.IsDisposed && this.InvokeRequired)
{
this.Invoke((ThreadStart)delegate ()
{
Error.Text = cmsError.message;
TXTPPLines.Text = String.Join(Environment.NewLine, Lines);
Connect.Enabled = true;
Disconnect.Enabled = false;
});
}
}
private Nc SetNcByType()
{
if (NCType == "Siemens")
return new Nc_Siemens(500);
if (NCType == "Demo")
return new Nc_Demo(NCIp, NCPort);
if (NCType == "Osai")
return new Nc_Osai(NCIp, NCPort, 500);
if (NCType == "Fanuc")
return new Nc_Fanuc(NCIp, NCPort, 500);
return null;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (NcCombo.SelectedItem.ToString().Equals("Siemens"))
{
TXTip.Enabled = false;
TXTport.Enabled = false;
}
else if (NcCombo.SelectedItem.ToString().Equals("Fanuc"))
{
TXTip.Enabled = true;
TXTport.Enabled = true;
TXTip.Text = "10.69.36.33";
TXTport.Text = "8193";
}
else if (NcCombo.SelectedItem.ToString().Equals("Osai"))
{
TXTip.Enabled = true;
TXTport.Enabled = true;
TXTip.Text = "192.168.157.2";
TXTport.Text = "8080";
}
else if (NcCombo.SelectedItem.ToString().Equals("Demo"))
{
TXTip.Enabled = true;
TXTport.Enabled = true;
TXTip.Text = "localhost";
TXTport.Text = "8080";
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (t != null)
t.Abort();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void Disconnect_Click(object sender, EventArgs e)
{
if (t != null)
t.Abort();
TXTPath1.Text = "";
TXTStat1.Text = "";
TXTNProcess.Text = "";
TXTName.Text = "";
TXTMachNum.Text = "";
TXTSft.Text = "";
TXTTime.Text = "";
TXTAlm.Text = "";
TXTAlmNc.Text = "";
TXTMsg.Text = "";
TXTPPName.Text = "";
TXTNow.Text = "";
TXTPPLines.Text = "";
Connect.Enabled = true;
Disconnect.Enabled = false;
}
}
}