Tag Archives: plc examples with ladder logic

PLC Programming Example – Pick and Place

We will apply the five steps to PLC program development to a pick and place robot example. The example will use a BRX PLC communicating to Factory IO (3D Software Simulator). Developing the PLC program is a process that can be clearly defined. In our series on the five steps to PLC program development we have done some similar practical examples.
Five Steps to PLC Program Development – Press
PLC Programming Examples:
Process Mixer
Shift Register (Conveyor Reject)
Paint Spraying
Delay Starting of 7 Motors

Define the task: (1)
Watch the sequence of operation video below.
Watch on YouTube : PLC Programming Example – Pick and Place Testing
Continue Reading!

PLC Programming Example – Delay Starting of 7 Motors

We will look at a PLC programming example of delaying the start of 7 motors. Each motor will be on a switch that the operator can select at any time. The motor outputs should have a 5 second delay between the outputs coming on.

This question originally came from PLCTalk.net. An original solution to the problem came from Peter Steinhoff. His solution is what we will be presenting. It is simple and straight forward.

We will be using the Do-more Designer software which comes with a simulator. This fully functional program is offered free of charge at automation directKeep on Reading!

What Everybody Ought to Know About PLC Programming Languages

PLC programs are normally written in a special application on a personal computer, then downloaded to the PLC. This downloaded program is similar to compiled code to keep the program efficient. The program is stored in the PLC either in battery-backed-up RAM or some other non-volatile flash memory.

Albert Einstein said “The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking” PLC programming languages have evolved to both adapt and change the way we program these units. We will look at all five programming languages as defined by the IEC 61131-3 Standard.

  • Structured Text (ST)
  • Function Block Diagram (FBD)
  • Sequential Function Chart (SFC)
  • Instruction List (IL)
  • Ladder Diagram (LD)

Not all of these programmable controller languages are available in every PLC. Ladder logic programming is by far the largest percentage of use in PLC’s today. Fundamental concepts of PLC programming are common to all manufacturers. Differences in I/O addressing, memory organization, and instruction sets mean that PLC programs are never interchangeable between different makers. Even within the same product line of a single manufacturer, different models may not be directly compatible. This is true when looking at manufactures that private label other controllers.

Estimates are as high as 95% of installations use ladder logic programming in the programmable logic controller.

The PLC programming language that is used can be decided when you look at the following:

  • Maintenance and troubleshooting
  • Knowledge of language
  • Acceptance of the country, location, or individual plant
  • Application of the PLC
  • Ease of changing PLC program

The actual programming of the PLC is the second last step in the development of programs. The five steps to PLC program development is a good method to follow before picking what programming language to use. As mentioned before the languages supported by each PLC may differ. Please refer to the types of programming that are available for your model and version of PLC.

Let’s quickly review some of the different programming languages for the PLC.

Structured Text (ST) is a high level programming language that closely resembles Pascale programming. Statements are used to define what to execute.
ST MP50pro_st

Function Block Diagram (FBD) is a graphical representation of AND, NAND, OR, NOR gates, etc. that are drawn. It will describe the function between input and output variables.
FBD MP50pro_fbd

Sequential Function Chart (SFC) is like a flowchart of your program. It defines the steps through which your program moves.
SFC MP50pro_sfc

Instruction List (IL) can also be referred to as mnemonic code and statement list. It contains simple instructions for looking at your variables.
IL MP50pro_il

Ladder Diagram (LD) is the most popular programming language for the PLC. It was written to mimic the mechanical relays in the panel that the programmable logic controller replaced. It has two vertical rails and a series of horizontal rungs between them. Controllers will usually scan from left to right top to bottom. The output of one rung is available for the next rung.
LD MP50pro_ladder

Note: All pictures from PLCopen IEC 61131 Basics

PLC programming methods are evolving. PLC Open is an organization that is defining new methods to take advantage of the latest computer innovations. They have defined the IL method of programming to XML (Extended Markup Language) which is used for web development. This in my opinion keeps moving the ideal method, to a standard way to program PLCs.

If you have any questions or need further information please contact me.
Thank you,
Garry



If you’re like most of my readers, you’re committed to learning about technology. Numbering systems used in PLC’s are not difficult to learn and understand. We will walk through the numbering systems used in PLCs. This includes Bits, Decimal, Hexadecimal, ASCII and Floating Point.

To get this free article, subscribe to my free email newsletter.


Use the information to inform other people how numbering systems work. Sign up now.

The ‘Robust Data Logging for Free’ eBook is also available as a free download. The link is included when you subscribe to ACC Automation.

PLC Programming Example – Paint Spraying

We will look at a PLC basic tutorial of a paint spraying station. Following the 5 steps to program development this PLC programming example should fully explain the procedure for developing the PLC program logic. Ladder will be our PLC programming language.

We will be using the Do-more Designer software which comes with a simulator. This fully functional program is offered free of charge at automation direct.

Define the task:

What has to happen?
Paint Station 01

Paint spraying system where boxes are fed by gravity through a feeder magazine one at a time onto a moving conveyor belt. Upon the start signal, boxes are pushed towards the conveyor by valve 1. This is a cylinder which extends and retracts which operates switches S1 and S2 respectfully. A spraying nozzle paints each box as it passes under the paint spray controlled by valve 2. A sensor (S3) counts each box being sprayed. When 6 boxes have been painted the valve 2 shuts off (paint spray) and valve 1 (cylinder) stops moving boxes onto the conveyor. Three seconds later the conveyor stops moving and the hopper with its load moves forward (valve 3) where it is emptied. Ten seconds later the hopper returns to the original position. The cycle is then complete and waits for a start signal again.

