VB to PLC - Control Questions-VBForums
Results 1 to 6 of 6

Thread: VB to PLC - Control Questions

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    9

    VB to PLC - Control Questions

    Hello Forum,

    I have a few questions regarding VB and PLC's. Is it possible to control a PLC using a VB program? I would only need to have a STOP and CONTINUE function sent to the PLC, nothing more than that as far as controlling the PLC.

    I would be using Microsoft Visual Studio 2010 Professional and I would be attempting to control a Siemens S7 300 PLC with either the 312, 314, or 315 processor. I am not sure which processor is in it right now. I would be able to communicate either through ethernet or serial port (RS232) connection.

    Also, is it possible to record data from the Siemens S7 300 PLC using a VB program to either a CSV file or a Excel 2010/2007 file?

    Thanks for you help and for your time!!

  2. #2
    Addicted Member Darren M.'s Avatar
    Join Date
    Nov 2005
    Location
    D/FW
    Posts
    200

    Re: VB to PLC - Control Questions

    Brian, yes, it is possible, depending upon the PLC itself. Spend a little time on Google and you'll be amazed at what you can find....
    http://www.plcdev.com/book/export/html/373

    Good luck and when you're finished, post the code here so the next person asking for these can find your solution. (You were hoping for the same, right?)

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    9

    Re: VB to PLC - Control Questions

    Thanks Darren! Haha, Yes I sure was

  4. #4
    Addicted Member Darren M.'s Avatar
    Join Date
    Nov 2005
    Location
    D/FW
    Posts
    200

    Re: VB to PLC - Control Questions

    I've worked w/ PLC's and they can be a struggle. However, once you have the basic communications down they are actually pretty fun. Software so rarely changes the physical world. With PLC's you finally get the chance to "touch" something with your code. Frankly, I wish I did more with PLC's because of that reason. Just my 2¢.

    So, in the spirit of learning and helping others, please continue to post here. I also enjoy helping others but do believe in a "help us help you" mentality. Post your code, specs, and anything else you can to help us "learn" the particulars of the PLC and I'd be willing to bet you get a VERY good response. Just don't expect the community to "do it for you". Just remember, you get out what you put in.

    Good luck, looking forward to your progress.

    On the edit: Please note that I am not suggesting you post anything proprietary or that could otherwise jeopardize any kind of NDA, employment, etc.
    Last edited by Darren M.; Jan 5th, 2012 at 10:34 AM. Reason: Legal BS

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2011
    Posts
    9

    Re: VB to PLC - Control Questions

    I sure will! As soon as I am able to figure something out or have more details about the problem, I will post it here. Hopefully I will have made some progess on this by the end of the week as long as other projects don't get in the way too much. Thanks again for your help!

  6. #6
    New Member
    Join Date
    Nov 2012
    Posts
    1

    Re: VB to PLC - Control Questions

    Below is the code I have so far, when I run the app I can see in RSLinx OPC Group that "PLC5K-VB-HMI" is active. So I am connected to Linx OPC.

    What I don't know what to do next, how do I get a button click to turn on a tag (BOOL) in the Control Logix 5000 Tag Data Base to turn on?

    I am a beginner at this, but I learn quickly from example code. I am a PLC programmer trying to learn VB and hopefully C++ later on.

    I am trying to create a VB HMI.

    Imports OPCAutomation.OPCServerState
    Imports RSLogix5000ProjectFiles
    Imports RSLogix5000FTLSLib
    Public Class Form1
    Dim MyServer As New OPCAutomation.OPCServer
    Dim WithEvents PLC5KGroup As OPCAutomation.OPCGroup

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    IntializeOPC()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _
    Handles Me.FormClosing
    DisconnectOPCServer()
    End Sub

    Sub IntializeOPC()
    Dim OPCServers As New Hashtable
    Dim vAllOPCServers As Object
    Dim index As Int32

    'Create a list of all OPC servers available on machine
    Try
    vAllOPCServers = MyServer.GetOPCServers("localhost")
    For index = LBound(vAllOPCServers) To UBound(vAllOPCServers)
    OPCServers.Add(vAllOPCServers(index).ToString, index)
    Debug.Print(vAllOPCServers(index).ToString)
    Next

    Catch ex As Exception
    'If no OPC servers, then shutdown
    MsgBox("No OPC Servers available on machine!" & vbCrLf & _
    "Start OPC Server and re-start HDPrintServer.", _
    MsgBoxStyle.Exclamation)
    Me.Close()
    End Try

    'Our target is RSLinx, so if it is not in the list, shutdown
    If OPCServers("RSLinx OPC Server") Then
    Try
    MyServer.Connect("RSLinx OPC Server")
    Catch ex As Exception
    MsgBox("Error in MyServer.Connect!" & vbCrLf & "Application will shutdown.", _
    MsgBoxStyle.Exclamation)
    Me.Close()
    End Try
    Else
    MsgBox("RSLinx not available on machine!" & vbCrLf & "Application will shutdown.", _
    MsgBoxStyle.Exclamation)
    Me.Close()
    End If

    'If the connection is successful, then we can add items to the server
    CreatePLC5KGroup()
    End Sub
    Sub CreatePLC5KGroup()
    Dim arItemIDs() As String
    Dim arClientHandles As Int32()
    Dim arServerHandles As Object = Nothing
    Dim arErrors As Object = Nothing
    ReDim arItemIDs(1)
    arItemIDs(1) = "MyServer" 'Sample item.

    'Create a unique handle for each of the ItemIDs
    ReDim arClientHandles(1)
    arClientHandles(1) = 1
    PLC5KGroup = MyServer.OPCGroups.Add("PLC5K-VB-HMI")
    PLC5KGroup.UpdateRate = 250 'How often you want the item values checked in milliseconds
    PLC5KGroup.DeadBand = 0
    Try
    PLC5KGroup.OPCItems.AddItems(1, arItemIDs, arClientHandles, arServerHandles, arErrors)
    Catch ex As Exception

    End Try

    Dim i As Int16
    If Not arErrors Is Nothing Then
    For i = LBound(arErrors) To LBound(arErrors)
    If arErrors(i) <> 0 Then
    Debug.Print(MyServer.GetErrorString(arErrors(i)))
    End If
    Next
    End If
    'If not suscribed, the DataChange event will never fire. Can be toggled if you want to stop updates.
    PLC5KGroup.IsSubscribed = True
    End Sub

    Sub DisconnectOPCServer()
    Try
    If MyServer.ServerState = OPCRunning Then
    MyServer.Disconnect()
    End If
    Catch ex As Exception
    MsgBox("Exception thrown in MyServer.Disconnet. Message is " & ex.Message & vbCrLf & _
    "Application will shutdown.", _
    MsgBoxStyle.Exclamation)
    Me.Close()
    End Try
    End Sub

    Private Sub PLC5KGroup_DataChange(ByVal TransactionID As Integer, _
    ByVal NumItems As Integer, ByRef ClientHandles As System.Array, ByRef ItemValues As System.Array, _
    ByRef Qualities As System.Array, ByRef TimeStamps As System.Array) Handles PLC5KGroup.DataChange
    Dim item As Integer
    For item = 1 To NumItems
    Select Case ClientHandles(item)
    Case Is = 1
    If Qualities(item) <> 0 Then 'Non zero indicates good read.
    TextBox1.Text = ItemValues(item).ToString
    Else
    TextBox1.Text = "Unknown"
    End If
    End Select
    Next item

    End Sub

    End Class

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.