mercredi 12 février 2014

Exemple Focas en VB Part 3

Pour faire suite à un précédent article, voici un petit exemple de code complémentaire sur la programmation sur CN Fanuc par la liaison FOCAS. Le code reprend l'utilisation de la fonction "Status_of_Function" définie dans le précédent billet. Cet exemple permet de relire le type de CN présent sur la machine .

'---------------------
' Ethernet connexion
'---------------------
' allocate library handle 3
Declare Function cnc_allclibhndl3 Lib "fwlib32.dll" (ByVal sIPaddr As String, ByVal nPort As Integer, ByVal nTimeout As Long, FlibHndl As Integer) As Integer
' free library handle
Declare Function cnc_freelibhndl Lib "fwlib32.dll" (ByVal FlibHndl As Integer) As Integer
' read CNC system information
Declare Function cnc_sysinfo Lib "fwlib32.dll" (ByVal FlibHndl As Integer, Odb As ODBSYS) As Integer
' cnc_sysinfo:read CNC system information
#If FS15BD Then

Type ODBSYS
  Dummy As Integer             ' dummy
  Max_axis As Integer          ' maximum axis number
  Cnc_type As String * 2       ' cnc type <ascii char>
  Mt_type As String * 2        ' M/T/TT <ascii char>
  Series As String * 4         ' series NO. <ascii char>
  Version As String * 4        ' version NO.<ascii char>
  Axes As String * 2           ' axis number<ascii char>
End Type

#Else

Type ODBSYS
  Addinfo As Integer           ' additional information
  Max_axis As Integer          ' maximum axis number
  Cnc_type As String * 2       ' cnc type <ascii char>
  Mt_type As String * 2        ' M/T/TT <ascii char>
  Series As String * 4         ' series NO. <ascii char>
  Version As String * 4        ' version NO.<ascii char>
  Axes As String * 2           ' axis number<ascii char>
End Type

#End If


Private Sub Form_Load()
   Dim LibHndl As Integer
    Dim Ret As Integer
    Dim SysInfo As ODBSYS
    Dim Result(16) As Integer
    Dim TypeS As String


'  192.0.0.1 Addresse connexion Ethernet sur CN
'  8193 : Port Focas
   Ret = cnc_allclibhndl3("192.0.0.1", "8193", 10, LibHndl)
   If Ret Then call Status_of_Function(Ret, "cnc_allclibhndl3")

' Recupère les informations sur la CN
    Ret = cnc_sysinfo(LibHndl, SysInfo)
    If Ret Then call Status_of_Function(Ret, "cnc_sysinfo")

'Affiche la description de la CN
    TypeS = "Fanuc " & CStr(SysInfo.Cnc_type)
' Addinfo
' bit 1
'0 : not an i Series CNC
'1 : i Series CNC


    Ret = ConvBin(SysInfo.Addinfo, Result) ' si 10 alors 1 : i Series CNC
    If Result(1) = 1 Then
     TypeS = TypeS & "i Series CNC"
    End If
   
 ' L'information MT_type indique si la CN est du type fraisage ou tournage
' M' : Machining center
' T' : Lathe
' MM' : M series with 2 path control
' TT' : T series with 2/3 path control
' MT' : T series with compound machining function

    TypeS = TypeS & " " & SysInfo.Axes & " axes"
    TypeS = TypeS & " " & SysInfo.Mt_type 
 
    MsgBox TypeS

'  De-allocate handle
    Ret = cnc_freelibhndl(LibHndl)
    If Ret Then Call Status_of_Function(Ret, "cnc_freelibhndl")
End Sub

' Conversion d'un entier en binaire
Function ConvBin(ByVal Number As Integer, Res() As Integer) As Integer
    Dim Rest As Integer
    Dim Result As String
    Dim IND As Integer
    IND = 0
   
    Do
        Rest = Number Mod 2
        Number = Number \ 2
        Res(IND) = Rest
        IND = IND + 1
        'Result = CStr(Rest) & Result
    Loop While Number > 0
   
    ConvBin = IND - 1
   
End Function

19 commentaires:

  1. Allo
    J'ai le CD de librairies FOCAS2 et ceci m'interesse particulierement! J'ai fais quelques macros en VB pour solidworks, mais je demarrais toujours d'une existante, serais t'il possible pour toi de nous faire un petit tutorial expliquant comment partir de zero pour creer une application Focas. Sur mon CD j'ai des headers en VB et en C+ , les libraries, mais je doit avouer que je ne comprends pas trop comment assembler le tout, quand je veux commencer mon projet, visual studio 2005 ne trouve pas de template sur mon ordi donc je bloque des le depart. Ou si tu aurais un projet deja fait que je pourrais analyser pour comprendre le fonctionnement de tout ca ce serais tres apprécié. Mon email est vincent_pomerleau@hotmail.ca

    RépondreSupprimer
  2. Bonsoir,

    Désolé mais je ne programme qu'avec vb6 je n'ai donc aucun projet en vb.net

    RépondreSupprimer
  3. dsl j avais oublié ce projet,je souhaitais travailler en VB6 aussi, je me suis mal exprimé, j aimerais juste avoir un peu d'aide pour commencer du debut, mes connaissances sont tres limitées en prog

    RépondreSupprimer
  4. How do you read the variables? For example # 3901

    RépondreSupprimer
  5. Should be something like

    Dim MacroInfo As ODBM
    Ret = cnc_rdmacro(nLibHndl, 3901, 10, MacroInfo)

    You can also test with

    Dim MacroInfo3 As IODBMRN3
    Ret = cnc_rdmacror3(nLibHndl, 3901, 0, MacroInfo3)

    RépondreSupprimer
  6. Thank you for listing on variable # 3901.
    Of your article, is there a demo that can be downloaded to study it and try it out?
    Thank you
    Massimo

    RépondreSupprimer
  7. I have difficulty retrieving the cycle time with "cnc_rdtimer".
    I get it with the 3rd, but not so manage the result that is a huge number even if in fact they are a few minutes.

    my code:
    Dim TempoInfo As IODBTIME
    Dim TempoCiclo As String
    Ret = cnc_rdtimer(LibHndl, 3, TempoInfo)
    TempoCiclo = Int(TempoInfo.Minute) & " " & Int(TempoInfo.msec)
    MsgBox TempoCiclo

    How can it be transformed?
    Thanks
    Massimo

    RépondreSupprimer
    Réponses
    1. Ce commentaire a été supprimé par l'auteur.

      Supprimer
    2. We recently faced the same issue: after a bit of trial we found that the endianness of the values is "wrong": to get correct numbers we simply revert the order of the 4 bytes of every value.

      Supprimer
  8. I don't think that cnc_rdtime give you the Cycle time. I'm using personnaly

    Dim CycleSec As IODBPSD3
    Dim CycleMin As IODBPSD1

    'Cycle Time
    Ret = cnc_rdparam(nLibHndl, 6757, 0, 8, CycleSec)
    Ret = cnc_rdparam(nLibHndl, 6758, 0, 8, CycleMin)
    TypeS = CycleMin.Cdata & " min " & Round(CycleSec.Ldata / 1000, 0) & " sec"

    RépondreSupprimer
  9. After a Short Test .. Yes cnc_rdtimer give me the same result:

    Dim CycleTime As IODBTIME
    Ret = cnc_rdtimer(nLibHndl, 3, CycleTime)
    TypeS = CycleTime.nMinute & " min " & Round(CycleTime.nMsec / 1000, 0) & " sec"
    TextTime = TypeS

    RépondreSupprimer
  10. Thanks so much for the help.
    I with cnc_rdtimer for 1 minute and 15 seconds (rounded seconds) I receive 16777216 minutes -2143486 seconds. This is the code:

    Ret = cnc_rdtimer(LibHndl, 3, CycleTime)
    If Ret Then Call Status_of_Function(Ret, "cnc_rdtimer")
    CycleInfo = CycleInfo & CycleTime.nMinute & " minuti " & Round(CycleTime.nMsec / 1000, 0) & " secondi"

    If I wanted to use the 6757 seconds and 6758 minutes to retrieve the cycle time, I can not find the time parameter. You know him?

    And to retrieve the running program number, what do you use? Can you show me the code?

    Thank you very much
    Maximum

    RépondreSupprimer
  11. I' sorry for your issue with cnc_rdtimer but this funtion works fine for me , the result return by this function or by the parameter #6757 and #6758 give the same result.

    I don't realy understand what do you mean by "I can not find the time parameter. You know him?"

    And last point the prog number :

    Dim MainProg As ODBPRO

    ' Current prog
    Ret = cnc_rdprgnum(nLibHndl, MainProg)
    TypeS = " O" + Format(MainProg.Mdata, "0000")

    You can also Get the Program comment with :

    ProgNum = MainProg.Mdata

    Dim ProgName As PRGDIR3

    Ret = cnc_rdprogdir3(nLibHndl, 1, ProgNum, 1, ProgName)
    TypeS = ProgName.sComment

    RépondreSupprimer
  12. Thank you very much for the time you devote me, in the afternoon I try to recover the program number.
    For the cycle time if I can not with cnc_rdtimer, I wanted to reopen as you do with the parameters 6757 for second and 6758 for minute. But for hours? I can not find the parmeter.
    Sorry if I did not understand it, use the automatic translator.
    Thank
    Massimo

    RépondreSupprimer
  13. For the program, use your own code, but I get a weird number. Same cycle time problem.
    My check is 31i I put my full code with statements.
    I do not understand if I'm wrong

    ' CNC_RDPRGNUM
    Private Declare Function cnc_rdprgnum Lib "fwlib32.dll" (ByVal FlibHndl As Integer, OdbPrg As ODBPRO) As Integer

    ' CNC_RDPRGNUM
    Private Type ODBPRO
    dummy As Integer
    Data As Integer
    mdata As Long
    End Type

    'Programma
    Dim ProgrammaCN As ODBPRO
    Dim Programma As String

    'Info su Programma
    Ret = cnc_rdprgnum(LibHndl, ProgrammaCN)
    If Ret Then Call Status_of_Function(Ret, "cnc_rdprgnum")
    Programma = "Programma in esecuzione: "
    Programma = Programma & " " & ProgrammaCN.Data

    For the O2864 program I receive O187697968.
    High numbers like cycle times.

    Thanks
    Massimo

    RépondreSupprimer
  14. I think it's a problem with the ODBPRO type definition

    Try :

    Type ODBPRO
    Dummy(0 To 1) As Integer ' dummy
    Data As Integer ' running program number
    Mdata As Integer ' main program number
    End Type


    Type IODBTIME
    nMinute As Long
    nMsec As Long
    End Type

    There is no parameter for process time in hours , just minutes : 120 minutes = 2 hours

    RépondreSupprimer
  15. Hello,
    in your code to read the cycle time by parameters:

    'Cycle Time
    Ret = cnc_rdparam(nLibHndl, 6757, 0, 8, CycleSec)
    Ret = cnc_rdparam(nLibHndl, 6758, 0, 8, CycleMin)
    TypeS = CycleMin.Cdata & " min " & Round(CycleSec.Ldata / 1000, 0) & " sec"

    how do I declare the function IODBPSD1 and IODBPSD3?
    And the Type?

    thank you very much
    Massimo

    RépondreSupprimer
  16. Hi
    most in depth examples but it does not work in VB.net 2017
    thank you

    RépondreSupprimer