Define the Inputs and Outputs:

Inputs:
Start Switch – On/Off (Normally Open) – NO
Stop Switch – On/Off (Normally Closed) – NC
S1 – Valve 1 (cylinder retract) On/Off – NO
S2 – Valve 1 (cylinder extend) On/Off – NO
S3 – Box Detected- On/Off – NO
Outputs:
Motor – On/Off (Conveyor Run)
Valve 1- Cylinder to feed boxes – On/Off
Valve 2- Paint Spray – On/Off
Valve 3- Cylinder to move hopper – On/Off

Develop a logical sequence of operation:

Fully understanding the logic before starting to program can save you time and frustration.

Sequence Table: The following is a sequence table for our paint spraying application.

Sequence Table
1 – Input / Ouput ON
0 – Input / Output OFF
x – Input / Output Does not Matter
When power goes off and comes on the sequence will continue. This means that we must use memory retentive areas of the PLC. The stop pushbutton will stop the sequence. The start will resume until the end.

Develop the PLC program:

The best way to see the development of the programmable logic controller program is to follow the sequence table along with the following program. You will see the direct correlation between the two and get a good understanding of the process.

This is the main process start and stop bit. V0:0 is used because it is memory retentive.
Paint Stn Program 1

Control of the Motor (Conveyor) and the paint spray is done with the V0:0 contact in front of the actual PLC output. The conveyor and paint spray will stop when the timer 0 is done. This is the delay after the last box is detected to allow the box to be painted and loaded onto the hopper.
Paint Stn Program 2

Control of the box movement onto the conveyor. As long as we have the process start and the hopper count is not complete this will allow the cylinder to put boxes on the conveyor.
Paint Stn Program 3

Count number of boxes in the hopper via S3. The counter is memory retentive.
Paint Stn Program 4

Timer to stop the conveyor and spray after the last box is detected for the hopper. This will allow time for the box to be sprayed and loaded into the hopper.
Paint Stn Program 5

Hopper movement to load and unload the boxes.
Paint Stn Program 6

The hopper unload timer is to unload the boxes and will then trigger the reset conveyor timer, box counter and the process start bit (V0:0).
Paint Stn Program 7

Test the program:

Paint Spraying
Test the program with a simulator or actual machine. Make modifications as necessary. Remember to follow up after a time frame to see if any problems arise that need to be addressed with the program.

Watch on YouTube : PLC Programming Example – Paint Spraying
If you have any questions or need further information please contact me.
Thank you,
Garry



If you’re like most of my readers, you’re committed to learning about technology. Numbering systems used in PLC’s are not difficult to learn and understand. We will walk through the numbering systems used in PLCs. This includes Bits, Decimal, Hexadecimal, ASCII and Floating Point.

To get this free article, subscribe to my free email newsletter.


Use the information to inform other people how numbering systems work. Sign up now.

The ‘Robust Data Logging for Free’ eBook is also available as a free download. The link is included when you subscribe to ACC Automation.

Five Steps to PLC Program Development

Programming a PLC can sometimes be a daunting task. The best method is to break the task into some smaller steps. These are the steps that I have used for years. We will apply them to a die stamping application.

1 – Define the task:

What has to happen?
Die Stamping

A master switch is used to start the process and to shut it down. Two sensors: an upper limit switch that indicates when the piston is fully retracted and a lower limit switch that indicates when the piston is fully extended. When the master switch is turned on the piston reciprocates between the extended and retracted positions. This is achieved with an up and down solenoid. When the master switch is turned off, the piston returns to the retracted position and all solenoids are off.

2 – Define the Inputs and Outputs:

Inputs:
Master Switch – On/Off
Upper Limit Switch – On/Off
Lower Limit Switch – On/Off

Outputs:
Down Solenoid – On/Off
Up Solenoid – On/Off

3 – Develop a logical sequence of operation:

This can be done with the use of a flow chart or sequence table. You can use anything to fully understand the logic of the operation before programming. Many people do not use this step and jump straight to programming.

Fully understanding the logic before starting to program can save you time and frustration.

Sequence Table: The following is a sequence table for our die stamping application. I usually review this sequence with the person with the most knowledge of the machine. This can be the designer and / or the machine operator.
Sequence Table

How to read the Sequence Table: Follow the steps from left to right, top to bottom. Inputs and outputs are labelled as 1 (ON), 0 (OFF) or X (Does not Matter). Step 1 indicates that it does not matter the upper and lower limit switch positions. The master switch is off, so the up and down solenoids are off. Steps 3 and 4 repeat themselves as long as the master switch is on.

Note: You will notice that at step 2, after the master switch turns on the up solenoid will be activated. So the piston always retracts when the master switch is first turned on.  This operation was picked up in development of our logical sequence.

4 – Develop the PLC program:

Look at the sequence table in respect to the following logic. I have used Set and Reset conditions so it is easily followed by the sequence table. When the master switch turns on the up solenoid is activated. Notice the first rung is a direct correlation. Follow the rest of the sequence table with this ladder logic.

PLC Program Die Stamping
Document, Document, Document This is a vital part of every program, which will save you time and money when you have to return to the program years later.

5 – Test the program:

Die_Stamping
Test the program with a simulator or actual machine. Make modifications as necessary. Check with the people most knowledgeable on the machine, to see if it is doing what they expect. Do they need something else? Follow up after a time frame to see if any problems arise that need to be addressed.

These five steps will help you in your PLC programming.

  1. Define the task
  2. Define the inputs and outputs
  3. Develop a logical sequence of operation
  4. Develop the PLC program
  5. Test the program

The five steps form the basis of all PLC development. You will notice that the actual programming does not occur until the second last step. Usually more time is spent on understanding the task and sequence of operation.

Watch on YouTube : Five Steps to PLC Program Development
If you have any questions or need further information please contact me.
Thank you,
Garry



If you’re like most of my readers, you’re committed to learning about technology. Numbering systems used in PLC’s are not difficult to learn and understand. We will walk through the numbering systems used in PLCs. This includes Bits, Decimal, Hexadecimal, ASCII and Floating Point.

To get this free article, subscribe to my free email newsletter.


Use the information to inform other people how numbering systems work. Sign up now.

The ‘Robust Data Logging for Free’ eBook is also available as a free download. The link is included when you subscribe to ACC Automation.

Now You Can Have Robust Data Logging for Free – Part 12

Now You Can Have Robust Data Logging for Free – Part 12

HTML and Scripting Languages

 We have the following accomplished:
  • PLC program
  • Visual Basic Program
  • Data collected in a Database
  • IIS web service established
  • ASP Script Written
iis106
Lets take a closer look at the ASP Script ( AccRL.asp) that was written in part 11:
The <html> is at the start of the file and the </head> is at the end of the file. These tags all have to have a start and end.  The ‘/’ indicates the end of the tag.
The <head> is used to place the information for the web page. The refresh will load the page after 300 seconds (5 minutes). This way the information will always be the latest. Title is used to label the page. This is the information at the top of the browser. The SHORTCUT ICON is used for the icon at the top of the browser near the page address.
<html>

<head>
<meta HTTP-EQUIV=”Refresh” CONTENT=”300″>
<title>ACC Automation – Robust Logger</title>
<LINK REL=”SHORTCUT ICON” HREF=”http://192.168.1.3/ACC_Do.ico”/></head>

ActiveX Data Objects (ADO) is used to access databases from your web pages. ADOVBS.inc is a file that has all of the ADO constants defined.  Be sure to add this file in your root web application directory.
<!– #include virtual=”/adovbs.inc” –>

The <% and %> symbols indicate the start and finish of VBScript in the page. We dimension our variables for StartTime and EndTime. These will be used to determine how long our script took to execute.
<%
Dim StartTime, EndTime
StartTime = Timer

We dimension the variables that are used for the connection to the database file.
Dim OBJdbConnection
Dim rs1
Dim objCmd

We set up the connection to the database and determine what information we need to retrieve.
Set OBJdbConnection = Server.CreateObject(“ADODB.Connection”)

OBJdbConnection.Open “Provider=Microsoft.ACE.OLEDB.12.0;DATA SOURCE=C:\AccRL\data\AccRL.accdb;Persist Security Info=False;”
set rs1 = Server.CreateObject(“ADODB.recordset”)
with rs1
 .CursorType = adOpenForwardOnly
 .LockType = adLockReadOnly
 .CursorLocation = adUseServer
 .ActiveConnection = OBJdbConnection
 .Source = “SELECT * FROM Minute_Log;”
end with

Using getrows will allow us to execute the Select command and retrieve all of the information in one pass from the database.  This is the quickest method to get the information out quickly.
rs1.Open

arraytime = rs1.getrows()
rs1.close

We now write the information from the database to the page.
Response.Write arraytime(0,0) & “<br>”
Response.Write arraytime(1,0) & “<br>”
Response.Write Year(arraytime(1,0))& “/” & Right(“0” & Month(arraytime(1,0)), 2) & “/” & Right(“0” & Day(arraytime(1,0)), 2) & “<br>”
Response.Write arraytime(2,0)& “<br>”
Response.Write arraytime(3,0)& “<br>”
Response.Write arraytime(4,0)& “<br>”

The EndTime is now set and the total time it took for the process is displayed.
EndTime = Timer

Response.write “<p>Processing took “&(EndTime-StartTime)&” seconds<p>&nbsp;”
%>
</body>
</html>

Now that you have information into the database and IIS running, you can display the data in various ways.
Charts:
iis109 display
Graphs:
iis108 display Gauges:
iis107 display
This ends our robust logger design. For the complete PLC program, VB source code and web page file please send me an email and ask for the ACC Robust Logger Program. I will be happy to email you the information.
If you have any questions or need further information, please contact me.
Regards,
Garry

Now You Can Have Robust Data Logging for Free – Part 1
Now You Can Have Robust Data Logging for Free – Part 2
Now You Can Have Robust Data Logging for Free – Part 3
Now You Can Have Robust Data Logging for Free – Part 4
Now You Can Have Robust Data Logging for Free – Part 5
Now You Can Have Robust Data Logging for Free – Part 6
Now You Can Have Robust Data Logging for Free – Part 7
Now You Can Have Robust Data Logging for Free – Part 8
Now You Can Have Robust Data Logging for Free – Part 9
Now You Can Have Robust Data Logging for Free – Part 10
Now You Can Have Robust Data Logging for Free – Part 11
Now You Can Have Robust Data Logging for Free – Part 12




If you’re like most of my readers, you’re committed to learning about technology. Numbering systems used in PLC’s are not difficult to learn and understand. We will walk through the numbering systems used in PLCs. This includes Bits, Decimal, Hexadecimal, ASCII and Floating Point.

To get this free article, subscribe to my free email newsletter.


Use the information to inform other people how numbering systems work. Sign up now.

The ‘Robust Data Logging for Free’ eBook is also available as a free download. The link is included when you subscribe to ACC Automation.

Now You Can Have Robust Data Logging for Free – Part 11

Now You Can Have Robust Data Logging for Free – Part 11

HTML and Scripting Languages

 We have the following accomplished:
  • PLC program
  • Visual Basic Program
  • Data collected in a Database
  • IIS web service established
The machine that has the IIS web service must have the Microsoft Access Database Engine 2010 installed. This can be obtained by the following link:
You can select the 32 bit or 64 bit version that matches your computer.

Microsoft Access Database Engine 2010 Redistributable
Note:  If you have office installed on your machine already then you probably will already have this file.

ActiveX Data Objects (ADO) is used to access databases from your web pages. ADOVBS.inc is a file that has all of the ADO constants defined.  Be sure to add this file in your root web application directory. How to add this code to a web page is shown in the sample code below .
You can download ADOVBS.inc from this site in text format. (Just rename to ADOVBS.inc from ADOVBS.txt)
ADO Introduction:
http://www.w3schools.com/asp/ado_intro.asp

Lets set up ASP on IIS to display any error messages to our browser.
Call up Control Panel and then goto Administrative Tools. Call up Internet Information Services (IIS) Manger.iis100

From IIS Manager, double click on ASP under IIS. Expand Debugging Properties and change the Send Errors To Browser to True.

iis102

iis103

Lets also ensure that your browser is set to display the error messages in internet explorer (IE). Call up Internet options from the main settings.

iis104

iis105

Click the setting for ‘Show friendly HTTP error messages’. This will ensure that the error messages show up in your browser.

The last part  of our project is to display the database information to the network. We do this by using a webpage. The HTML and VBScript can be writing in any editor. (Like Notepad)

There are also a great number of online editors that you can visual see what your page will look like while developing your code.
To learn more about VBScript following the link below:
Lets take a look at the AccRL.asp file:


<html>
<head>
<meta HTTP-EQUIV=”Refresh” CONTENT=”300″>
<title>ACC Automation – Robust Logger</title>
<LINK REL=”SHORTCUT ICON” HREF=”http://192.168.1.3/ACC_Do.ico”/></head>
<!– #include virtual=”/adovbs.inc” –>
<%
Dim StartTime, EndTime
StartTime = Timer

Dim OBJdbConnection
Dim rs1
Dim objCmd

Set OBJdbConnection = Server.CreateObject(“ADODB.Connection”)
OBJdbConnection.Open “Provider=Microsoft.ACE.OLEDB.12.0;DATA SOURCE=C:\AccRL\data\AccRL.accdb;Persist Security Info=False;”
set rs1 = Server.CreateObject(“ADODB.recordset”)
with rs1
 .CursorType = adOpenForwardOnly
 .LockType = adLockReadOnly
 .CursorLocation = adUseServer
 .ActiveConnection = OBJdbConnection
 .Source = “SELECT * FROM Minute_Log;”
end with

rs1.Open
arraytime = rs1.getrows()
rs1.close

Response.Write arraytime(0,0) & “<br>”
Response.Write arraytime(1,0) & “<br>”
Response.Write Year(arraytime(1,0))& “/” & Right(“0” & Month(arraytime(1,0)), 2) & “/” & Right(“0” & Day(arraytime(1,0)), 2) & “<br>”
Response.Write arraytime(2,0)& “<br>”
Response.Write arraytime(3,0)& “<br>”
Response.Write arraytime(4,0)& “<br>”

EndTime = Timer
Response.write “<p>Processing took “&(EndTime-StartTime)&” seconds<p>&nbsp;”
%>
</body>
</html>

Place this AccRL.asp file into the root directory of our web server. Call up the page though our browser (http:\\localhost\AccRL.asp) and the following output will be seen.
iis106
In part 12 we will break down the ASP code and modify. For the complete PLC program, VB source code and web page file please send me an email and ask for the ACC Robust Logger Program. I will be happy to email you the information.
If you have any questions or need further information, please contact me.
Regards,
Garry

Now You Can Have Robust Data Logging for Free – Part 1
Now You Can Have Robust Data Logging for Free – Part 2
Now You Can Have Robust Data Logging for Free – Part 3
Now You Can Have Robust Data Logging for Free – Part 4
Now You Can Have Robust Data Logging for Free – Part 5
Now You Can Have Robust Data Logging for Free – Part 6
Now You Can Have Robust Data Logging for Free – Part 7
Now You Can Have Robust Data Logging for Free – Part 8
Now You Can Have Robust Data Logging for Free – Part 9
Now You Can Have Robust Data Logging for Free – Part 10
Now You Can Have Robust Data Logging for Free – Part 11




If you’re like most of my readers, you’re committed to learning about technology. Numbering systems used in PLC’s are not difficult to learn and understand. We will walk through the numbering systems used in PLCs. This includes Bits, Decimal, Hexadecimal, ASCII and Floating Point.

To get this free article, subscribe to my free email newsletter.


Use the information to inform other people how numbering systems work. Sign up now.

The ‘Robust Data Logging for Free’ eBook is also available as a free download. The link is included when you subscribe to ACC Automation.

Now You Can Have Robust Data Logging for Free – Part 10

Now You Can Have Robust Data Logging for Free – Part 10

Computer Web Server (IIS)

 We have come a long way. The PLC program has been written. The visual basic program has been written. Information is now being collected from the Do-more PLC via Modbus TCP and stored in a database using visual basic.
The next step is to deliver the information on the network. We will do this by installing the Internet Information Services. (IIS) This is a group of internet servers that include a Web or Hypertext Transfer Protocol server (HTTP) and a File Transfer Protocol server (FTP). IIS will allow us to connect the physical hardware to the data. This could be desktop computers, laptops, tablets, cell phones, watches etc. The advantage of using HTTP is that we can share the information to all of these devices without having to be concerned over the operating system of each of them. As long as they can display a web page we are good to go.
Active Service Pages (ASP) will be installed at the same time. This is a program that will run scrips at the server before delivering the HTML code to the browser. It is similar to CGI and Perl but is simpler and faster.
ASP.Net Tutorial
We will install this on a Windows 8.1 machine.
Call up the Control Panel.
  • Swiping in from the right and searching for “control panel”.
  • Win + x will call a menu to select the control panel.
IIS_000
Select Programs and Features
IIS_001
Select Turn Windows features on or off
IIS_002
Select ASP after expanding Internet Information Services / World Wide Web Services / Application Development Features. This will select all of the other options.
IIS_003
Hit OK to install the services.
IIS_004
IIS_005
We now have IIS installed.
IIS_006
Under the following default directory you will find the location to put your web pages.
C:\inetpub\wwwroot\
IIS_007
If you call up the iisstart.htm file in this directory it will call up a page from Microsoft to explain the IIS web service.
IIS_009
Installing IIS on windows 7 and XP is very similar to the above procedure. Windows 98 you had to install a personal web service (PWS) and then ASP separately.
Further information on ASP can be obtained from the following website:
This site will walk you through ASP.
In part 11 we will  look at HTML and scripting languages like JavaScript or VBScript.
If you have any questions or need further information, please contact me.
Regards,
Garry

Now You Can Have Robust Data Logging for Free – Part 1
Now You Can Have Robust Data Logging for Free – Part 2
Now You Can Have Robust Data Logging for Free – Part 3
Now You Can Have Robust Data Logging for Free – Part 4
Now You Can Have Robust Data Logging for Free – Part 5
Now You Can Have Robust Data Logging for Free – Part 6
Now You Can Have Robust Data Logging for Free – Part 7
Now You Can Have Robust Data Logging for Free – Part 8
Now You Can Have Robust Data Logging for Free – Part 9
Now You Can Have Robust Data Logging for Free – Part 10




If you’re like most of my readers, you’re committed to learning about technology. Numbering systems used in PLC’s are not difficult to learn and understand. We will walk through the numbering systems used in PLCs. This includes Bits, Decimal, Hexadecimal, ASCII and Floating Point.

To get this free article, subscribe to my free email newsletter.


Use the information to inform other people how numbering systems work. Sign up now.

The ‘Robust Data Logging for Free’ eBook is also available as a free download. The link is included when you subscribe to ACC Automation.

Now You Can Have Robust Data Logging for Free – Part 9

Now You Can Have Robust Data Logging for Free – Part 9

Computer Program Visual Basic (VB6) Continue

 MainScr

The final code will be completed for the program. If you want a link to download the complete code then go to the contact page and put Robust Logger Program in the description.

The actual code that will be seen will be in Italic. This way you can pick out the code from the commentary.

Private Sub Form_Load()
MbusStatus = 0
ProductionPointerNow = 10
MinutePointerNow = 660
End Sub
 
When our form loads, the MbusStatus will be set to 0. This will control all of the communications to the Do-More PLC. The ProductionPointerNow and the MinutePointerNow will be used to determine where we are in getting data.
Here are our declarations for the program.
Dim MbusQuery
Public MbusResponse As String
Dim MbusByteArray(255) As Byte
Public MbusStatus As Integer
Public ProductionPointer As Integer
Public ProductionPointerNow As Integer
Public MinutePointer As Integer
Public MinutePointerNow As Integer
Start Logging Button:
We will stop or ensure the timer between reads has been disabled.
We will then check the status of the winsock. If it is not opened and ready to send then change the background colour to indicate to the operator what is going on.
Note:  Winsock RemotePort = 502
Private Sub Command1_Click()
Timer1.Enabled = False ‘ Stop the interval between reads to the PLC
Dim StartTime
If Winsock1.State <> 7 Then
If (Winsock1.State <> sckClosed) Then
Winsock1.Close
End If
Winsock1.RemoteHost = Text1.Text ‘ Set IP Address
Winsock1.Connect
StartTime = Timer ‘ Use the timer to determine if a connection cannot be made
Do While ((Timer < StartTime + 2) And (Winsock1.State <> 7))
DoEvents
Loop
If (Winsock1.State = 7) Then
Text1.BackColor = &HFF00& ‘ Change background colour to green
Else
Text1.BackColor = &HFF ‘ Change background colour to red
Exit Sub
End If
End If
Check the status of the Winsock. If we are good to go then determine the MbusStatus (0 to 9) and either send a read or write string out to the PLC.
If (Winsock1.State = 7) Then
Select Case MbusStatus
Case 0, 2
‘Read all the Daily Production Values
MbusQuery = Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(6) + Chr(0) + Chr(3) + Chr((Val(ProductionPointerNow) \ 256)) + Chr(((Val(ProductionPointerNow) Mod 256) – 1)) + Chr(0) + Chr(20)
Case 3
‘Reset Daily Production Pointer to 30 (Write)
MbusQuery = Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(9) + Chr(1) + Chr(16) + Chr(0) + Chr(0) + Chr(0) + Chr(1) + Chr(2) + Chr(0) + Chr(30)
Winsock1.SendData MbusQuery
Case 4, 6
‘Read the Minute Log Pointer
MbusQuery = Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(6) + Chr(0) + Chr(3) + Chr(0) + Chr(1) + Chr(0) + Chr(1)
Case 5
‘Read all the Minute Log Values
MbusQuery = Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(6) + Chr(0) + Chr(3) + Chr((Val(MinutePointerNow) \ 256)) + Chr(((Val(MinutePointerNow) Mod 256) – 1)) + Chr(0) + Chr(20)
Case 7
‘Reset Minute Log Pointer to 670 (Write)
MbusQuery = Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(9) + Chr(1) + Chr(16) + Chr(0) + Chr(1) + Chr(0) + Chr(1) + Chr(2) + Chr(2) + Chr(158)
Winsock1.SendData MbusQuery
Case 8
‘Read the current Daily Production Values
MbusQuery = Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(6) + Chr(0) + Chr(3) + Chr(0) + Chr(9) + Chr(0) + Chr(20)
Case 9
‘Read the current Minute Log Values
MbusQuery = Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(6) + Chr(0) + Chr(3) + Chr(2) + Chr(147) + Chr(0) + Chr(10) End Select
Winsock1.SendData MbusQuery ‘Send out the Modbus Information
Timer2.Enabled = True ‘Set the timeout timer for communications
Else
MsgBox (“Device not connected via TCP/IP”)
Exit Sub
End If
End Sub
 Retrieving the information from Winsock
 We first get all of the bytes of data from winsock. Then we determine the MbusStatus and then handle the data appropriately.
