VB.Net – exchange data with Siemens PLC

Version of the communication library suitable for data exchange with Siemens PLC and VB.Net application you can also find on the same website.

You need copy libnodave.net.dll file in the your PC and add references in the VB.NET project properties. This implements a bridge layer between libnodave.dll and applications written in MS .Net languages.

Also copy libnodave.dll in the same directory where is exe file is located.

You can see available functions in the DLL.

Library contains all necessary functions for:

Connection with PLC

Public fds As libnodave.daveOSserialType
Public di As libnodave.daveInterface
Public dc As libnodave.daveConnection
Public res As Integer
Public buf(100) As Byte
Public localMPI As Integer = 0
Public rack As Integer = 0
Public slot As Integer = 2
Public plcMPI As Integer = 2
Public Connection As Boolean = False
Public IP As String = "192.168.1.100"

Private Sub ConnectPLC.....
  fds.rfd = libnodave.openSocket(102, IP)
  fds.wfd = fds.rfd

  If fds.rfd > 0 Then       ' if step 1 is ok
    di = New libnodave.daveInterface(fds, "IF1",
                    0, libnodave.daveProtoISOTCP,
                    libnodave.daveSpeed187k)

    di.setTimeout(1000000)
    res = di.initAdapter
    If res = 0 Then       ' init Adapter is ok
       ' rack amd slot don't matter in case of MPI
       dc = New libnodave.daveConnection(di, 0,
                                          rack, slot)
       res = dc.connectPLC()
       If res = 0 Then
         Connection = True
         ToolStripStatusLabel1.Text = "Connected " + IP
       End If
    End If
  End If
End Sub

Private Sub DisconnectPLC....

  dc.disconnectPLC()
  ToolStripStatusLabel1.Text = "Disconnected."

End Sub

– Read data from PLC


Private Sub Timer1......

  If Connection Then
  'reading 16 bytes

  res = dc.readBytes(libnodave.daveFlags, 0, 10, 16, buf)
  If res = 0 Then
    Label1.Text = Str(dc.getS32)  'DINT 4 bytes
    Label2.Text = Str(dc.getS32)  'DINT 4 bytes
    Label3.Text = Str(dc.getS32)  'DINT 4 bytes
    Label4.Text = Str(dc.getFloat) 'float 4 bytes
  End If

  res = dc.readBytes(libnodave.daveInputs, 0, 96, 2, buf)
  If res = 0 Then
    Label5.Text = Str(dc.getU16)  'WORD 2 bytes
  End If

  res = dc.readBytes(libnodave.daveFlags, 68, 8, 4, buf)
  If res = 0 Then
    Label6.Text = Str(dc.getU32)  'DB68.DBW8 DWORD 4 bytes
  End If
End Sub

– Write data to PLC

