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.IO; 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 { private static Nc N; private static String NCType; private static String NCIp; private static ushort NCPort; private Thread t; private Thread toolThread; 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)); 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() { PROC_MODE mode = PROC_MODE.AUTO; PROC_STATUS status = PROC_STATUS.ERROR; ushort procnum = 0; String MachNumber = ""; String ModelName = ""; String SFTVersion = ""; String PPName = ""; DateTime NcTime = new DateTime(); Dictionary Axes = new Dictionary(); Stopwatch sw = new Stopwatch(); List procAlarms = new List(); List Msg = new List(); PreAndPostPowerOnModel powerOn = new PreAndPostPowerOnModel(); List ncAlarms = new List(); List Lines = new List(); List keys = new List(); List headsTest = new List(); List axesNames = new List(); List functionality = new List(); AxisResetDataModel axesReset = new AxisResetDataModel(); ProcessDataModel processData = new ProcessDataModel(); ToolTableConfiguration toolsConfig = new ToolTableConfiguration(); List dataTable = new List(); List shanks = new List(); List famiies = new List(); List positions = new List(); List magazine = new List(); SiemensToolModel newTool = new SiemensToolModel() { FamilyName = "TEST1", ChildId = 1, Id = 6, MagazinePositionType = 1, ToolType = 110, LeftSize = 1, RightSize = 1, Rotation = ROTATION.CLOCKWHISE, LifeType = SIEMENS_LIFE_TYPE.COUNT, Cooling1 = true, Cooling2 = false, IsActive = true, InFixedPlace = true, IsInhibited = true, IsMeasured = true, InChangeTool = true, IsInUse = false, PreAlarm = false, EdgesData = null }; EdgeModel newEdge = new EdgeModel() { Id = 1, NominalLife = 22, PreAlmLife = 21, ResidualLife = 20, EdgeAdditionalParams = new Dictionary() { { "cuttingEdge", 1 }, { "lenght", 12 } } }; ShankModel newShank = new ShankModel() { Name = "MULTITOOLLISsimo", MagazinePositionType = 1, LeftSize = 1, RightSize = 1, IsEnabled = true, InFixedPlace = true, IsInhibited = true, InChangeTool = true, InUse = true }; List newMagazine = new List() { new NewToolInMagazineModel{ PositionId = 1, ToolId = 1 }, new NewToolInMagazineModel{ PositionId = 2, ToolId = 6 }, new NewToolInMagazineModel{ PositionId = 3, ToolId = 7 }, }; OffsetModel offset = new OffsetModel(); N = SetNcByType(); CmsError cmsError = N.NC_Connect(); cmsError = N.NC_RModelName(ref ModelName); bool error = false; string serial = ""; 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_RDateTime(ref NcTime); if (cmsError.IsError()) { error = true; SetError(Lines, cmsError); } cmsError = N.NC_RSerialNumber(ref serial); if (cmsError.IsError()) { error = true; SetError(Lines, cmsError); } if (cmsError.IsError()) { error = true; SetError(Lines, cmsError); } N.PLC_RPowerOnData(ref powerOn); if (cmsError.IsError()) { error = true; SetError(Lines, cmsError); } N.TOOLS_WStartEditData(); cmsError = N.TOOLS_WStartEditTooling(1); cmsError = N.TOOLS_WRestoreBackup(); cmsError = N.FILES_CopyProgram("C:\\TO_INI.INI", @"test\\OEM\\CMS\\SYS\\STEP\\testLocal", true); //cmsError = N.TOOLS_WStopEditTooling(1); // cmsError = N.TOOLS_WAddShank(ref newShank); // List a = new List() ; // N.TOOLS_RMagazineTools(1, ref a); // cmsError = N.TOOLS_WDeleteShank(4); //cmsError = N.TOOLS_WUpdateEdge(1, ref newEdge); //List c = new List(); //List b = new List(); // cmsError = N.TOOLS_RAvailableTools(ref c, ref b); //cmsError = N.TOOLS_RConfiguration(ref toolsConfig); //cmsError = N.TOOLS_WUpdatePosition(new PositionModel() //{ // Disabled = false, // MagazineId = 1, // PositionId = 1 //}); // cmsError = N.TOOLS_WUnloadToolInMagazine(1, 53); // cmsError = N.TOOLS_WLoadToolInMagazine(1, new NewToolInMagazineModel() { PositionId = 1, ToolId = 4 }); //cmsError = N.TOOLS_ROffset(1, ref offset); // cmsError = N.TOOLS_WStartEdit(); // cmsError = N.TOOLS_WOffset(3, offset); //cmsError = N.TOOLS_WAddTool(); //cmsError = N.PLC_WRefreshAllMessages(); //cmsError = N.PLC_RHeadsData(headsTest, 1); // cmsError = N.TOOLS_WLoadToolIntoShank(5, 1, 10); // cmsError = N.TOOLS_WAddEdge(24, ref newEdge); //cmsError = N.TOOLS_RToolsData(ref dataTable); //cmsError = N.TOOLS_RShanksData(ref shanks); //cmsError = N.TOOLS_RFamilyData(ref famiies); //cmsError = N.TOOLS_RMagazinePositions(ref positions); // cmsError = N.TOOLS_WAddTool(ref newTool); //ShankModel shank = new ShankModel(); // newShank.Name = newShank.Name + "1"; // cmsError = N.TOOLS_WAddShank(ref newShank); //FamilyModel family = new FamilyModel //{ // Name = "Da demo" //}; //cmsError = N.TOOLS_WDeleteTool(11); //// cmsError = N.TOOLS_WAddFamily(ref family); //cmsError = N.TOOLS_RConfiguration(ref toolsConfig); //cmsError = N.TOOLS_WUpdateTool(newTool); //cmsError = N.TOOLS_RMagazineTools(1, ref magazine); //cmsError = N.TOOLS_RAvailableTools(ref multiTools, ref tools); //cmsError = N.TOOLS_WUpdateMagazineTools(1, newMagazine); // N.PLC_WPowerOnData(6, true); //N.PLC_RFunctionAccess(ref functionality); //N.PLC_RAxesResetData(ref axesReset); //cmsError = N.PLC_RUserSoftKeys(ref keys); //cmsError = N.PLC_RNcSoftKeys(ref keys); //N.PROC_RSelectedProcess(ref procnum); //cmsError = N.PLC_WNcSoftKey(1); //// cmsError = N.PLC_WUserSoftKey(1); //N.PROC_RStatusAndData(1, ref processData); //sw.Restart(); //cmsError = N.AXES_RAxesNames(1, ref axesNames); ////cmsError = N.AXES_WSelectAxis(2); ////N.PROC_WSelectProcess(4); //cmsError = N.AXES_RSelectedAxis(ref axisId); ////cmsError = N.PLC_WRefreshAllMessages(); //cmsError = N.PLC_WHeadOverride(1, HEAD_OVERRIDE_SIGN.MINUS); //Dictionary messages = new Dictionary(); //cmsError = N.NC_GetTranslatedPlcMessages("it", ref messages); //MessageBox.Show(messages.Count.ToString() + " " + messages.FirstOrDefault()); ////cmsError = N.PROC_RSelectedProcess(ref procnum); sw.Stop(); 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.Id)); 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; if ((200 - (int)sw.ElapsedMilliseconds) < 0) Debug.WriteLine("Ritardo " + sw.ElapsedMilliseconds); Thread.Sleep(2000); } } public void ChangeTool() { if (NCType != "Fanuc") return; Nc ncFanuc = SetNcByType(); ToolModel toola = RicercaPerManina(1); CmsError cmsError = ncFanuc.NC_Connect(); byte id = 0; Stopwatch stopwatch = new Stopwatch(); int times = 0; long time = 0; while (true) { //id = 12; //ncFanuc.MEM_RWByte(Nc.W, 0, Nc.MEMORY_TYPE.Fanuc_R, 9000, 1, ref id); //id = 0; stopwatch.Restart(); // Read next tool area ncFanuc.MEM_RWByte(Nc.R, 0, Nc.MEMORY_TYPE.Fanuc_R, 9200, 1, ref id); // if not empty if (id != 0) { // Query ToolModel tool = RicercaPerManina(id); List list = new List() { id, tool.FamilyId, tool.ShankId, 2, 3, 4 }; // Write ids into nc memory ncFanuc.MEM_RWIntegerList(Nc.W, 0, Nc.MEMORY_TYPE.Fanuc_R, 9201, 0, ref list); id = 0; // Reset id to 0 ncFanuc.MEM_RWByte(Nc.W, 0, Nc.MEMORY_TYPE.Fanuc_R, 9200, 1, ref id); stopwatch.Stop(); times++; time += stopwatch.ElapsedMilliseconds + 100; // Write counters this.Invoke((ThreadStart)delegate () { mediaTestDbTXT.Text = (time / times).ToString(); lastTimeDbTXT.Text = stopwatch.ElapsedMilliseconds.ToString(); totalTimesTXT.Text = times.ToString(); }); } // Wait Thread.Sleep(100); } } public void ChangeToolOsai() { if (NCType != "Osai") 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 list = new List() { id, tool.FamilyId, tool.ShankId }; N.MEM_RWIntegerList(Nc.W, 0, Nc.MEMORY_TYPE.Osai_GW, 8001, 0, ref list); id = 0; N.MEM_RWByte(Nc.W, 0, Nc.MEMORY_TYPE.Osai_GW, 8000, 1, ref id); stopwatch.Stop(); this.Invoke((ThreadStart)delegate () { mediaTestDbTXT.Text = stopwatch.ElapsedMilliseconds.ToString(); }); } } } 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 Lines, CmsError cmsError) { if (!this.IsDisposed && this.InvokeRequired) { this.Invoke((ThreadStart)delegate () { Error.Text = cmsError.localizationKey; 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 = "192.168.139.1"; 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 Disconnect_Click(object sender, EventArgs e) { N.NC_Disconnect(); if (t != null) t.Abort(); //if (toolThread != null) // toolThread.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; } private void writeFileBtn_Click(object sender, EventArgs e) { List obj = new List() { new NcToolModel() { Id = 22, FamilyId = 1, OffsetId1 = 1, OffsetId2 = 2, OffsetId3 = 3, OffsetLength = 4, ResidualLife = 5, ResidualRevive = 6, ShankId = 7 }, new NcToolModel() { Id = 22, FamilyId = 1, OffsetId1 = 1, OffsetId2 = 2, OffsetId3 = 3, OffsetLength = 4, ResidualLife = 5, ResidualRevive = 6, ShankId = 7 } }; CmsError cms = N.TOOLS_WUpdateTools(obj); if (cms.IsError()) { MessageBox.Show(cms.localizationKey); } } private void readFileBtn_Click(object sender, EventArgs e) { using (FileStream fileStream = new FileStream("C:\\test.csv", FileMode.OpenOrCreate)) { CmsError cms = N.FILES_RProgramToFile(@"test\\OEM\\CMS\\SYS\\STEP\\Tools.csv", fileStream); if (cms.IsError()) { MessageBox.Show(cms.localizationKey); } } } private void NetworkReadButton_Click(object sender, EventArgs e) { List obj = new List() { new NcToolModel() { Id = 22, FamilyId = 1, OffsetId1 = 1, OffsetId2 = 2, OffsetId3 = 3, OffsetLength = 4, ResidualLife = 5, ResidualRevive = 6, ShankId = 7 }, new NcToolModel() { Id = 22, FamilyId = 1, OffsetId1 = 1, OffsetId2 = 2, OffsetId3 = 3, OffsetLength = 4, ResidualLife = 5, ResidualRevive = 6, ShankId = 7 } }; N.TOOLS_WUpdateTools(obj); } } }