Private Sub Winsock1_DataArrival(ByVal datalength As Long)
Timer2.Enabled = False ‘Stop communications timeout timer
‘Get all the inforamtion from the data arriving
Dim B As Byte
Dim j As Byte
returnInfo = “”
For i = 1 To datalength
Winsock1.GetData B
MbusByteArray(i) = B
returnInfo = returnInfo & B
Next
j = 0
On Error Resume Next
Select Case MbusStatus
Case 0
‘Read the Daily Production Pointer
ProductionPointer = Val(Str((MbusByteArray(10) * 256) + MbusByteArray(11)))
If ProductionPointer <> 30 Then
ProductionPointerNow = ProductionPointerNow + 20
MbusStatus = 1
Else
MbusStatus = 4
End If
Case 1
With Adodc1
.CommandType = adCmdTable
.RecordSource = “Production_Log”
.Refresh
.Recordset.AddNew
End With
‘Read all the Daily Production Values
Label18.Caption = Format$((Str((MbusByteArray(10) * 256) + MbusByteArray(11)) & “/” & Str((MbusByteArray(12) * 256) + MbusByteArray(13)) & “/” & Str((MbusByteArray(14) * 256) + MbusByteArray(15))), “yyyy/mm/dd”)
Label19.Caption = Val(Str((MbusByteArray(16) * 256) + MbusByteArray(17)) & Str((MbusByteArray(18) * 256) + MbusByteArray(19)))
Label20.Caption = Val(Str((MbusByteArray(20) * 256) + MbusByteArray(21)) & Str((MbusByteArray(22) * 256) + MbusByteArray(23)))
Label21.Caption = Val(Str((MbusByteArray(24) * 256) + MbusByteArray(25)) & Str((MbusByteArray(26) * 256) + MbusByteArray(27)))
Label22.Caption = Val(Str((MbusByteArray(28) * 256) + MbusByteArray(29)) & Str((MbusByteArray(30) * 256) + MbusByteArray(31)))
Label23.Caption = Val(Str((MbusByteArray(32) * 256) + MbusByteArray(33)) & Str((MbusByteArray(34) * 256) + MbusByteArray(35)))
Label24.Caption = Val(Str((MbusByteArray(36) * 256) + MbusByteArray(37))) / 10
Label25.Caption = Val(Str((MbusByteArray(38) * 256) + MbusByteArray(39))) / 10
Label26.Caption = Val(Str((MbusByteArray(40) * 256) + MbusByteArray(41))) / 10
Label27.Caption = Val(Str((MbusByteArray(42) * 256) + MbusByteArray(43))) / 10
Label28.Caption = Val(Str((MbusByteArray(44) * 256) + MbusByteArray(45))) / 10
With Adodc1
.Recordset.Update
.Recordset.MoveLast
.Refresh
End With ProductionPointerNow = ProductionPointerNow + 20
If ProductionPointer = ProductionPointerNow Then
MbusStatus = 2
End If
Case 2
‘Read the Daily Production Pointer
ProductionPointer = Val(Str((MbusByteArray(10) * 256) + MbusByteArray(11)))
If ProductionPointer = ProductionPointerNow Then
MbusStatus = 3
Else
ProductionPointerNow = ProductionPointerNow + 20
MbusStatus = 1
End If
Case 3
‘Reset Daily Production Pointer to 30 (Write)
If (MbusByteArray(8) = 16) And (MbusByteArray(12) = 1) Then
MbusStatus = 4
ProductionPointerNow = 10
Else
Text1.BackColor = &HFF
End If
With Adodc1
.CommandType = adCmdTable
.RecordSource = “Production_Log”
.Refresh
.Recordset.AddNew
End With
Case 4
‘Read the Minute Log Pointer
MinutePointer = Val(Str((MbusByteArray(10) * 256) + MbusByteArray(11)))
If MinutePointer <> 670 Then
MinutePointerNow = MinutePointerNow + 10
MbusStatus = 5
Else
MbusStatus = 8
End If
Case 5
With Adodc2
.CommandType = adCmdTable
.RecordSource = “Minute_Log”
.Refresh
.Recordset.AddNew
End With
‘Read all the Minute Values
Label29.Caption = Format$((Str((MbusByteArray(10) * 256) + MbusByteArray(11)) & “/” & Str((MbusByteArray(12) * 256) + MbusByteArray(13)) & “/” & Str((MbusByteArray(14) * 256) + MbusByteArray(15))), “yyyy/mm/dd”)
Label30.Caption = Format$((Str((MbusByteArray(16) * 256) + MbusByteArray(17)) & “:” & Str((MbusByteArray(18) * 256) + MbusByteArray(19)) & “:” & Str((MbusByteArray(20) * 256) + MbusByteArray(21))), “hh:nn:ss”)
Label31.Caption = Val(Str((MbusByteArray(22) * 256) + MbusByteArray(23)) & Str((MbusByteArray(24) * 256) + MbusByteArray(25)))
Label32.Caption = Val(Str((MbusByteArray(26) * 256) + MbusByteArray(27)))
With Adodc2
.Recordset.Update
.Recordset.MoveLast
.Refresh
End With MinutePointerNow = MinutePointerNow + 10
If MinutePointer <= MinutePointerNow Then
MbusStatus = 6
End If
Case 6
‘Read the Minute Log Pointer
MinutePointer = Val(Str((MbusByteArray(10) * 256) + MbusByteArray(11)))
If MinutePointer = MinutePointerNow Then
MbusStatus = 7
Else
MinutePointerNow = MinutePointerNow + 10
MbusStatus = 5
End If
Case 7
‘Reset Minute Log Pointer to 670 (Write)
If (MbusByteArray(8) = 16) And (MbusByteArray(12) = 1) Then
MbusStatus = 8
MinutePointerNow = 660
Else
Text1.BackColor = &HFF
End If
With Adodc2
.CommandType = adCmdTable
.RecordSource = “Minute_Log”
.Refresh
.Recordset.AddNew
.Refresh
End With
Case 8
‘Read the current Daily Production Values
Label18.Caption = Format$((Str((MbusByteArray(10) * 256) + MbusByteArray(11)) & “/” & Str((MbusByteArray(12) * 256) + MbusByteArray(13)) & “/” & Str((MbusByteArray(14) * 256) + MbusByteArray(15))), “yyyy/mm/dd”)
Label19.Caption = Val(Str((MbusByteArray(16) * 256) + MbusByteArray(17)) & Str((MbusByteArray(18) * 256) + MbusByteArray(19)))
Label20.Caption = Val(Str((MbusByteArray(20) * 256) + MbusByteArray(21)) & Str((MbusByteArray(22) * 256) + MbusByteArray(23)))
Label21.Caption = Val(Str((MbusByteArray(24) * 256) + MbusByteArray(25)) & Str((MbusByteArray(26) * 256) + MbusByteArray(27)))
Label22.Caption = Val(Str((MbusByteArray(28) * 256) + MbusByteArray(29)) & Str((MbusByteArray(30) * 256) + MbusByteArray(31)))
Label23.Caption = Val(Str((MbusByteArray(32) * 256) + MbusByteArray(33)) & Str((MbusByteArray(34) * 256) + MbusByteArray(35)))
Label24.Caption = Val(Str((MbusByteArray(36) * 256) + MbusByteArray(37))) / 10
Label25.Caption = Val(Str((MbusByteArray(38) * 256) + MbusByteArray(39))) / 10
Label26.Caption = Val(Str((MbusByteArray(40) * 256) + MbusByteArray(41))) / 10
Label27.Caption = Val(Str((MbusByteArray(42) * 256) + MbusByteArray(43))) / 10
Label28.Caption = Val(Str((MbusByteArray(44) * 256) + MbusByteArray(45))) / 10
With Adodc1
.Recordset.Update
.Recordset.MoveLast
End With MbusStatus = 9
Case 9
‘Read the current Minute Log Values
Label29.Caption = Format$((Str((MbusByteArray(10) * 256) + MbusByteArray(11)) & “/” & Str((MbusByteArray(12) * 256) + MbusByteArray(13)) & “/” & Str((MbusByteArray(14) * 256) + MbusByteArray(15))), “yyyy/mm/dd”)
Label30.Caption = Format$((Str((MbusByteArray(16) * 256) + MbusByteArray(17)) & “:” & Str((MbusByteArray(18) * 256) + MbusByteArray(19)) & “:” & Str((MbusByteArray(20) * 256) + MbusByteArray(21))), “hh:nn:ss”)
Label31.Caption = Val(Str((MbusByteArray(22) * 256) + MbusByteArray(23)) & Str((MbusByteArray(24) * 256) + MbusByteArray(25)))
Label32.Caption = Val(Str((MbusByteArray(26) * 256) + MbusByteArray(27)))
With Adodc2
.Recordset.Update
.Recordset.MoveLast
End With
MbusStatus = 0
End Select
Timer1.Enabled = True ‘Set the interval between the next communication
End Sub
The last thing that we do is save and compile the VB program. I will be happy to send the complete program to you . Please go to the contact page and put Robust Logger Program in the description.
In part 10 we will display the information on a web server. We will use IIS and ASP to actively connect and display the information in the database.
If you have any questions or need further information, please contact me.
Regards,
Garry