Private Sub Button2_Click(ByVal sender....
  Dim x1 As Double = 12345.9
  Dim x2 As Integer = 123456
  Dim x3 As Short = 1234
  Dim x4 As Byte = 34

  If Connection Then
    'write 4 bytes s7 float to MD4
     buf = BitConverter.GetBytes(libnodave.daveToPLCfloat(x1))
    res = dc.writeBytes(libnodave.daveFlags, 0, 4, 4, buf)

    'write 4 bytes s7 DWORD or DINT to MD12
    buf = BitConverter.GetBytes(libnodave.daveSwapIed_32(x2))
    res = dc.writeBytes(libnodave.daveFlags, 0, 12, 4, buf)

    'write 2 bytes s7 WORD or INT to MW568
    buf = BitConverter.GetBytes(libnodave.daveSwapIed_16(x3))
    res = dc.writeBytes(libnodave.daveFlags, 0, 568, 2, buf)

    'write byte to MB80
    buf(0) = x4
    res = dc.writeBytes(libnodave.daveFlags, 0, 80, 1, buf)

    'write 2 bytes s7 WORD or INT to DB34.DBW2
    buf = BitConverter.GetBytes(libnodave.daveSwapIed_16(x3))
    res = dc.writeBytes(libnodave.daveFlags, 34, 2, 2, buf)
  Else
    MsgBox("No connection with PLC!")
  End If
End Sub

 

– Write bits  to PLC

Private Sub Button3_Click(....
  Dim Adr As Integer
  Dim Par As Boolean = True

  Dim InputNum As Integer = 20 'input I20.3
  Dim BitNum As Integer = 3

  If Par Then
    buf(0) = 255 'write 1
  Else
    buf(0) = 0 'write 0
  End If
  If Connection Then
    Adr = InputNum * 8 + BitNum

    'write Input I20.3
    res = dc.writeBits(libnodave.daveInputs, 0, Adr, 1, buf)

    'write Merke M20.3
    res = dc.writeBits(libnodave.daveFlags, 0, Adr, 1, buf)

    'write output Q20.3
    res = dc.writeBits(libnodave.daveOutputs, 0, Adr, 1, buf)
  Else
    MsgBox("No connection with PLC!")
  End If

End Sub

 

– Reading byte from PLC and setting bits


Private Sub Timer1_Tick(....
  Dim a As Byte

  If Connection Then
    res = dc.readBytes(libnodave.daveInputs, 0, 0, 1, buf)
    If res = 0 Then
      a = dc.getU8
      If IsBitSet(a, 0) Then
        Panel1.BackColor = Color.Lime
      Else
        Panel1.BackColor = Color.DarkGreen
      End If
      If IsBitSet(a, 1) Then
        Panel2.BackColor = Color.Lime
      Else
        Panel2.BackColor = Color.DarkGreen
      End If
  Else
    ToolStripStatusLabel1.Text = "Read data. " + libnodave.daveStrerror(res)
  End If

End Sub

Public Function IsBitSet(ByVal InByte As Byte, ByVal Bit As Byte) As Boolean
  'Is a n'th bit in InByte 1 of not?
  IsBitSet = ((InByte And (2 ^ Bit)) > 0)
End Function

 


You can use function readBits too.

With  libnodave library is easy to read and write data from and to a S7 PLC.

With function readBytes you can read data from specified memory area of the PLC (inputs, outputs, flags, DB) in the buffer as a sequence of bytes. Then you can read content of the buffer with the required format.

To write data to PLC must first complete sequence of bytes with data in them and then write them to PLC with function writeBytes. In this case you must know exactly defined format data in the PLC.

It can also read multiple items.  The purpose of this mechanism is to read multiple blocks of bytes from different start addresses or memory areas with a single request to the PLC.

Source of the short example.

VS_source

Example visualization of controlling RSP Uniwax machine using the libnodave.dll and full simulation of machine interfacing with S7PROSIM COM component. So example can use for simulation of the machine interfaces or for monitoring of the control machine.

VS_source

Advertisements

65 Responses to “VB.Net – exchange data with Siemens PLC”

  1. Licio Says:

    Hi Alex,
    First of all thanks for making this information avalible.
    I’m buzy to create a datalogger for S7-200 and maybe you can help me out.
    I would like to use libnodave function prepareReadRequest but I do not completelly understand how to read and write DI, DO, AI and AO signals.
    Below the code I wrote for te job.

    ‘Defined outsie sub
    ‘Dim fds As libnodave.daveOSserialType
    ‘Dim di As libnodave.daveInterface
    ‘Dim dc As libnodave.daveConnection

    Sub readMultipleItemsFromPLC()

    Dim res As Long
    Dim mi As libnodave.PDU
    Dim resultSet As libnodave.resultSet

    mi = dc.prepareReadRequest()
    mi.addVarToReadRequest(libnodave.daveInputs, 0, 0, 1)
    mi.addVarToReadRequest(libnodave.daveOutputs, 0, 0, 2)

    resultSet = New libnodave.resultSet
    res = dc.execReadRequest(mi, resultSet)
    If res = 0 Then
    res = dc.useResult(resultSet, 0)
    AddStatus(“- ” + Str(dc.getS32()))
    AddStatus(“- ” + Str(dc.useResult(resultSet, 0)))
    AddStatus(“- ” + Str(dc.useResult(resultSet, 1)))
    Label4.Text = “PLC multi request OK ” + Str(res)
    Else
    Label4.Text = “PLC multi request error ” + Str(res)
    End If

    End Sub

    The data I get is not matching the signal avalible on the PLC.
    Could you please have a loock and tell me wat’s is incorrect.

    Many Thanks

    Licio

  2. Licio Says:

    Hi Alex,

    Thanks for your quick replay, problem is solved.
    If you are interested I kan send you the files.

    Thanks again

    Licio

    • Steven Says:

      yeah great, i also try to get a connection between PC and S7, but it doesn’t work.
      Can you send me those files?

      Greets

      Steven

  3. Mike Says:

    I am very excited about what is described above, immediately going to experiment with it. Was absolutely in need of this for “normal” S7-300 applications. Used to have it as a standard built in feature for Simotion applications. Missed it so much I was thinking about buying a logic analyzer privately, I might still because that will be better for some really fast changing signals, but think doing it in software will cover most situations.

    Always better to copy than to make yourself, economically speaking.. 🙂 Although I do enjoy inventing something myself as well. Just finished implementing the M-bus protocol on S7-300, both Siemens and the M-bus instrument manufacturer (Kamstrup energy meter) told me their products had not been combined yet, and therefore no solution existed. Poured a lot of effort into it, got it working seamlessly and elegantly. Looking at the end result it seems really simple, a 320 line state machine with just 7 states, programmed in SCL. But getting there definitely was not easy.

    Thanks for the .dll beforehand, now have to get it to work for me, but looks nice already!

  4. Luis Says:

    Hello, thank you very much for sharing this library, I have worked with her and I managed to make a small application, but I need to know how I can change the CPU operating status (RUN / STOP) from this application.

    I imagine that avoids an address memory allows.

    thank you very much in advance

    Note: Translated with Google.

  5. Peter Says:

    Hello, I wrote program with Libnodave and MPI/Profibus communication. It’s working on my computer, but when I copy this program to another PC it doesn’t work. There is installed PG/PC interface. What can be the problem?

  6. Peter Says:

    I have “libnodave.net.dll” in “Release” directory. Should I copy “libnodave.dll”? I have installed Step 7 V5.5. I have a connection with PLC, because I see it in online mode.

  7. alexsentcha Says:

    Peter, “libnodave.dll is necessary because” libnodave.dll’s is library self, but libnodave.net.dll link with .Net.

  8. Peter Says:

    Thanks, It’s work 🙂 I have another question. I see that I need ‘S7online’ to communication with PLC. ‘S7online’ is ‘PG/PC Interface’ in contol panel. I installed NCM manager, because it take little space, but I need license for this. Is there any other way to connect to PLC without license?

  9. alexsentcha Says:

    No problem.You can look on the Internet file Simatic_EKB_Install That’s all licenses.

  10. Peter Says:

    I was thinking about something legal 🙂 thank you for help

  11. alexsentcha Says:

    For LibNoDave you don’t need any license. License need only for Step 7. But if you use Siemens PLC i think you have needed license. If you don’t have, then you can try Simatic_EKB_Install.

  12. Sedat Says:

    Hello. I have done corrections according to your explanation above. I am trying to connect PLC via ISO TCP. But I receive an error message “BadImageFormatException was unhandled”. What can be the reason? How can I send screenshot?
    Regards,
    Sedat

    • Paul Says:

      I Have the same problem: Is a solution already known ?

      • Paul Says:

        Problem solved:
        in VB: set compiler to x86: no 64 bit compilation: to do this :
        (VB.NET2010 Express)
        Menu: Build\Configuration manager: default platform is any CPU. change this to x86.

        + Copy libnodave.dll and libnodave.net.dll in Exe path.

  13. Robert Says:

    Hallo Alex.

    This is great article. I have one question. Is it working this also with MPI adapter via USB? When yes, can you please describe, how to setup variables in code? Thaks and good luck.

    • Robert Says:

      Thanks Alex.

      I found MPI TS adapter with RS 232 connection and I using this with RS232/USB adapter. Is this case same like MPI with USB or it is possible use this with RS232/USB converter?Because if I tried this, I can open port “(fds.rfd = libnodave.setPort(“COM7”, “19200”, AscW(“0″))”, initialitze adapter “di = New libnodave.daveInterface(fds, “IF1″, localMPI, libnodave.daveProtoMPI, libnodave.daveSpeed19k)” after I have “res=0”, but after initialize connection “dc = New libnodave.daveConnection(di, plcMPI, rack, slot)” my “res=4”. Can you please explain me, where can be problem?. Thank you.
      Robert

  14. Ugo Says:

    Hallo,
    Thanks for excellent work
    I try connect with LOGO!0BA7 Siemens but don’t work
    i have found on weintek site the parameter for connect with Logo the parameter are:
    PLC Type S7/200 port:102 PLC sta:1 Local TSAP:1000 Rem TSAP: 2100 I want try with C Libnodave, can you help me for set parameter?
    thank you very much

  15. Ugo Says:

    Hello Alex
    Thanks for the quick response.
    I’m trying to connect via ethernet with the library libnodave
    c in c + + but I do not know how to set up to connect to the Logo
    I think I should change the local and remote TSAP

  16. andy Says:

    Hello Alex !

    I was wondering : is it possible to connect to a Siemens 1200 plc?

    Could it be done with your dll?

    Thx

  17. Paolo Says:

    Hello Mr.Alex
    How can i write a real value in DB data, whit libnodave.net and VisualStudio2010.
    ex. db150.dbd0
    Rgds

  18. Emma Avworo Says:

    Alex
    Thanks for a great detailed job on the equally helpful libnodave library. I have a problem, each time I use the examples, vb2005 tells me libnodave not defined, and libnodave.daveSerialType not defined. Is there a declaration file am missing?

    Emma

  19. Luis Says:

    Hi Alex!
    Thank you for sharing this library, I have worked with her and I managed to make a small application, but I need to know how I can write to PLC a data type boolean ex. A0.0 and also how I can read and write several PLC’s with a single application?, and other cuestion, where do I find a user guide for this library?.
    Thanks in advanced

  20. mario Says:

    hi,
    do you know how to write a float value to plc memory instead a single (or array of) bytes? i’m using vb. there are functions for recover formatted values from the read buffer, but none for write some data greater than a byte in the write buffer

    thanks in advance!

  21. Andries Says:

    Has anyone out there a working C# example for Siemens Logo!OBA7 (networked in master-slave mode) ?

    Here is my experience with libnodave and Logo! OBA7 until now:
    Very good starting points:
    http://yours-truly.de/node/2046
    http://www.ip-symcon.de/forum/threads/15059-Neue-Siemens-Logo-0BA7-über-Ethernet-an-IPS

    In my case:
    -2 logo’s, network connected in master-slave modes (used for light switching in my house)
    -Needed to set the master logo to decentral communication as explained in the above article
    -used rack 1, slot 0, port 102, ip address

    Connection works fine but i’m only able to WriteBytes() to daveFlags at this point. Setting daveOutPuts or reading daveInputs is not working and i don’t know why. Any information about master-slave (network input/outputs) would also be appreciated.

    Please share your experience, this could be great stuff to play with !
    Thanks in advance !

    • Andries Says:

      Hi Alex,

      Thanks for your reply.

      Do you know if it’s possible to directly modify the outputs ?
      (Only writing to flags, reading inputs is working fine for me at this point)

      I can integrate flags in my diagram and write to them, but this is a lot of work, makes diagram less attractive, and the number of flags is limited.

      Thanks in advance !

  22. Udit Says:

    I tried to connect Siemens S7-300 PLC using VS 2012 with windows 7. but didn’t get success, can any one help me?.

    int i, a = 0, j, res, b = 0, c = 0;
    float d = 0;

    fds.rfd = libnodave.setPort(“COM1”, “19200”,’O’); //19200
    fds.wfd = fds.rfd;
    if (fds.rfd > 0)
    {

    di = new libnodave.daveInterface(fds, “IF1”, 0, libnodave.daveProtoMPI2, libnodave.daveSpeed187k); // fds, “IF1”, 0, libnodave.daveProtoISOTCP, libnodave.daveSpeed187k //fds, “IF1”, localMPI, libnodave.daveProtoMPI, libnodave.daveSpeed187k
    di.setTimeout(1000000);
    res = di.initAdapter();
    if (res ==0)
    {
    dc = new libnodave.daveConnection(di, 2, 0, 0);
    // dc = new libnodave.daveConnection(di, 0, 0, 2);
    int x=dc.connectPLC();
    if (4== dc.connectPLC())
    {
    // Console.Write(“PLC Connected”);
    byte[] buf = new byte[10];
    // res = dc.readBytes(libnodave.daveFlags, 0, 0, 16, buf);

    res= dc.readBytes(libnodave.daveFlags, 0, 0, 16, buf);
    // res = dc.readBytes(libnodave.daveDB, 0, 0, 16, buf);
    // res = dc.readBits(libnodave.daveProtoUserTransport, 0, 0, 16, buf);
    if (res == -3)
    {

    //a = dc.getCounterValueAt(0);
    //Console.WriteLine(“Count: %d, “, c);
    a = dc.getS32();
    b = dc.getS32();
    c = dc.getS32();
    d = dc.getFloat();
    Console.WriteLine(“FD0: ” + a);
    Console.WriteLine(“FD4: ” + b);
    Console.WriteLine(“FD8: ” + c);
    Console.WriteLine(“FD12: ” + d);

    }
    else
    Console.WriteLine(“error ” + res + ” ” + libnodave.daveStrerror(res));
    }
    dc.disconnectPLC();
    }
    di.disconnectAdapter();
    libnodave.closePort(fds.rfd);
    }
    else
    {
    Console.WriteLine(“Couldn’t open serial port “);
    return -1;
    }
    return 0;
    }
    }

  23. sedat atalay Says:

    hello. I need to set a memory bit such as M200.3 in S7-300. Can you send me an example program for VB.net?

  24. nicksar Says:

    Hi Alexandr,
    or any
    Where is locations for sours VB.net application?
    Thanks
    Nick

  25. dimtiris Says:

    Dear thanks for this perfect lib. can i connect my logo plc with the vb app with the programming cable?

  26. Tamas Says:

    Hy Alex,
    I can connect to the PLC succesfully (S7-300).
    But I have a problem, When I set the last bit of byte (example: I set db189.dbx2.7) and I want read It, It’s generate error (OverflowException was unhandled).

    res = dc.readBytes(libnodave.daveDB, 189, 2, 1, buf)
    If res = 0 Then
    a = Str(dc.getS8)
    If IsBitSet(a, 7) Then
    Panel8.BackColor = Color.Lime
    Else
    Panel8.BackColor = Color.DarkGreen
    End If

    If the last bit is not set, anything else is working. (I use visual studio 2012)
    Have you got any idea?

  27. Raja G Says:

    Im new for vb and Im interested in vb base plc, I need any simulation tools to do the vb pprogramming and test . Please advise. .

  28. nguyen vu Says:

    Hi all
    I’m writing applications for siemens s7-1200 and Libnodave library, I want to run and stop the CPU but I do not know what commands to use in the library, I hope you can help me

    • nguyen vu Says:

      I use code, but it does not work.
      I write language VB.net
      dim res as integer
      dim dc as libnodave.daveconnection
      res=dc.stop
      and I want read status mode s7-1200 Run/Stop , I use commands .
      I hope you can help me

    • nguyen vu Says:

      people can not help me answer the question

    • nguyen vu Says:

      i see library libnodave , but I am not used
      Protected Shared Function daveStart(dc As System.IntPtr) As Integer
      I hope you can help me
      I see very little libnodave library documentation in order to use the library. What you are able to share documents with people

  29. stijn Says:

    Hello, I’m trying to read a value from a counter on my s7-1200. But I keep get value 10 as a result from my code. Here is how I do it:

    Dim b(1000) As Byte
    Dim res As Integer = dc.readBytes(libnodave.daveCounter, 0, 0, 1, b)
    Dim iCounterValue As Integer = dc.getCounterValue()

    The value I get from the dc.readBytes is always 10, what am I doing wrong? Any Ideas?

  30. Andreas Bergman Says:

    Hello, does anyone no how to list the blocks in simatic manager or in the online plc with libnodave(or another dll-file) to a listbox placed on a userform? I want to for example select a datablock in the listbox and then loop throw the selected datablock and read the actual values of db in the plc to a spreadsheet(or a texfile).

    Regards Andreas

  31. xvanic04 Says:

    Hello,
    I am a .NET programmer new in the region of automation. Therefore I am sorry for my stupid question. Is it possible to download data (spindle speed,…) directly from PLC using mentioned library(program running anywhere in the network)? We have PCU20 which runs under DOS, NC-DDE is not available.
    Thank you for any help.
    Radek

  32. Peter Says:

    Hello Alex, I wrote app based on MPI/DP communication in VB.NET. It’s working, but I need high reliability. When I disconnect cable and connect again communication return, but not always. Sometimes I have error:

    “The runtime has encountered a fatal error. The address of the error was at 0x6961a3da, on thread 0x18f4.
    The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code.
    Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.”

    “0x6961a3da” and “0x18f4” are usually different. It’s always error “0xc0000005”.
    This error is showing in different moment. Sometime for 10 disconnect, and sometime for 100 disconnect.

    I notice that I don’t have this error when I read only one data block. For example:
    RetVal = dc.readBytes(132, 205, 400, 210, ReceivedDataBytes)
    But when I read two or more data block then error is appear (When I disconnect of course).
    What can be the reason?

  33. Andries Says:

    Hello,

    Do you know how to read network outputs (NQ) from 2 logo’s connected as master-slave (the program runs on the master only) with libnodave ?

    In other words, where does the program store ‘network outputs status’ in memory ? Any advice/tip is welcome…

    • Andries Says:

      Hi, Thanks for info. I was hoping that network outputs was stored somewhere in DaveDb memory area.

    • Andries Says:

      Hi Alexsentcha,

      I found a note that gives me some new hope on getting network outputs to work on http://www.automation-se.de/index.php/contlogohowto :

      “Netzwerkeingänge Analog oder Digital werden über den DB1 abgebildet, so daß der Netzwerkeingang V0.0 z.B. über den DB1.DBX0.0 erreichbar ist. Analog dazu ist das Eingangswort VW4 über DB1.DBW4 erreichbar.”

      Unfortunately daveReadBytes(daveDB,1, 0, 4, null) gives me all 0 bytes (while NQ1 is high).

      I’m still fighting this problem…

  34. paul1611 Says:

    Hello,
    Thanks for sharing great library,
    I did many times by your guide, but the connection was false.
    I replaced “di = New libnodave.daveInterface(fds, “IF1″,0, libnodave.daveProtoISOTCP, libnodave.daveSpeed187k)” by “di = New libnodave.daveInterface(fds, “IF1″, 0, 132, 2)”
    the connection is true.
    when i try read bits from PLC
    If Connection Then
    res = dc.readBytes(libnodave.daveFlags, 0, 0, 16, buf) >> error here.
    can you help me work out this problem, i use PLC S7-1200 and VB.net 2012
    I’m looking forward you reply.

    Rergards.

  35. Andries Says:

    In case someone has the same problem:
    You can’t change input/outputs that are linked to other logic. The input/output changes for some millisec but is recycled on the next processor run (logic takes priority). The solution is to add flags (M) in your program logic, the output is then controlled by the logic only.

    In case you have not enough flags available you can use eg. 5 flags to have 2^5=32 different states. Each state can modify output.

    Using the same flags in your logic you can write (not read) network outputs from slave devices.

    The only problem that lasts is how to read network outputs…

    LibNoDave is great tool !

  36. George A. Loukas Says:

    Great Library,it’s been really helpful.

    Although i would like to attach an LM35 which uses an analog input.
    Could you please let me know how i will read this input first in simatic and then in VB?

    Thanks in advance

  37. Benoit LAMBERT Says:

    Hi all,
    First I would to say thanks for all for sharing sample, source, comments,….
    And more important thanks Alex….

    I’m not programmer, automatician or something like that. So I’m sorry if re-aske you about this source.

    I read “all” post tand if I understand the library “libnodave” work on S7-200-300-1200. With protocol communication TCP IP (ethernet).
    Some tried with a MPI/DP not give good results!!?

    I have a S7-314c 2DP~ with a MPI/DP bought on ebay with only driver for XP I don’t find for 7.

  38. PRADEEP.G Says:

    Hi Mr. Alex.. Thanks for the awesome work and info…I have developed a code, like below…using your info…However when i try to connect, it is getting an exception “Couldnot find the DLL” during execution of the code libnodave.openSocket(102, IP)…I had added the reference and copied libnodave.dll to my project folder also. But still i face this problem..Can u please help

  39. PRADEEP.G Says:

    My code

    Imports libnodave
    Public Class Form1
    Public fds As libnodave.daveOSserialType
    Public di As libnodave.daveInterface
    Public dc As libnodave.daveConnection
    Public res As Integer
    Public buf(100) As Byte
    Public localMPI As Integer = 0
    Public rack As Integer = 0
    Public slot As Integer = 2
    Public plcMPI As Integer = 2
    Public Connection As Boolean = False
    Public IP As String = “10.0.100.108”
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Public Sub connectPLC()
    fds.rfd = libnodave.openSocket(102, IP)
    fds.wfd = fds.rfd

    If fds.rfd > 0 Then ‘ if step 1 is ok
    di = New libnodave.daveInterface(fds, “IF1”,
    0, libnodave.daveProtoISOTCP,
    libnodave.daveSpeed187k)

    di.setTimeout(1000000)
    res = di.initAdapter
    If res = 0 Then ‘ init Adapter is ok
    ‘ rack amd slot don’t matter in case of MPI
    dc = New libnodave.daveConnection(di, 0,
    rack, slot)
    res = dc.connectPLC()
    If res = 0 Then
    Connection = True
    MessageBox.Show(“Connected ” + IP, “Connected”)
    End If
    End If
    End If
    End Sub
    Public Sub disconnectPLC()
    dc.disconnectPLC()
    MessageBox.Show(“DisConnected ” + IP, “DisConnected”)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim Adr As Integer
    Dim Par As Boolean = True

    Dim InputNum As Integer = TextBox1.Text
    Dim BitNum As Integer = TextBox2.Text

    Adr = InputNum * 8 + BitNum

    If ComboBox1.SelectedText = “Input” Then
    TextBox3.Text = dc.readBits(libnodave.daveInputs, 0, Adr, 1, buf)
    End If
    If ComboBox1.SelectedText = “Output” Then
    TextBox3.Text = dc.readBits(libnodave.daveOutputs, 0, Adr, 1, buf)
    End If
    If ComboBox1.SelectedText = “Memory” Then
    TextBox3.Text = dc.readBits(libnodave.daveFlags, 0, Adr, 1, buf)
    End If
    ‘write Input I1.3

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Dim Adr As Integer
    Dim Par As Boolean = True

    Dim InputNum As Integer = 1 ‘input I20.3
    Dim BitNum As Integer = 3

    If TextBox3.Text = “1” Then
    buf(0) = 255 ‘write 1
    Else
    buf(0) = 0 ‘write 0
    End If
    If Connection Then
    Adr = InputNum * 8 + BitNum

    If ComboBox1.SelectedText = “Input” Then
    res = dc.writeBits(libnodave.daveInputs, 0, Adr, 1, buf)
    End If
    If ComboBox1.SelectedText = “Output” Then
    res = dc.writeBits(libnodave.daveOutputs, 0, Adr, 1, buf)
    End If
    If ComboBox1.SelectedText = “Memory” Then
    res = dc.writeBits(libnodave.daveFlags, 0, Adr, 1, buf)

    End If
    Else
    MsgBox(“No connection with PLC!”)
    End If

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    Dim addr As Integer = Int16.Parse(TextBox4.Text)
    Dim bytelen As Integer = Integer.Parse(TextBox5.Text)
    TextBox6.Text = dc.readBytes(libnodave.daveFlags, 0, addr, bytelen, buf)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim addr As Integer = Int16.Parse(TextBox4.Text)
    Dim bytelen As Integer = Integer.Parse(TextBox5.Text)
    Dim x3 As Int16 = Int16.Parse(TextBox6.Text)

    buf = BitConverter.GetBytes(libnodave.daveSwapIed_16(x3))
    res = dc.writeBytes(libnodave.daveFlags, 0, addr, bytelen, buf)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    disconnectPLC()

    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    connectPLC()

    End Sub
    End Class

  40. site Says:

    whoah this weblog is great i like studying your articles. Stay up the great work!
    You know, many people are hunting round for this information, you can help them
    greatly.

  41. Tamas Says:

    Hy Alex,
    How can I update the PLC time with my system time?

  42. Yurii Says:

    Hi Alex!
    I have problem with connection to controller S7-200. I download your source and try write myself code in C#. I’m use MVS Prof 2012 and when I try connect to controllet I got error. I check that in project libnodave.net.dll lib is add to references. I change only string IP data on PLC IP adress. Error message screen I’m sharing down:
    http://yadi.sk/d/PvkXf9uMLAUR9
    http://yadi.sk/d/XUIXDnGHLAULU
    If you can can we continue conversation at email. My email (scotty1988@yandex.ru). Thanks in advance!

    Regards.

  43. Avinash Says:

    Dear All,
    I tried the code in both c# and VB but i am unable to connect my plc, I am using s7-1200 cpu and i am not using my local lan port instead i am using one USB-TO-LAN Converter, kindly help me on this.

  44. Avinash Says:

    Dear All,
    I am using USB-TO-LAN Converter but doubt is which port address i need to use.

  45. Avinash Says:

    Can any one help the communication

  46. George Says:

    Good morning,

    I would like some help to connect with MPI on my s7-300.I read above and placed the code but i’m doing something wrong.please could you help?
    Here is the code :
    fds.rfd = libnodave.setPort(Port, useBaud, AscW(“O”))
    fds.wfd = fds.rfd
    If fds.rfd > 0 Then
    di = New libnodave.daveInterface(fds, “IF1″, localMPI, 0, 2)
    di.setTimeout(1000000)
    res = di.initAdapter

    If res = 0 Then
    dc = New libnodave.daveConnection(di, plcMPI, 0, 0)
    res = dc.connectPLC()
    If res = 0 Then
    Connection = True
    ToolStripStatusLabel1.Text = “Connected ”
    Timer1.Enabled = True

  47. KANG NAM WOOK Says:

    Hello!

    I have personal question, I wonder if we can use this program to connect for PLC which is SIEMENS-S7-300, SIEMENS-S7-400,
    MELSEC-A273UH

    because I just check the possibility for use this library for my new project

    Thanks a lot!!!!

  48. Devendra Singh Says:

    Hello,
    we are .NET programmer in the region of automation. we want to connect siemens s7 200 smart plc using libnodave.net.dll .
    when we run the program the port is open but connection to plc is getting error. -1

    Please Help us.

    If you have any program in vb.net, C# and vb 6.0 for siemens s7 200 getting data inputs and output please share it…………..

    Devendra

  49. Manoj Says:

    HI,

    we have try to connect Plc model siemens S7 200 smart by com port but com is connected and PLC not connect with computer.

    fds.rfd = libnodave.setPort(“COM3″, “9600″, 8) ‘ (102, IP)

    fds.wfd = fds.rfd
    If fds.rfd > 0 Then ‘ if step 1 is ok
    di = New libnodave.daveInterface(fds, “IF1″, 0, libnodave.daveProtoISOTCP, libnodave.daveSpeed187k)
    di.setTimeout(1000000) ‘ Make this longer if you have a very long response time
    res = di.initAdapter

    If res = 0 Then ‘ init Adapter is ok
    dc = New libnodave.daveConnection(di, 0, rack, slot) ‘ rack amd slot don’t matter in case of MPI
    res = dc.connectPLC()
    If res = 0 Then
    Connection = True
    ToolStripStatusLabel1.Text = “Connected ” + IP
    End If
    End If
    End If

    res is display -1 values after res = dc.connectPLC()

    please help us

    Thanking you

  50. Benoit LAMBERT Says:

    Hi all,

    I try to connect to a s7-1212C PLC.
    I configur my IP adress on plc with TIA portal v11. 64x

    I put the same adress in VB.net project but when i compil impossible to dial with plc.

    Can you say/ explain/share with me how you do to connect the plc?

    thx all.

  51. Thongchai Bu Says:

    Hi Alex
    Can exchange by IBH net or not?

  52. minh duc Says:

    i want to read or wirte value analog from PC to PLC. Can you help me? i tried read PQW 0:
    res = dc.readBytes(libnodave.daveAnaOut, 0, 0, 4, buf)
    MsgBox(Str(dc.getU16))
    but error

  53. Sedat Atalay Says:

    Hello. I need to connect to S7-1200 through it’s Profinet interface. Can you please sens me an example project for MS visual studio?

    Thanks and regards.
    Sedat

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: