Bonjour à tous,

J'essaie de collecter les informations d'un PLC Siemens S7-300 depuis Excel en utilisant Libnodave.dll.
Avec les exemples de programme que j'ai pu trouver sur internet ça a été facile de collecter quelques données.
Mais je dois collecter plusieurs fois la même donnée, dans l'idée de sortir un graphique.


La plupart du temps (sinon toujours) les premières données sont collectés correctement, et au bon d'un moment Excel ne répond plus et freeze.

J'ai alors essayé de vérifier la connexion à chaque boucle, et effectivement après un certain nombre de boucle (aléatoire) je perds la connexion.
Si j'essaie ensuite de redémarrer la macro readFromPLC() j'obtiens le message "No route PLC, check connexion and settings"

Le seul moyen de rétablir la connexion est de redémarrer Excel.

Est-ce qu'il y a la possibilité de faire un reboot de la connexion sans avoir à redémarrer Excel?

Si quelqu'un à un conseil je suis preneur,

Merci d'avance

Ci-dessous le code:
_________________________________________________________________________________________________________________________
Private Function initialize(ByRef ph As Long, ByRef di As Long, ByRef dc As Long)

ph = 0
di = 0
dc = 0
Dim time2 As Long
initialize = -1
res = -1
peer$ = ActiveWorkbook.Worksheets("VarTab").Cells(2, 9)
ph = openSocket(102, peer$) ' for ISO over TCP
If (ph > 0) Then
di = daveNewInterface(ph, ph, "IF1", 0, daveProtoISOTCP, daveSpeed500k)
res = daveInitAdapter(di)
If res = 0 Then
MpiPpi = Cells(6, 5)
dc = daveNewConnection(di, MpiPpi, ActiveWorkbook.Worksheets("VarTab").Cells(3, 9), ActiveWorkbook.Worksheets("VarTab").Cells(4, 9))
res = daveConnectPLC(dc)
If res = 0 Then
initialize = 0
End If
End If
End If
End Function
_________________________________________________________________________________________________________________________
Private Sub cleanUp(ByRef ph As Long, ByRef di As Long, ByRef dc As Long)
If dc <> 0 Then
res = daveDisconnectPLC(dc)
Call daveFree(dc)
dc = 0
End If
If di <> 0 Then
res = daveDisconnectAdapter(di)
Call daveFree(di)
di = 0
End If
If ph <> 0 Then
res = closePort(ph)
res = closeSocket(ph)
ph = 0
End If
End Sub
_________________________________________________________________________________________________________________________
Sub readFromPLC()
Dim ph As Long, di As Long, dc As Long, iRow As Integer, dbnum As String, addrOffset As String, addrBit As String, Sample As Integer
Dim TagName As String, TagAdress As String, TagType_array() As String

Sample = ActiveWorkbook.Worksheets("VarTab").Cells(14, 9).value + 2
res = initialize(ph, di, dc)
If res = 0 Then

For iRow = 3 To Sample
Faulty = False

If iRow > 3 Then
res = initialize(ph, di, dc)
End If
If res = 0 Then
TagAdress = ActiveWorkbook.Worksheets("VarTab").Cells(3, 3).value
TagType_array = Split(ActiveWorkbook.Worksheets("VarTab").Cells(3, 3).value, ".")
dbnum = Replace(TagType_array(0), "DB", "")
If UBound(TagType_array) > 1 Then
addrOffset = Replace(TagType_array(1), "DBX", "")
addrBit = TagType_array(2)
res2 = daveReadBytes(dc, daveDB, dbnum, addrOffset, 1, 0)
If res2 = 0 Then
Dim bfbyte As Byte
Dim bitStat As Integer
Dim bitPos As Byte
bitPos = CByte(addrBit)
bfbyte = daveGetU8(dc)
bitStat = bfbyte And 2 ^ bitPos

If bitStat > 0 Then
ActiveWorkbook.Worksheets("VarTab").Cells(iRow, 4) = True
Else
ActiveWorkbook.Worksheets("VarTab").Cells(iRow, 4) = False
End If
Else
ActiveWorkbook.Worksheets("VarTab").Cells(iRow, 4) = "#####"
ActiveWorkbook.Worksheets("VarTab").Cells(iRow, 4).Interior.Color = RGB(255, 0, 0)
End If
Else

If InStr(TagType_array(1), "DBD") > 0 Then
addrOffset = Replace(TagType_array(1), "DBD", "")
res2 = daveReadBytes(dc, daveDB, dbnum, addrOffset, 4, 0)
If res2 = 0 Then
ActiveWorkbook.Worksheets("VarTab").Cells(iRow, 4) = daveGetFloat(dc)
Else
ActiveWorkbook.Worksheets("VarTab").Cells(iRow, 4) = "#####"
ActiveWorkbook.Worksheets("VarTab").Cells(iRow, 4).Interior.Color = RGB(255, 0, 0)
End If
ElseIf InStr(TagType_array(1), "DBW") > 0 Then
addrOffset = Replace(TagType_array(1), "DBW", "")
res2 = daveReadBytes(dc, daveDB, dbnum, addrOffset, 2, 0)
If res2 = 0 Then
ActiveWorkbook.Worksheets("VarTab").Cells(iRow, 4) = daveGetU16(dc)
Else
ActiveWorkbook.Worksheets("VarTab").Cells(iRow, 4) = "#####"
ActiveWorkbook.Worksheets("VarTab").Cells(iRow, 4).Interior.Color = RGB(255, 0, 0)
End If
ElseIf InStr(TagType_array(1), "DBB") > 0 Then
addrOffset = Replace(TagType_array(1), "DBB", "")
res2 = daveReadBytes(dc, daveDB, dbnum, addrOffset, 1, 0)
If res2 = 0 Then
ActiveWorkbook.Worksheets("VarTab").Cells(iRow, 4) = daveGetU8(dc)
Else
ActiveWorkbook.Worksheets("VarTab").Cells(iRow, 4) = "#####"
ActiveWorkbook.Worksheets("VarTab").Cells(iRow, 4).Interior.Color = RGB(255, 0, 0)
End If
End If
End If
Else
Faulty = True
GoTo Fault
End If

Fault: Call cleanUp(ph, di, dc)
If Faulty = True Then
ActiveWorkbook.Worksheets("VarTab").Cells(iRow, 4) = "FAULT"

End If

Next
Else
MsgBox "No route PLC, check connection and settings"
Call cleanUp(ph, di, dc)
End If

End Sub