Now You Can Have Robust Data Logging for Free – Part 1
Now You Can Have Robust Data Logging for Free – Part 2
Now You Can Have Robust Data Logging for Free – Part 3
Now You Can Have Robust Data Logging for Free – Part 4
Now You Can Have Robust Data Logging for Free – Part 5
Now You Can Have Robust Data Logging for Free – Part 6
Now You Can Have Robust Data Logging for Free – Part 7
Now You Can Have Robust Data Logging for Free – Part 8
Now You Can Have Robust Data Logging for Free – Part 9




If you’re like most of my readers, you’re committed to learning about technology. Numbering systems used in PLC’s are not difficult to learn and understand. We will walk through the numbering systems used in PLCs. This includes Bits, Decimal, Hexadecimal, ASCII and Floating Point.

To get this free article, subscribe to my free email newsletter.


Use the information to inform other people how numbering systems work. Sign up now.

The ‘Robust Data Logging for Free’ eBook is also available as a free download. The link is included when you subscribe to ACC Automation.

Now You Can Have Robust Data Logging for Free – Part 8

Now You Can Have Robust Data Logging for Free – Part 1
Now You Can Have Robust Data Logging for Free – Part 2
Now You Can Have Robust Data Logging for Free – Part 3
Now You Can Have Robust Data Logging for Free – Part 4
Now You Can Have Robust Data Logging for Free – Part 5
Now You Can Have Robust Data Logging for Free – Part 6
Now You Can Have Robust Data Logging for Free – Part 7
Now You Can Have Robust Data Logging for Free – Part 8

 

