From e7e9d26b75abbf0ea0b7d6fb8768dcc0984f4010 Mon Sep 17 00:00:00 2001 From: Nicola Pievani Date: Mon, 27 Feb 2023 19:28:46 +0100 Subject: [PATCH] Aggiunte funzioni per gestione CN Axium DripFeed --- My Project/AssemblyInfo.vb | 4 +- NcComm/CNCommunication.vb | 26 +- NcComm/M_Num_old.vb | 1638 +++++++++++++++++++----------------- Utility/Utility.vb | 22 +- 4 files changed, 897 insertions(+), 793 deletions(-) diff --git a/My Project/AssemblyInfo.vb b/My Project/AssemblyInfo.vb index 7d9de92..f4f30fc 100644 --- a/My Project/AssemblyInfo.vb +++ b/My Project/AssemblyInfo.vb @@ -62,5 +62,5 @@ Imports System.Windows ' by using the '*' as shown below: ' - - + + diff --git a/NcComm/CNCommunication.vb b/NcComm/CNCommunication.vb index 8517d99..4c2fbbb 100644 --- a/NcComm/CNCommunication.vb +++ b/NcComm/CNCommunication.vb @@ -2,6 +2,7 @@ Imports System.Globalization Imports System.Collections.ObjectModel Imports EgtUILib +Imports OmagCUT.Num Public Class CNCommunication @@ -354,8 +355,8 @@ Public Class CNCommunication If Not String.IsNullOrEmpty(sVal) Then m_CN.SetCnDataVar(CN_generico.CnData.Remote, sVal) End If - ' Inizializzo la comunicazione - m_CN.Init() + ' Inizializzo la comunicazione + m_CN.Init() Catch ex As Exception ' set status to statusbar @@ -907,28 +908,49 @@ Public Class CNCommunication End If ' Se richiesta successiva attivazione, faccio reset If bActivate Then + EgtOutLog("Call function: DGeneralFunctions_Reset") + TimeSpanInit() m_CN.DGeneralFunctions_Reset() + EgtOutLog(TimeSpanEnd()) End If ' Attesa opportuna System.Threading.Thread.Sleep(m_MainWindow.m_CurrentMachine.nResetSendDelay) If m_nNCType = 1 Then ' Cancellazione eventuale file già presente con lo stesso nome (ignoro eventuale errore) + EgtOutLog("Delete_NC_prog(sNumProg)") + TimeSpanInit() m_CN.Delete_NC_prog(sNumProg) + EgtOutLog(TimeSpanEnd()) ' Attesa opportuna System.Threading.Thread.Sleep(100) End If + EgtOutLog("Download_NC_prog(sCncPath, sNumProg)") + TimeSpanInit() bOk = bOk AndAlso (m_CN.Download_NC_prog(sCncPath, sNumProg) = 0) + If m_nNCType = 1 AndAlso TypeOf m_CN Is NumNCOld AndAlso DirectCast(m_CN, NumNCOld).bIsDripFeed Then + ' eventauli cambi di stato sono trattati all'interno della funzione + Return True + End If + EgtOutLog(TimeSpanEnd()) + ' Attesa opportuna System.Threading.Thread.Sleep(100) ' Se richiesta attivazione If bActivate Then ' Attivazione programma + EgtOutLog("ActivateProgram(nNumProg)") + TimeSpanInit() bOk = bOk AndAlso (m_CN.ActivateProgram(nNumProg) = 0) + EgtOutLog(TimeSpanEnd()) ' Attesa opportuna System.Threading.Thread.Sleep(100) ' Modalità automatica + EgtOutLog("DGeneralFunctions_WriteCncMode(0)") + TimeSpanInit() bOk = bOk AndAlso (m_CN.DGeneralFunctions_WriteCncMode(0) = 0) + EgtOutLog(TimeSpanEnd()) End If + ' Messaggio con risultato If bOk Then m_CurrProjPage.SetInfoMessage(EgtMsg(90317)) 'Programma CN trasmesso con successo diff --git a/NcComm/M_Num_old.vb b/NcComm/M_Num_old.vb index b4796b6..d1f0504 100644 --- a/NcComm/M_Num_old.vb +++ b/NcComm/M_Num_old.vb @@ -9,11 +9,15 @@ Imports System.Text Imports System.Windows.Forms Imports System.Runtime.InteropServices Imports EgtUILib +Imports System.IO +Imports System.Threading Namespace Num -Public Class NumNCOld - Inherits CN_generico + Public Class NumNCOld + Inherits CN_generico + + Public bIsDripFeed As Boolean = False #Region " Classes" #End Region @@ -79,6 +83,8 @@ Public Class NumNCOld Const TSi = 1, TFi = 2, SPi = 3, THi = 4, INI = 5, TABSYMB = 10, ALLMODULE = 16, CMODULE = 65, TXTFILE = 97 Const IHMFNT = 98, IHMBMP = 99, IHMRSC = 100, IHMINI = 101, IHMTXT = 102, IHMICO = 103, IHMCOD = 104 + Const DNC_MODE_FIRST = &H3A00, DNC_MODE_CONTINUE = &H3B00, DNC_MODE_ABORT = &H3C00 + Const DNC_IN_PROGRES = &HC8, DNC_RING_BUFFER_FULL = &H7 #End Region #Region " Structures" @@ -115,7 +121,7 @@ Public Class NumNCOld Dim Type As Long End Structure - _ + Public Structure tDNC20_ObjectList Public ElemNum As UInt16 Public First As UInt16 @@ -123,7 +129,7 @@ Public Class NumNCOld End Structure Public Structure My_data_struct - _ + Public data As Byte() End Structure @@ -209,6 +215,7 @@ Public Class NumNCOld Declare Function UpLoadFile Lib "dnc2.dll" (ByVal Port As Int16, ByVal prog As Int32, ByVal filename As String) As Int16 Declare Function DownLoadFile Lib "dnc2.dll" (ByVal Port As Int16, ByVal prog As Int32, ByVal filename As String) As Int16 + Declare Function DownLoadPPPFileHSL Lib "dnc2.dll" (ByVal Port As Int16, ByVal prog As Int32, ByVal ringsize As Int32, ByVal filename As String, ByVal Mode As UInt16, ByRef Index As UInt16) As Int16 Declare Function DncDeleteFile Lib "dnc2.dll" (ByVal Port As Int16, ByVal prog As Int32) As Int16 Declare Function DNC20_Position_OM Lib "dnc20_2.dll" (ByVal Port As Int16, ByVal Groupe As Int16, ByRef d As My_data_struct, ByVal Mode As UInt16) As Int16 @@ -232,928 +239,985 @@ Public Class NumNCOld #Region " Constructor" - Sub New() - n_state = 0 - sz_NC_error_messages = New List(Of String) - End Sub + Sub New() + n_state = 0 + sz_NC_error_messages = New List(Of String) + End Sub - Public Overrides Function Init() As Boolean + Public Overrides Function Init() As Boolean - n_state = 0 + n_state = 0 - ' Stringhe valide : "COM" o "XIP01, 0, 10, 0, 0, 0" - Dim nret As Integer = NC_connect(m_szCnDataVar(CnData.Comm)) - If nret <> 0 Then Return False + ' Stringhe valide : "COM" o "XIP01, 0, 10, 0, 0, 0" + Dim nret As Integer = NC_connect(m_szCnDataVar(CnData.Comm)) + If nret <> 0 Then Return False - n_state = 2 + n_state = 2 - ' Create a timer and set an half second interval. - aTimer = New System.Timers.Timer() + ' Create a timer and set an half second interval. + aTimer = New System.Timers.Timer() - Dim nRefresh As Integer = 200 - Dim nTemp As Integer - If Integer.TryParse(m_szCnDataVar(CnData.Refresh), nTemp) Then - nRefresh = nTemp - End If - - aTimer.Interval = nRefresh - - ' Hook up the Elapsed event for the timer. - AddHandler aTimer.Elapsed, AddressOf OnTimedEvent - - aTimer.AutoReset = True ' Have the timer fire repeated events (true is the default) - - aTimer.Enabled = True ' Start the timer - - Return n_state >= 2 - End Function - - Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs) - - If Not b_busy Then - aTimer.Enabled = False - b_busy = True - - nCount += 1 - - Read_machine_pos() ' leggo posizioni assi - - If nCount = 2 Or nCount = 4 Then - timer_read_var() ' leggo un mucchio di variabili in un colpo solo + Dim nRefresh As Integer = 200 + Dim nTemp As Integer + If Integer.TryParse(m_szCnDataVar(CnData.Refresh), nTemp) Then + nRefresh = nTemp End If - If nCount = 4 Then - nCount = 0 - get_errors() ' leggo eventueli errori PLC e CN + aTimer.Interval = nRefresh + + ' Hook up the Elapsed event for the timer. + AddHandler aTimer.Elapsed, AddressOf OnTimedEvent + + aTimer.AutoReset = True ' Have the timer fire repeated events (true is the default) + + aTimer.Enabled = True ' Start the timer + + Return n_state >= 2 + End Function + + Private Sub OnTimedEvent(source As Object, e As System.Timers.ElapsedEventArgs) + + If Not b_busy Then + aTimer.Enabled = False + b_busy = True + + nCount += 1 + + Read_machine_pos() ' leggo posizioni assi + + If nCount = 2 Or nCount = 4 Then + timer_read_var() ' leggo un mucchio di variabili in un colpo solo + End If + + If nCount = 4 Then + nCount = 0 + get_errors() ' leggo eventueli errori PLC e CN + End If + + b_busy = False + aTimer.Enabled = True End If - b_busy = False - aTimer.Enabled = True - End If + End Sub - End Sub + Function NC_connect(sz_connect_string As String) As Int16 - Function NC_connect(sz_connect_string As String) As Int16 + Dim nret As Int16 + Dim msga As String - Dim nret As Int16 - Dim msga As String + 'nret = SetPLCTool("XIP01, 0, 10, 0, 0, 0") + 'nret = SetPLCTool("COM") - 'nret = SetPLCTool("XIP01, 0, 10, 0, 0, 0") - 'nret = SetPLCTool("COM") + nret = SetPLCTool(sz_connect_string) - nret = SetPLCTool(sz_connect_string) + If nret <> 0 Then + msga = "Communication not available (SetPLCTool) : " & nret.ToString & " - " & sz_connect_string + EgtOutLog(msga) + End If - If nret <> 0 Then - msga = "Communication not available (SetPLCTool) : " & nret.ToString & " - " & sz_connect_string - EgtOutLog(msga) - End If + 'Richiedo porta comunicazione PC-CN 2 + ' iport2 = 1 + nret = Get_Port(n_portNC) + If nret <> 0 Then + msga = " Port not available (Get_Port) : " & nret.ToString + EgtOutLog(msga) + End If - 'Richiedo porta comunicazione PC-CN 2 - ' iport2 = 1 - nret = Get_Port(n_portNC) - If nret <> 0 Then - msga = " Port not available (Get_Port) : " & nret.ToString - EgtOutLog(msga) - End If + ' eventualmente aggiungere apertura porta per PPP + 'iport3 = 3 + 'nret = Get_Port(iport3) - ' eventualmente aggiungere apertura porta per PPP - 'iport3 = 3 - 'nret = Get_Port(iport3) + global_state = eStateType.READY - global_state = eStateType.READY + Return nret - Return nret + End Function - End Function + Function NC_disconnect() As Int16 - Function NC_disconnect() As Int16 + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If + Dim nret As Int16 = Free_Port(n_portNC) - Dim nret As Int16 = Free_Port(n_portNC) + global_state = eStateType.NOT_INIT - global_state = eStateType.NOT_INIT + Return nret - Return nret - - End Function + End Function #End Region - Function timer_read_var() As Int16 + Function timer_read_var() As Int16 - Const FIRST_GROUP As Int16 = 0, N_GROUPS As Int16 = 1 + Const FIRST_GROUP As Int16 = 0, N_GROUPS As Int16 = 1 - Dim nret As Int16 = -1 + Dim nret As Int16 = -1 - If global_state <> eStateType.READY Then - Return global_state - End If - - global_state = eStateType.BUSY - - ' Lettura variabile %V6003 - - iFirstVar = &H6003 - - nret = ReadLadderVar(n_portNC, VBY, iFirstVar, 1, MyData, SYN) - If nret = 0 Then - Var6003 = MyData.data(0) - End If - - ' Lettura variabili da %V600A.B a %V600D.B tramite un solo accesso in memoria per %V600A.B e lettura di 4 in serie - - iFirstVar = &H600A - - nret = ReadLadderVar(n_portNC, VBY, iFirstVar, 5, MyData, SYN) - - If nret = 0 Then - Var600A = MyData.data(0) ' Feed_override - Var600B = MyData.data(1) ' spindle_override - Var600C = MyData.data(2) - Var600D = MyData.data(3) ' active_mode - Var600E = MyData.data(4) - - b_feed_changed = True - d_DInterpo_Feed_override = Math.Round(Var600A * 120.0 / 255, 0) - - b_spindle_data_changed = True - n_spindle_override(0) = CShort(Math.Round((Var600B * 50 / 255) + 50)) - End If - - ' Lettura variabile assorbimento mandrino ( %V250F ) - - iFirstVar = &H250F - - nret = ReadLadderVar(n_portNC, VW, iFirstVar, 1, MyData, SYN) - - If nret = 0 Then - - d_spindle_power = CDbl(BitConverter.ToInt16(MyData.data, 0)) / 10.0 - - End If - - ' Lettura potenziometro F override...... - - ' Type tInterpo - ' FeedMes1 As Integer '; /* not used */ - ' FeedMes2 As Integer '; /* internal unit per NC sampling time */ - ' Distance As Long '; /* to go on the path */ - ' FeedProg As Long '; - ' Override As Long '; /* 16 LSB used */ - ' End Type - - nret = InterpoState(n_portNC, FIRST_GROUP, N_GROUPS, MyData, SYN) - - If nret <> 0 Then - Dim sMsg As String = "Error in InterpoState : " & nret.ToString - EgtOutLog(sMsg) - End If - - ' FeedMes1 = BitConverter.ToInt16(MyData.data, 0)) - ' FeedMes2 = BitConverter.ToInt16(MyData.data, 2)) '/* internal unit per NC sampling time */ - ' Distance = BitConverter.ToInt32(MyData.data, 4)) '/* to go on the path */ - ' FeedProg = BitConverter.ToInt32(MyData.data, 8)) - ' Override = BitConverter.ToInt32(MyData.data, 12)) /* 16 LSB used */ - - 'e non gestiamo il caso del numero negativo....... - d_DInterpo_Prog_Feed = CDbl(BitConverter.ToUInt16(MyData.data, 10)) ' sembrerebbero i due bytes più alti del long ...?? - - ' Lettura variabile S effettiva - - iFirstVar = &H2600 - - nret = ReadLadderVar(n_portNC, VL, iFirstVar, 1, MyData, SYN) - - If nret = 0 Then - d_spindle_eff(0) = CDbl(BitConverter.ToInt32(MyData.data, 0)) ' progr. Feed - End If - - ' Lettura variabili da %V6010.L a %V601C.L - - iFirstVar = &H6010 - - nret = ReadLadderVar(n_portNC, VL, iFirstVar, 4, MyData, SYN) - - If nret = 0 Then - var6010L = BitConverter.ToInt32(MyData.data, 0) ' progr. Feed - var6014L = BitConverter.ToInt32(MyData.data, 4) ' progr. speed - var6018L = BitConverter.ToInt32(MyData.data, 8) - var601CL = BitConverter.ToInt32(MyData.data, 12) - End If - - ' d_DInterpo_Prog_Feed = CDbl(var6010L) ' sembrerebbe non buona letta da qua ... - d_spindle_prog(0) = CDbl(var6014L) - - - ' Lettura variabili da %V6020.L a %V602C.L - - iFirstVar = &H6020 - - nret = ReadLadderVar(n_portNC, VL, iFirstVar, 4, MyData, SYN) - - If nret = 0 Then - var6020L = BitConverter.ToInt32(MyData.data, 0) - var6024L = BitConverter.ToInt32(MyData.data, 4) ' potenziometro tablet - var6028L = BitConverter.ToInt32(MyData.data, 8) ' disponibile - var602CL = BitConverter.ToInt32(MyData.data, 12) ' disponibile - End If - - global_state = eStateType.READY - Return nret - - End Function - - Private Function set_NC_mode(n_mode As Integer) As Int16 - - Dim nret As Int16 = -1 - - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If - - global_state = eStateType.BUSY - - nret = WriteObjectVarious2(n_portNC, Mode, n_mode) ' 0 AUTO 1 SEQUENZIALE 2 IMD 7 MANUALE - - If nret <> 0 Then - EgtOutLog("Error in set_NC_mode(" & n_mode.ToString() & ") : " & nret.ToString()) - End If - - global_state = eStateType.READY - - Return nret - - End Function - - Private Function set_MDI_command(sz_command As String) As Int16 - - Dim nret As Int16 = -1 - Dim nret2 As Int16 = -1 - Dim nret3 As Int16 = -1 - - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If - - global_state = eStateType.BUSY - - nret = WriteObjectVarious2(n_portNC, Mode, MDI_M) ' SELEZIONE MODO IMD - If nret = 0 Then - - nret2 = DNC20_DialWithModes_2(n_portNC, sz_command, SYN) ' PASSO IL COMANDO SU UN BUFFER - If nret2 = 0 Then - - nret3 = StartNC(n_portNC) ' DO IL CICLO - If nret3 = 0 Then - - nret = WriteObjectVarious2(n_portNC, Mode, MANUAL_M) ' ' SELEZIONO MODO MANUALE - - Else - nret = nret3 + 3000S ' codice di errore della StartNC - End If ' If nret3 = 0 - Else - nret = nret2 + 2000S ' codice di errore della DNC20_DialWithModes_2 - End If ' If nret2 = 0 - End If ' If nret = 0 - - global_state = eStateType.READY - Return nret - - End Function - - Function Read_machine_pos() As Int16 - - Dim nret As Int16 - Dim i, n As Integer - - If global_state <> eStateType.READY Then - Return global_state - End If - - global_state = eStateType.BUSY - - If b_OM_pos Then ' quote rispetto a 0 Macchina - nret = DNC20_Position_OM(n_portNC, 0, MyData, SYN) - - Else ' quote rispetto a 0 Pezzo - nret = DNC20_Position_OP(n_portNC, 0, MyData, SYN) - End If - - If nret <> 0 Then - Dim sMsg As String = "Error in DNC20_Position_O* : " & nret.ToString - EgtOutLog(sMsg) - End If - - For i = 0 To Math.Min(8, MAX_AXES) ' si possono leggere come massimo 9 assi 0 -> 8 - - n = i * 16 - - b_axis_valid(i) = (MyData.data(n) <> 0) - - If b_axis_valid(i) Then - If i < A_AXIS Then - d_axis_position(i) = (BitConverter.ToInt32(MyData.data, n + 4)) / Vmminch ' X - d_axis_delta(i) = (BitConverter.ToInt32(MyData.data, n + 8)) / Vmminch - Else - d_axis_position(i) = (BitConverter.ToInt32(MyData.data, n + 4)) / NumCifreDecP4 ' A - d_axis_delta(i) = (BitConverter.ToInt32(MyData.data, n + 8)) / NumCifreDecP4 - End If + If global_state <> eStateType.READY Then + Return global_state End If - Next - If b_axis_events_enable Then - MyBase.OnNewPositionData() - MyBase.OnNewPosDeltaData() - End If + global_state = eStateType.BUSY - global_state = eStateType.READY - Return nret + ' Lettura variabile %V6003 - End Function + iFirstVar = &H6003 -#Region " Metodi pubblici" + nret = ReadLadderVar(n_portNC, VBY, iFirstVar, 1, MyData, SYN) + If nret = 0 Then + Var6003 = MyData.data(0) + End If - Public Overrides Function Stop_Connection() As Boolean + ' Lettura variabili da %V600A.B a %V600D.B tramite un solo accesso in memoria per %V600A.B e lettura di 4 in serie - If VerifyBusy() <> eStateType.READY Then - Dim sMsg As String = "Error in NC_disconnect : always busy" - EgtOutLog(sMsg) - End If + iFirstVar = &H600A - aTimer.Stop() + nret = ReadLadderVar(n_portNC, VBY, iFirstVar, 5, MyData, SYN) - Dim nRet As Short = NC_disconnect() + If nret = 0 Then + Var600A = MyData.data(0) ' Feed_override + Var600B = MyData.data(1) ' spindle_override + Var600C = MyData.data(2) + Var600D = MyData.data(3) ' active_mode + Var600E = MyData.data(4) - If nRet <> 0 Then - Dim sMsg As String = "Error in NC_disconnect : " & nRet.ToString - EgtOutLog(sMsg) - End If + b_feed_changed = True + d_DInterpo_Feed_override = Math.Round(Var600A * 120.0 / 255, 0) - Return True - End Function + b_spindle_data_changed = True + n_spindle_override(0) = CShort(Math.Round((Var600B * 50 / 255) + 50)) + End If - Public Overrides Sub ReadEls_Add_Parameter(sz_var As String, n_par As Short) + ' Lettura variabile assorbimento mandrino ( %V250F ) - ' Verifico accessibilità CN - If VerifyBusy() <> eStateType.READY Then - n_DReadELS_handle = 0 - Return - End If - ' Dichiaro CN occupato - global_state = eStateType.BUSY + iFirstVar = &H250F - Dim Ask As tDNC20_ObjectList + nret = ReadLadderVar(n_portNC, VW, iFirstVar, 1, MyData, SYN) - Select Case sz_var(0) - Case "E"(0) - Ask.ElemNum = CUShort(sz_var.Substring(1, 1)) - Ask.First = CUShort(sz_var.Remove(0, 2)) - Ask.Quantity = 1 - Dim nVal As Int32 - Dim nRet As Int16 = DNC20_ReadExxxxx(n_portNC, 0, 1, Ask, nVal, SYN) - If nRet = 0 Then - d_DReadELS_value = CDbl(nVal) - n_DReadELS_handle = n_par - End If - Case "L"(0) - Ask.ElemNum = 0 - Ask.First = CUShort(sz_var.Remove(0, 1)) - Ask.Quantity = 1 - Dim fVal As Single - Dim nRet As Int16 = DNC20_ReadLxxx(n_portNC, 0, 1, Ask, fVal, SYN) - If nRet = 0 Then - d_DReadELS_value = CDbl(fVal) - n_DReadELS_handle = n_par - End If - Case Else - n_DReadELS_handle = 0 - End Select + If nret = 0 Then - ' Dichiaro CN libero - global_state = eStateType.READY + d_spindle_power = CDbl(BitConverter.ToInt16(MyData.data, 0)) / 10.0 - End Sub - ' - ' call DVariables_WriteVariables("E80000;E80001" , "1;2" ) - ' - Public Overrides Sub DVariables_WriteVariables(sz_parameters As String, sz_values As String) + End If + ' Lettura potenziometro F override...... - End Sub + ' Type tInterpo + ' FeedMes1 As Integer '; /* not used */ + ' FeedMes2 As Integer '; /* internal unit per NC sampling time */ + ' Distance As Long '; /* to go on the path */ + ' FeedProg As Long '; + ' Override As Long '; /* 16 LSB used */ + ' End Type - Public Overrides Sub DVariables_WriteVariables2(sz_parameters As String, sz_values As String) + nret = InterpoState(n_portNC, FIRST_GROUP, N_GROUPS, MyData, SYN) + If nret <> 0 Then + Dim sMsg As String = "Error in InterpoState : " & nret.ToString + EgtOutLog(sMsg) + End If - End Sub + ' FeedMes1 = BitConverter.ToInt16(MyData.data, 0)) + ' FeedMes2 = BitConverter.ToInt16(MyData.data, 2)) '/* internal unit per NC sampling time */ + ' Distance = BitConverter.ToInt32(MyData.data, 4)) '/* to go on the path */ + ' FeedProg = BitConverter.ToInt32(MyData.data, 8)) + ' Override = BitConverter.ToInt32(MyData.data, 12)) /* 16 LSB used */ - Public Overrides Function MDI_command() As Short + 'e non gestiamo il caso del numero negativo....... + d_DInterpo_Prog_Feed = CDbl(BitConverter.ToUInt16(MyData.data, 10)) ' sembrerebbero i due bytes più alti del long ...?? - Dim nret As Int16 = set_MDI_command(sz_ManualDataInput) + ' Lettura variabile S effettiva - If nret <> 0 Then - EgtOutLog("Error Execute MDI Command :" & nret.ToString()) - End If + iFirstVar = &H2600 - Return nret + nret = ReadLadderVar(n_portNC, VL, iFirstVar, 1, MyData, SYN) - End Function - ' - ' Activate program - ' returns 0 = OK ; > 0 error - ' - Public Overrides Function ActivateProgram(n_part_program As Integer) As Int16 + If nret = 0 Then + d_spindle_eff(0) = CDbl(BitConverter.ToInt32(MyData.data, 0)) ' progr. Feed + End If - Dim nret As Int16 = -1 + ' Lettura variabili da %V6010.L a %V601C.L - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If + iFirstVar = &H6010 - global_state = eStateType.BUSY + nret = ReadLadderVar(n_portNC, VL, iFirstVar, 4, MyData, SYN) - nret = WriteObjectVarious2(n_portNC, Mode, AUTO_M) ' potrebbe essere anche modo 11 ! + If nret = 0 Then + var6010L = BitConverter.ToInt32(MyData.data, 0) ' progr. Feed + var6014L = BitConverter.ToInt32(MyData.data, 4) ' progr. speed + var6018L = BitConverter.ToInt32(MyData.data, 8) + var601CL = BitConverter.ToInt32(MyData.data, 12) + End If - If nret = 0 Then - nret = WriteObjectVarious2(n_portNC, ACTIVE_PROG, n_part_program) ' notare : numero non moltiplicato * 10 - End If + ' d_DInterpo_Prog_Feed = CDbl(var6010L) ' sembrerebbe non buona letta da qua ... + d_spindle_prog(0) = CDbl(var6014L) - If nret <> 0 Then - EgtOutLog("Error in Activate program : " & nret.ToString()) - End If - global_state = eStateType.READY - Return nret + ' Lettura variabili da %V6020.L a %V602C.L - End Function - ' - ' Set DPOsition Mode OP/OM - ' - Public Overrides Sub set_OP_OM(n As Short) - b_OM_pos = ( n = 0) - End Sub - ' - ' Read Once position - ' returns 0 = OK ; > 0 error - ' - Public Overrides Function DPOsition_ReadOnce() As Int16 + iFirstVar = &H6020 - Return 1 - End Function - ' - ' Set Cycle Start - ' - Public Overrides Function DGeneralFunctions_CycleStart() As Int16 + nret = ReadLadderVar(n_portNC, VL, iFirstVar, 4, MyData, SYN) - Dim nret As Int16 = -1 + If nret = 0 Then + var6020L = BitConverter.ToInt32(MyData.data, 0) + var6024L = BitConverter.ToInt32(MyData.data, 4) ' potenziometro tablet + var6028L = BitConverter.ToInt32(MyData.data, 8) ' disponibile + var602CL = BitConverter.ToInt32(MyData.data, 12) ' disponibile + End If - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If - global_state = eStateType.BUSY + global_state = eStateType.READY + Return nret - nret = StartNC(n_portNC) + End Function - global_state = eStateType.READY + Private Function set_NC_mode(n_mode As Integer) As Int16 - Return nret + Dim nret As Int16 = -1 - End Function - ' - ' Set Feed Hold - ' - Public Overrides Function DGeneralFunctions_FeedHold() As Int16 + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If - Dim nret As Int16 = -1 + global_state = eStateType.BUSY - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If + nret = WriteObjectVarious2(n_portNC, Mode, n_mode) ' 0 AUTO 1 SEQUENZIALE 2 IMD 7 MANUALE - global_state = eStateType.BUSY + If nret <> 0 Then + EgtOutLog("Error in set_NC_mode(" & n_mode.ToString() & ") : " & nret.ToString()) + End If - nret = StopNC(n_portNC) + global_state = eStateType.READY - global_state = eStateType.READY - Return nret + Return nret - End Function - ' - ' Set NC Reset - ' - Public Overrides Function DGeneralFunctions_Reset() As Int16 - Dim nret As Int16 = -1 + End Function - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If + Private Function set_MDI_command(sz_command As String) As Int16 - global_state = eStateType.BUSY + Dim nret As Int16 = -1 + Dim nret2 As Int16 = -1 + Dim nret3 As Int16 = -1 - nret = InitNC(n_portNC) + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If - global_state = eStateType.READY - Return nret + global_state = eStateType.BUSY - End Function + nret = WriteObjectVarious2(n_portNC, Mode, MDI_M) ' SELEZIONE MODO IMD + If nret = 0 Then - Public Overrides Function DGeneralFunctions_WriteCncMode(nmode As Short) As Int16 + nret2 = DNC20_DialWithModes_2(n_portNC, sz_command, SYN) ' PASSO IL COMANDO SU UN BUFFER + If nret2 = 0 Then - ' Non controllare global_state, lo fa già la set_NC_mode + nret3 = StartNC(n_portNC) ' DO IL CICLO + If nret3 = 0 Then - Return set_NC_mode(nmode) + nret = WriteObjectVarious2(n_portNC, Mode, MANUAL_M) ' ' SELEZIONO MODO MANUALE - End Function + Else + nret = nret3 + 3000S ' codice di errore della StartNC + End If ' If nret3 = 0 + Else + nret = nret2 + 2000S ' codice di errore della DNC20_DialWithModes_2 + End If ' If nret2 = 0 + End If ' If nret = 0 - Public Overrides Function Write_var_1(sz_par As String, lvalue As Long) As Int16 + global_state = eStateType.READY + Return nret - Dim nret As Int16 = -1 - Dim n_what As UInt16 + End Function - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If + Function Read_machine_pos() As Int16 - global_state = eStateType.BUSY + Dim nret As Int16 + Dim i, n As Integer - If sz_par = "E80000" Then ' !!! da sistemare per beccare anche gli altri indirizzi - n_what = E80000 - MyData.data = BitConverter.GetBytes(lvalue) + If global_state <> eStateType.READY Then + Return global_state + End If - nret = WriteObjectVarious4(n_portNC, n_what, 0, 1, MyData) - Else - nret = -997 - End If + global_state = eStateType.BUSY - global_state = eStateType.READY - Return nret + If b_OM_pos Then ' quote rispetto a 0 Macchina + nret = DNC20_Position_OM(n_portNC, 0, MyData, SYN) - End Function - ' - ' da PC a CN - ' - Public Overrides Function Download_NC_prog(sz_PC_filename As String, sz_NC_filename As String) As Int16 + Else ' quote rispetto a 0 Pezzo + nret = DNC20_Position_OP(n_portNC, 0, MyData, SYN) + End If - ' Verifico accessibilità CN - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If - ' Dichiaro CN occupato - global_state = eStateType.BUSY + If nret <> 0 Then + Dim sMsg As String = "Error in DNC20_Position_O* : " & nret.ToString + EgtOutLog(sMsg) + End If - ' Determino numero programma ( %900.0 -> 9000) - Dim l As Int32 = get_Num_prog_number(sz_NC_filename) - ' Trasmetto il programma - Dim nRet As Int16 = DownLoadFile(n_portNC, l, sz_PC_filename) + For i = 0 To Math.Min(8, MAX_AXES) ' si possono leggere come massimo 9 assi 0 -> 8 - If nRet <> 0 Then - EgtOutLog("Error in Download_NC_prog : " & nRet.ToString()) - End If + n = i * 16 - ' Dichiaro CN libero - global_state = eStateType.READY + b_axis_valid(i) = (MyData.data(n) <> 0) - Return nRet - - End Function - ' - ' da CN a pc - ' - Public Overrides Function Upload_NC_prog(sz_PC_filename As String, sz_NC_filename As String) As Int16 - - Dim nret As Int16 = -1 - - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If - - global_state = eStateType.BUSY - - Dim l As Int32 = get_Num_prog_number(sz_NC_filename) - - nret = UpLoadFile(n_portNC, l, sz_PC_filename) - - global_state = eStateType.READY - Return nret - - End Function - - Private Function get_Num_prog_number(sz_in As String) As Int32 - - Dim sz_n As String = sz_in - Dim n_prog_num As Int32 - - If sz_n.Contains("%") Then sz_n = sz_n.Replace("%", "") - - If sz_n.Contains(".") Then - sz_n = sz_n.Replace(".", "") - n_prog_num = CInt(sz_n) - Else - n_prog_num = 10 * CInt(sz_n) - End If - - Return n_prog_num - - End Function - - Public Overrides Function Delete_NC_prog(sz_NC_filename As String) As Int16 - - Dim nret As Int16 = -1 - - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If - - global_state = eStateType.BUSY - - Dim l As Int32 = get_Num_prog_number(sz_NC_filename) - - nret = DncDeleteFile(n_portNC, l) - - If nret <> 0 Then - EgtOutLog("Error in Delete_NC_prog : " & nret.ToString()) - End If - - global_state = eStateType.READY - Return nret - - End Function - - Public Overrides Function DTool_set_read_range(n_start As Short, n_stop As Short) As Int16 - - Dim nret As Int16 = -1 - - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If - - global_state = eStateType.BUSY - - nret = ReadTool(n_portNC, n_start, 1, MyData, SYN) - - If nret = RET_OK Then - - Tools(0).lenght = 0 ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - Tools(0).radius = 0 - Tools(0).comp_num = 0 - - b_Tool_data_changed = True - - End If - - global_state = eStateType.READY - Return nret - - End Function - - Public Overrides Function DTool_write_tool(n_tool_number As Short, Length_X As Double, Radius As Double, TipRadius_Z As Double, - DLength_X As Double, DTipRadius_Z As Double, - nDirection As Short, nType As Short, nH As Integer) As Int16 - Dim Tool As tCorOutil - - Dim nret As Int16 = -1 - - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If - - global_state = eStateType.BUSY - - ' da rifare !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - - Tool.L_X = CLng(Length_X * 1000) - Tool.R_Radus = CLng(Radius * 1000) - Tool.RTore_Z = CLng(TipRadius_Z * 1000) - Tool.DL_DX = 0 - Tool.DR_DZ = 0 - Tool.Direc = 0 - Tool.Type = 0 - - nret = WriteTool(n_portNC, n_tool_number, 1, Tool) - - global_state = eStateType.READY - Return nret - - End Function - - Public Overrides Function get_free_memory(n_zone As Int16) As Long - ' - ' - ' usare zona 0 - ' - Dim nret As Int16 = -1 - - Dim l_free_mem As Long - - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If - - global_state = eStateType.BUSY - - nret = ReadMemoryFree(n_portNC, l_free_mem, n_zone) - - global_state = eStateType.READY - - If nret = 0 Then - Return l_free_mem - Else - Return -9999 - End If - - End Function - - Public Overrides Function read_active_mode() As Int16 - - Return (Var600D) ' es 0 = auto, ecc. - - End Function - - Public Overrides Function Is_G24_active() As Boolean - Return (( Var6003 And &H2) <> 0) - End Function - - Public Overrides Function get_program_status() As Int16 - - Dim nret As Int16 = -1 - - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If - - global_state = eStateType.BUSY - - nret = ReadProgStatus(n_portNC, 0, 1, MyData, SYN) - - If nret = 0 Then - - l_GFunction = BitConverter.ToInt32(MyData.data, 0) - l_active_program = BitConverter.ToInt32(MyData.data, 4) - - n_BlocExe = CUShort(BitConverter.ToInt16(MyData.data, 8)) - n_ProgError = BitConverter.ToInt16(MyData.data, 10) - n_BlocError = CUShort(BitConverter.ToInt16(MyData.data, 12)) - n_Tool = CUShort(BitConverter.ToInt16(MyData.data, 14)) - n_Direction = CUShort(BitConverter.ToInt16(MyData.data, 16)) - n_Corrector = CUShort(BitConverter.ToInt16(MyData.data, 18)) - n_LeftFunct = CUShort(BitConverter.ToInt16(MyData.data, 20)) - End If - - global_state = eStateType.READY - - Return nret - End Function - - Public Overrides Function get_plc_messages() As Int16 - - If VerifyBusy() <> eStateType.READY Then - Return global_state - End If - Return get_errors() - - End Function - - Private Function get_errors() As Int16 - - Const GROUP_ACTIVE As UInt16 = &HFFFF ' 0xFFFF=Groupe Actif - - Const MORE_MESSAGES As UInt16 = 6 - Const NO_MORE_MESSAGES As UInt16 = 15 - - Dim nret As Int16 = -1, i As Short = 0 - - Dim nErrorLineNum As Int16 = 0 - - Dim b_int_NC_error As Boolean = False - - Dim szt As String = "" - - If global_state <> eStateType.READY Then - Return global_state - End If - - global_state = eStateType.BUSY - - ' PLC errors - b_PLC_error = False - SyncLock Me - sz_PLC_error_messages = "PLC Errors : " - End SyncLock - iFirstVar = &H7000 ' messaggio 0 " MACCHINA OK " - - nret = ReadLadderVar(n_portNC, VBY, iFirstVar, 100, MyData, SYN) ' max 120 bytes.... - If nret = 0 Then ' 253 = troppi dati - - For i = 0 To 100 - 1 - If MyData.data(i) <> 0 Then - b_PLC_error = True - SyncLock Me - sz_PLC_error_messages = sz_PLC_error_messages & " " & i.ToString - End SyncLock + If b_axis_valid(i) Then + If i < A_AXIS Then + d_axis_position(i) = (BitConverter.ToInt32(MyData.data, n + 4)) / Vmminch ' X + d_axis_delta(i) = (BitConverter.ToInt32(MyData.data, n + 8)) / Vmminch + Else + d_axis_position(i) = (BitConverter.ToInt32(MyData.data, n + 4)) / NumCifreDecP4 ' A + d_axis_delta(i) = (BitConverter.ToInt32(MyData.data, n + 8)) / NumCifreDecP4 + End If End If Next - iFirstVar = &H7000 + 100 ' messaggio 100 + If b_axis_events_enable Then + MyBase.OnNewPositionData() + MyBase.OnNewPosDeltaData() + End If - nret = ReadLadderVar(n_portNC, VBY, iFirstVar, 50, MyData2, SYN) + global_state = eStateType.READY + Return nret + + End Function + +#Region " Metodi pubblici" + + Public Overrides Function Stop_Connection() As Boolean + + If VerifyBusy() <> eStateType.READY Then + Dim sMsg As String = "Error in NC_disconnect : always busy" + EgtOutLog(sMsg) + End If + + aTimer.Stop() + + Dim nRet As Short = NC_disconnect() + + If nRet <> 0 Then + Dim sMsg As String = "Error in NC_disconnect : " & nRet.ToString + EgtOutLog(sMsg) + End If + + Return True + End Function + + Public Overrides Sub ReadEls_Add_Parameter(sz_var As String, n_par As Short) + + ' Verifico accessibilità CN + If VerifyBusy() <> eStateType.READY Then + n_DReadELS_handle = 0 + Return + End If + ' Dichiaro CN occupato + global_state = eStateType.BUSY + + Dim Ask As tDNC20_ObjectList + + Select Case sz_var(0) + Case "E"(0) + Ask.ElemNum = CUShort(sz_var.Substring(1, 1)) + Ask.First = CUShort(sz_var.Remove(0, 2)) + Ask.Quantity = 1 + Dim nVal As Int32 + Dim nRet As Int16 = DNC20_ReadExxxxx(n_portNC, 0, 1, Ask, nVal, SYN) + If nRet = 0 Then + d_DReadELS_value = CDbl(nVal) + n_DReadELS_handle = n_par + End If + Case "L"(0) + Ask.ElemNum = 0 + Ask.First = CUShort(sz_var.Remove(0, 1)) + Ask.Quantity = 1 + Dim fVal As Single + Dim nRet As Int16 = DNC20_ReadLxxx(n_portNC, 0, 1, Ask, fVal, SYN) + If nRet = 0 Then + d_DReadELS_value = CDbl(fVal) + n_DReadELS_handle = n_par + End If + Case Else + n_DReadELS_handle = 0 + End Select + + ' Dichiaro CN libero + global_state = eStateType.READY + + End Sub + ' + ' call DVariables_WriteVariables("E80000;E80001" , "1;2" ) + ' + Public Overrides Sub DVariables_WriteVariables(sz_parameters As String, sz_values As String) + + + End Sub + + Public Overrides Sub DVariables_WriteVariables2(sz_parameters As String, sz_values As String) + + + End Sub + + Public Overrides Function MDI_command() As Short + + Dim nret As Int16 = set_MDI_command(sz_ManualDataInput) + + If nret <> 0 Then + EgtOutLog("Error Execute MDI Command :" & nret.ToString()) + End If + + Return nret + + End Function + ' + ' Activate program + ' returns 0 = OK ; > 0 error + ' + Public Overrides Function ActivateProgram(n_part_program As Integer) As Int16 + + Dim nret As Int16 = -1 + + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If + + global_state = eStateType.BUSY + + nret = WriteObjectVarious2(n_portNC, Mode, AUTO_M) ' potrebbe essere anche modo 11 ! + + If nret = 0 Then + nret = WriteObjectVarious2(n_portNC, ACTIVE_PROG, n_part_program) ' notare : numero non moltiplicato * 10 + End If + + If nret <> 0 Then + EgtOutLog("Error in Activate program : " & nret.ToString()) + End If + + global_state = eStateType.READY + Return nret + + End Function + ' + ' Set DPOsition Mode OP/OM + ' + Public Overrides Sub set_OP_OM(n As Short) + b_OM_pos = (n = 0) + End Sub + ' + ' Read Once position + ' returns 0 = OK ; > 0 error + ' + Public Overrides Function DPOsition_ReadOnce() As Int16 + + Return 1 + End Function + ' + ' Set Cycle Start + ' + Public Overrides Function DGeneralFunctions_CycleStart() As Int16 + + Dim nret As Int16 = -1 + + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If + global_state = eStateType.BUSY + + nret = StartNC(n_portNC) + + global_state = eStateType.READY + + Return nret + + End Function + ' + ' Set Feed Hold + ' + Public Overrides Function DGeneralFunctions_FeedHold() As Int16 + + Dim nret As Int16 = -1 + + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If + + global_state = eStateType.BUSY + + nret = StopNC(n_portNC) + + global_state = eStateType.READY + Return nret + + End Function + ' + ' Set NC Reset + ' + Public Overrides Function DGeneralFunctions_Reset() As Int16 + Dim nret As Int16 = -1 + + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If + + global_state = eStateType.BUSY + + nret = InitNC(n_portNC) + + global_state = eStateType.READY + Return nret + + End Function + + Public Overrides Function DGeneralFunctions_WriteCncMode(nmode As Short) As Int16 + + ' Non controllare global_state, lo fa già la set_NC_mode + + Return set_NC_mode(nmode) + + End Function + + Public Overrides Function Write_var_1(sz_par As String, lvalue As Long) As Int16 + + Dim nret As Int16 = -1 + Dim n_what As UInt16 + + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If + + global_state = eStateType.BUSY + + If sz_par = "E80000" Then ' !!! da sistemare per beccare anche gli altri indirizzi + n_what = E80000 + MyData.data = BitConverter.GetBytes(lvalue) + + nret = WriteObjectVarious4(n_portNC, n_what, 0, 1, MyData) + Else + nret = -997 + End If + + global_state = eStateType.READY + Return nret + + End Function + + ' + ' da PC a CN (sincrono) + ' + Public Overrides Function Download_NC_prog(sz_PC_filename As String, sz_NC_filename As String) As Int16 + + ' Verifico accessibilità CN + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If + ' Dichiaro CN occupato + global_state = eStateType.BUSY + + ' Determino numero programma ( %900.0 -> 9000) + Dim l As Int32 = get_Num_prog_number(sz_NC_filename) + + ' verifico la dimensione del file + Dim myFile As New FileInfo(sz_PC_filename) + Dim sizeInBytes As Long = myFile.Length + + ' Verifico se dimensione file è maggiore del Buffer + If sizeInBytes > 32000 Then + ' Trasmetto il programma (asincrono) + Dim thread As New Thread(Sub() + Download_Drip_CN_prog(sz_PC_filename, sz_NC_filename) + End Sub) + thread.Start() + Return 1 + Else + ' Trasmetto il programma (interamente) + Dim nRet As Int16 = DownLoadFile(n_portNC, l, sz_PC_filename) + If nRet <> 0 Then + EgtOutLog("Error in Download_NC_prog : " & nRet.ToString()) + End If + ' Dichiaro CN libero + global_state = eStateType.READY + Return nRet + End If + + + + End Function + ' + ' da PC a CN (asincrono) + ' + + Private Function Download_Drip_CN_prog(sz_PC_filename As String, sz_NC_filename As String) As Int16 + bIsDripFeed = True + ' Verifico accessibilità CN + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If + ' Dichiaro CN occupato + global_state = eStateType.BUSY + + ' Determino numero programma ( %900.0 -> 9000) + Dim l As Int32 = get_Num_prog_number(sz_NC_filename) + ' Ultimo numero di segmento invaito + Dim i As UInt16 + + ' Invia il primo segmento + Dim nRet As Int16 = DownLoadPPPFileHSL(n_portNC, l, 32768, sz_PC_filename, DNC_MODE_FIRST, i) + If nRet <> DNC_IN_PROGRES And nRet <> DNC_RING_BUFFER_FULL Then + nRet = DownLoadPPPFileHSL(n_portNC, l, 32768, sz_PC_filename, DNC_MODE_ABORT, i) + EgtOutLog("Error in Download_NC_prog/DownLoadPPPFileHSL : " & nRet.ToString()) + End If + + ' Invio i segmenti seguenti + While nRet = DNC_IN_PROGRES Or nRet = DNC_RING_BUFFER_FULL + nRet = DownLoadPPPFileHSL(n_portNC, l, 32768, sz_PC_filename, DNC_MODE_CONTINUE, i) + End While + + If nRet <> 0 Then + EgtOutLog("Error in Download_NC_prog : " & nRet.ToString()) + End If + + ' Dichiaro CN libero + global_state = eStateType.READY + bIsDripFeed = False + ' chiudo il thread aperto + Return nRet + + End Function + + ' + ' da CN a pc + ' + Public Overrides Function Upload_NC_prog(sz_PC_filename As String, sz_NC_filename As String) As Int16 + + Dim nret As Int16 = -1 + + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If + + global_state = eStateType.BUSY + + Dim l As Int32 = get_Num_prog_number(sz_NC_filename) + + nret = UpLoadFile(n_portNC, l, sz_PC_filename) + + global_state = eStateType.READY + Return nret + + End Function + + Private Function get_Num_prog_number(sz_in As String) As Int32 + + Dim sz_n As String = sz_in + Dim n_prog_num As Int32 + + If sz_n.Contains("%") Then sz_n = sz_n.Replace("%", "") + + If sz_n.Contains(".") Then + sz_n = sz_n.Replace(".", "") + n_prog_num = CInt(sz_n) + Else + n_prog_num = 10 * CInt(sz_n) + End If + + Return n_prog_num + + End Function + + Public Overrides Function Delete_NC_prog(sz_NC_filename As String) As Int16 + + Dim nret As Int16 = -1 + + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If + + global_state = eStateType.BUSY + + Dim l As Int32 = get_Num_prog_number(sz_NC_filename) + + nret = DncDeleteFile(n_portNC, l) + + If nret <> 0 Then + EgtOutLog("Error in Delete_NC_prog : " & nret.ToString()) + End If + + global_state = eStateType.READY + Return nret + + End Function + + Public Overrides Function DTool_set_read_range(n_start As Short, n_stop As Short) As Int16 + + Dim nret As Int16 = -1 + + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If + + global_state = eStateType.BUSY + + nret = ReadTool(n_portNC, n_start, 1, MyData, SYN) + + If nret = RET_OK Then + + Tools(0).lenght = 0 ' !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + Tools(0).radius = 0 + Tools(0).comp_num = 0 + + b_Tool_data_changed = True + + End If + + global_state = eStateType.READY + Return nret + + End Function + + Public Overrides Function DTool_write_tool(n_tool_number As Short, Length_X As Double, Radius As Double, TipRadius_Z As Double, + DLength_X As Double, DTipRadius_Z As Double, + nDirection As Short, nType As Short, nH As Integer) As Int16 + Dim Tool As tCorOutil + + Dim nret As Int16 = -1 + + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If + + global_state = eStateType.BUSY + + ' da rifare !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + + Tool.L_X = CLng(Length_X * 1000) + Tool.R_Radus = CLng(Radius * 1000) + Tool.RTore_Z = CLng(TipRadius_Z * 1000) + Tool.DL_DX = 0 + Tool.DR_DZ = 0 + Tool.Direc = 0 + Tool.Type = 0 + + nret = WriteTool(n_portNC, n_tool_number, 1, Tool) + + global_state = eStateType.READY + Return nret + + End Function + + Public Overrides Function get_free_memory(n_zone As Int16) As Long + ' + ' + ' usare zona 0 + ' + Dim nret As Int16 = -1 + + Dim l_free_mem As Long + + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If + + global_state = eStateType.BUSY + + nret = ReadMemoryFree(n_portNC, l_free_mem, n_zone) + + global_state = eStateType.READY + + If nret = 0 Then + Return l_free_mem + Else + Return -9999 + End If + + End Function + + Public Overrides Function read_active_mode() As Int16 + + Return (Var600D) ' es 0 = auto, ecc. + + End Function + + Public Overrides Function Is_G24_active() As Boolean + Return ((Var6003 And &H2) <> 0) + End Function + + Public Overrides Function get_program_status() As Int16 + + Dim nret As Int16 = -1 + + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If + + global_state = eStateType.BUSY + + nret = ReadProgStatus(n_portNC, 0, 1, MyData, SYN) + + If nret = 0 Then + + l_GFunction = BitConverter.ToInt32(MyData.data, 0) + l_active_program = BitConverter.ToInt32(MyData.data, 4) + + n_BlocExe = CUShort(BitConverter.ToInt16(MyData.data, 8)) + n_ProgError = BitConverter.ToInt16(MyData.data, 10) + n_BlocError = CUShort(BitConverter.ToInt16(MyData.data, 12)) + n_Tool = CUShort(BitConverter.ToInt16(MyData.data, 14)) + n_Direction = CUShort(BitConverter.ToInt16(MyData.data, 16)) + n_Corrector = CUShort(BitConverter.ToInt16(MyData.data, 18)) + n_LeftFunct = CUShort(BitConverter.ToInt16(MyData.data, 20)) + End If + + global_state = eStateType.READY + + Return nret + End Function + + Public Overrides Function get_plc_messages() As Int16 + + If VerifyBusy() <> eStateType.READY Then + Return global_state + End If + Return get_errors() + + End Function + + Private Function get_errors() As Int16 + + Const GROUP_ACTIVE As UInt16 = &HFFFF ' 0xFFFF=Groupe Actif + + Const MORE_MESSAGES As UInt16 = 6 + Const NO_MORE_MESSAGES As UInt16 = 15 + + Dim nret As Int16 = -1, i As Short = 0 + + Dim nErrorLineNum As Int16 = 0 + + Dim b_int_NC_error As Boolean = False + + Dim szt As String = "" + + If global_state <> eStateType.READY Then + Return global_state + End If + + global_state = eStateType.BUSY + + ' PLC errors + b_PLC_error = False + SyncLock Me + sz_PLC_error_messages = "PLC Errors : " + End SyncLock + iFirstVar = &H7000 ' messaggio 0 " MACCHINA OK " + + nret = ReadLadderVar(n_portNC, VBY, iFirstVar, 100, MyData, SYN) ' max 120 bytes.... If nret = 0 Then ' 253 = troppi dati - For i = 0 To 50 - 1 - If MyData2.data(i) <> 0 Then + For i = 0 To 100 - 1 + If MyData.data(i) <> 0 Then b_PLC_error = True SyncLock Me - sz_PLC_error_messages = sz_PLC_error_messages & " " & (i + 100).ToString + sz_PLC_error_messages = sz_PLC_error_messages & " " & i.ToString End SyncLock End If Next + iFirstVar = &H7000 + 100 ' messaggio 100 + + nret = ReadLadderVar(n_portNC, VBY, iFirstVar, 50, MyData2, SYN) + If nret = 0 Then ' 253 = troppi dati + + For i = 0 To 50 - 1 + If MyData2.data(i) <> 0 Then + b_PLC_error = True + SyncLock Me + sz_PLC_error_messages = sz_PLC_error_messages & " " & (i + 100).ToString + End SyncLock + End If + Next + + End If End If - End If - '-------------------------------------------------------------------------- - ' Visualizzazione errore CN + '-------------------------------------------------------------------------- + ' Visualizzazione errore CN - b_NC_error = False - SyncLock Me - sz_NC_error_messages.Clear() - End SyncLock - b_int_NC_error = False + b_NC_error = False + SyncLock Me + sz_NC_error_messages.Clear() + End SyncLock + b_int_NC_error = False - iFirstVar = &H3 - nret = ReadLadderVar(n_portNC, R6, iFirstVar, 1, MyData, SYN) + iFirstVar = &H3 + nret = ReadLadderVar(n_portNC, R6, iFirstVar, 1, MyData, SYN) - If MyData.data(0) <> 0 Then - b_int_NC_error = True - End If + If MyData.data(0) <> 0 Then + b_int_NC_error = True + End If - If b_int_NC_error Then + If b_int_NC_error Then - nErrorLineNum = 0 + nErrorLineNum = 0 - n_ReadStatus = MORE_MESSAGES + n_ReadStatus = MORE_MESSAGES - While (n_ReadStatus = MORE_MESSAGES) + While (n_ReadStatus = MORE_MESSAGES) - nret = DNC40_ReadCncError(n_portNC, GROUP_ACTIVE, nErrorLineNum, MyData, SYN) + nret = DNC40_ReadCncError(n_portNC, GROUP_ACTIVE, nErrorLineNum, MyData, SYN) - If nret = 0 Then + If nret = 0 Then - ' ErrorNumber As Integer 'Numéro d'erreur - ' ErrorBlockNum As Integer 'Numéro de bloc en erreur - ' ReadStatus As Byte 'Status de lecture: 0x0F=pas de suite 0x06=suite - ' MsgSize As Byte 'Nombre de caractères dans le message - ' MsgTab As String * 122 'Message lu + ' ErrorNumber As Integer 'Numéro d'erreur + ' ErrorBlockNum As Integer 'Numéro de bloc en erreur + ' ReadStatus As Byte 'Status de lecture: 0x0F=pas de suite 0x06=suite + ' MsgSize As Byte 'Nombre de caractères dans le message + ' MsgTab As String * 122 'Message lu - n_ErrorNumber = CUShort(BitConverter.ToInt16(MyData.data, 0)) - n_ErrorBlockNum = CUShort(BitConverter.ToInt16(MyData.data, 2)) - n_ReadStatus = MyData.data(4) - n_MsgSize = MyData.data(5) + n_ErrorNumber = CUShort(BitConverter.ToInt16(MyData.data, 0)) + n_ErrorBlockNum = CUShort(BitConverter.ToInt16(MyData.data, 2)) + n_ReadStatus = MyData.data(4) + n_MsgSize = MyData.data(5) - If n_ErrorNumber <> 0 Then + If n_ErrorNumber <> 0 Then - For i = 0 To CShort(n_MsgSize) - 1S - szt = szt & System.Convert.ToChar(MyData.data(i + 6)) - Next + For i = 0 To CShort(n_MsgSize) - 1S + szt = szt & System.Convert.ToChar(MyData.data(i + 6)) + Next - SyncLock Me - sz_NC_error_messages.Add(szt) ' add this message to the error message list - End SyncLock + SyncLock Me + sz_NC_error_messages.Add(szt) ' add this message to the error message list + End SyncLock + End If + + nErrorLineNum = nErrorLineNum + 1S + Else + n_ReadStatus = NO_MORE_MESSAGES End If - nErrorLineNum = nErrorLineNum + 1S + End While + + b_NC_error = True + End If ' any error + + global_state = eStateType.READY + + Return nret + + End Function + + Function VerifyBusy() As eStateType + For I As Integer = 0 To 50 + If global_state <> eStateType.READY Then + System.Threading.Thread.Sleep(SLEEP_TIME) Else - n_ReadStatus = NO_MORE_MESSAGES + Exit For End If + Next - End While + Return global_state - b_NC_error = True - End If ' any error - - global_state = eStateType.READY - - Return nret - - End Function - - Function VerifyBusy() As eStateType - For I As Integer = 0 To 50 - If global_state <> eStateType.READY Then - System.Threading.Thread.Sleep(SLEEP_TIME) - Else - Exit For - End If - Next - - Return global_state - - End Function + End Function #End Region -End Class + End Class End Namespace \ No newline at end of file diff --git a/Utility/Utility.vb b/Utility/Utility.vb index d802d8d..6b4cbc4 100644 --- a/Utility/Utility.vb +++ b/Utility/Utility.vb @@ -1,4 +1,5 @@ Imports System.Globalization +Imports System.Diagnostics Imports System.Collections.ObjectModel Imports System.Text.RegularExpressions Imports EgtWPFLib @@ -17,8 +18,25 @@ Module Utility End Function - '-------------------------------------------------------------------------------------------------- - Friend Sub UpdateUI() + Friend StopWatch As Stopwatch + + Friend Sub TimeSpanInit() + StopWatch = New Stopwatch + StopWatch.Start() + End Sub + + Friend Function TimeSpanEnd() + Dim sTime As String = "" + If Not IsNothing(StopWatch) Then + StopWatch.Stop() + Dim ts As TimeSpan = StopWatch.Elapsed + sTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds) + End If + Return sTime + End Function + + '-------------------------------------------------------------------------------------------------- + Friend Sub UpdateUI() ' Costringo ad aggiornare UI Dim nDummy As Integer Application.Current.Dispatcher.Invoke(Windows.Threading.DispatcherPriority.Background, _