Computer Program Visual Basic (VB6) Continue

The display part of the program is done and we have reviewed the modbus TCP protocol. It is now time to start the VB6 code. Lets discuss how the logic will work.

1. User selects ‘Start Logging’
2. The timer for interval between communications is stopped.
3. The IP address is used to open the winsock connection. If the connection is made then the background colour of the IP Address will turn green and we will continue to step 4. If the connection already exists then continue to step 4. If the connection cannot be made then the background colour is red, further execution is stopped. Program will continue at step 1.
4. The following sequence of commands are made: Determine what command to send.

  • Read the Daily Production Log and Minute Log Pointers (MHR1 and MHR2 )
  • If MHR2 (Minute Log Pointer ) > 670 then
    • Request the Minute Log starting at MHR670 and keep incrementing by 10 until the last location is requested.
    • Write the value of 670 into MHR2 (Reset the pointer)
  • If MHR1 (Daily Production Log Pointer > 30 then
    • Request the Daily Production Log starting at MHR30 and keep incrementing by 20 until the last location is requested.
    • Write the value of 30 into MHR1 (Reset the pointer)
  • Read the current Minute Log information starting at MHR660
  • Read the current Daily Production Log information starting at MHR10
Once the command is sent a timer is set to determine if communications has been lost.
5. If the timer for communications is lost, goto step 3. Repeat the last command.
6. Turn off the timer to determine if communications has been lost. Read the information from the winsock tool. If the data needs to be stored, then update the database. Set the timer for interval between communications.

The above is the general program flow for the program.

Here are our variables:

Dim MbusQuery
Public MbusResponse As String
Dim MbusByteArray(255) As Byte
Public MbusStatus As Integer
Public ProductionPointer As Integer
Public MinutePointer As Integer  

Private Sub Command1_Click()
Timer1.Enabled = False ‘ Stop the interval between reads to the PLC

Timer1 is used to control the amount of time between intervals of communication to the PLC.

Dim StartTime
If Winsock1.State <> 7 Then
    If (Winsock1.State <> sckClosed) Then
        Winsock1.Close
    End If
    Winsock1.RemoteHost = Text1.Text ‘ Set IP Address
    Winsock1.Connect
    
    StartTime = Timer ‘ Use the timer to determine if a connection cannot be made
    
    Do While ((Timer < StartTime + 2) And (Winsock1.State <> 7))
        DoEvents
    Loop
    If (Winsock1.State = 7) Then
       Text1.BackColor = &HFF00& ‘ Change background colour to green
    Else
       Text1.BackColor = &HFF ‘ Change background colour to red
       Exit Sub
    End If
End If

Here is the code to control Winsock1. The IP address entered in Text1 is used to set the RemoteHost. We then check for the state of the Winsock1. The following are the different states that Winsock1 can be:

Constant Value Description
sckClosed 0 Default. Closed
sckOpen 1 Open
sckListening 2 Listening
sckConnectionPending 3 Connection pending
sckResolvingHost 4 Resolving host
sckHostResolved 5 Host resolved
sckConnecting 6 Connecting
sckConnected 7 Connected
sckClosing 8 Peer is closing the connection
sckError 9 Error
Here is the information that we will need to read and write to the MHR registers in the Do-More PLC.

‘ Read the information

‘0000:    Transaction Identifier
‘0000:    Protocol Identifier
‘0006:    Message Length (6 bytes to follow)
’00:      The Unit Identifier
’03:      The Function Code (read MHR Read Holding Registers)
‘0000:    The Data Address of the first register
‘0002:    The number of registers to write
‘ Write the information
‘0000:    Transaction Identifier
‘0000:    Protocol Identifier
‘0009:    Message Length (6 bytes to follow)
’01:      The Unit Identifier
’16:      The Function Code (read Analog Output Holding Registers)
‘0000:    The Data Address of the first register
‘0001:    The number of registers to write
’02:      The number of data bytes to follow
‘0030     The number to put into the register
All of the information will be bytes of data. The maximum value in a byte is 256. If we need to send a value of 600 then the two bytes of data will be:
600/256 = 2.34
The most significant byte is 02
600 – (256*2) = 88
The least significant byte is 88
So the information that we need to send through Winsock1 would be Chr(2) and Chr(88) to represent the value of 600.

In part 9 we will continue with writing the VB6 program. We will start reading and writing the information to the PLC.
If you have any questions or need further information please contact me.
Thank you,
Garry



If you’re like most of my readers, you’re committed to learning about technology. Numbering systems used in PLC’s are not difficult to learn and understand. We will walk through the numbering systems used in PLCs. This includes Bits, Decimal, Hexadecimal, ASCII and Floating Point.

To get this free article, subscribe to my free email newsletter.


Use the information to inform other people how numbering systems work. Sign up now.

The ‘Robust Data Logging for Free’ eBook is also available as a free download. The link is included when you subscribe to ACC Automation.