# Here is a Method That is Helping PLC Programmers to Program Faster

PLC programming involves both direct and indirect addressing. Direct address programming involves writing each ladder logic rung to do the operation required. We often forget about using the powerful indirect addressing to solve our logic.

The below animated picture will show a simple example of using indirect addressing. This will use the MOVE instruction and transfer a word indirectly to output word V100. V[V0] means that the value in V0 will point to the V memory to get the value to move. You can think of this as a pointer for the memory location to move.

Of course we need to monitor V0. Our values are in sequence from V1 to V6. We need to ensure that V0 is always in the range from 1 to 6.

Lets take a look at a program sample using the Do-more Designer Software. We will set up the sequence similar to the animation above, but expand the program.
Just like above we will set up the pointer at V0 and the output at V100 memory locations. V1 to V37 will hold our output data sequence. This is outputs that we want to set on each event and/or time frame. You can see some of the registers and the corresponding values. These are set as a hexadecimal value. The following link will provide a review of the numbering systems in the PLC. (WHAT EVERYBODY OUGHT TO KNOW ABOUT PLC (PROGRAMMABLE LOGIC CONTROLLER) NUMBERING SYSTEMS)

This is the logic to set up the move instruction. The source is V[V0] which means the pointer is V0 in this memory area. The destination will be V100.

An internal timing bit ST5(\$100ms) is used to increment the pointer V0. This could also be done by an event or series of events. The pointer is then compared to ensure that it is between 1 and 37.

Finally the output word is then transferred to the physical outputs. This is done by using the MAPIO instruction. Each bit can be set independently.

This example uses indirect addressing to program a sequence based upon time. We could just as easily used indirect addressing to compare inputs to a table and set the outputs accordingly. You can see how this method can greatly reduce the amount of time to develop your program. This holds especially true if the sequence needs to be changed. It would be just a matter of changing data values in the table.

The following are separate posts that use indirect addressing:

Building a PLC Program You Can Be Proud Of – Part 1
This use the control of an intersection traffic light to demonstrate direct versus indirect addressing.

Building a PLC Program That You Can Be Proud Of – Part 2
A sample program to control valves. This uses indirect addressing for the inputs as well as the outputs.

Now You Can Have Robust Data Logging for Free – Part 1
Using indirect addressing, this sample program will log information in the PLC to be retrieved at a later time.

Indirect addressing is a powerful method of programming to simplify and program faster than you ever thought possible. You can even use indirect addressing in the PLC to scale a non-linear analog input signal. Let me know you thoughts on using indirect addressing. What can you come up with?

Watch on YouTube : Here is a Method That is Helping PLC Programmers to Program Faster

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 Secret of Using Counters

Counters  are used in the majority of PLC programs. This is especially true if part of your SCADA system. Counters like the animated picture above count things. In this situation we are counting the number of turns the little guy makes. The counter is displaying the total number. This is considered a totalizing counter. If an output turned on to do something then it would be a preset (target number entered for the count) counter. There are also a wide variety of off the shelf industrial counters that you can use. The implementation of counters can be vast, however it all starts with a TIMING CHART. This is the same as the timing charts we discussed in ‘The Secret of Timers’ post.

A timing chart is the secret behind understanding of the counter that you need in your application. Making a timing chart before writing the program will ensure that all of the information will be accounted.

The timing chart is mapped out on a x and y plain. The ‘y’ plain has the state of the input on/off (1 or 0). The ‘x’ plain will show time.

The following shows a timing chart for a counter:
As you can see in this timing chart, you have an input, output and display.

Inputs:
Inputs are used usually sensors that are wired to the counter (PLC) to indicate the items that we need to count. They can be switches, photoelectric sensors, proximity sensors, encoders, etc. (Wiring of NPN / PNP devices) A counter will generally have only one input. In the case of an encoder input it is still only one input, however this is wired usually as a A, B and Z phase. Z is always the reset. A and B indicate the pulses and are leading or trailing each other by 90 degrees depending on direction. Allot of counters will also allow you to as a direction input signal. However this is all still only one input.

Outputs:
Outputs from counters are generally discrete. This means that they are on or off, similar to the inputs. Outputs will trigger when the count value matches the set value. The duration that the output is on depends on the reset signal, to start the count again. (DC Solenoids protection) Allot of the counters today will allow you to have multiple outputs. These multifunction counters can have several preset outputs that trigger when the counter set value has been reached. Batch outputs are also available on some of the industrial counters. A batch output counts the number of times that the preset has been reached. This output will be turned on when the number entered for the batch has been reached.

Set Value – SV:
This is usually on the display and shows the preset value. It is the target number of counts.

Present Value – PV:
This is usually on the display and shows the current or accumulated value.

The PLC programming is usually not that much different then the industrial counter. Allot of the manufactures will have an up counter, down counter and/or an up/down counter. Just as the name implies the display is either counting up or down. You have to refer to the instruction manual of the manufacturer you are programming for the way in which the counter will be programmed.

In the above example Do-More PLC program we have an up and a down counter. X0 is the input and X1 is the reset on both of these counters. (CT0, CT1)
The preset value is stored in memory location D0. This value is set to the number 3.
When the present value (accumulated) reaches the set value (preset) then the CT0.Done bit goes on and the output Y0 is active. Y0 will remain on until the reset input goes on.
The only difference for down counter is the display. You will see that the present value will count down to zero (0) before the CT1.Done bit is turned on.
These counters are memory retentive. So in order to make the counter non-memory retentive, use the first scan bit of the PLC to trigger the reset of the counter. (ST0 – \$FirstScan)

Every PLC has counters. They all have different types depending on what you are trying to achieve. It will all start with your Timing Chart.

Watch on YouTube : Learn PLC Programming – Free 9 – The Secret of Counters

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 Secret of Using Timers

Timers are used in the majority of PLC programs. There are also a wide variety of off the shelf industrial timers that you can use. The implementation of timers can be vast, however it all starts with a TIMING CHART.

A timing chart is the secret behind understanding of the timer that you need in your application. Making a timing chart before writing the program will ensure that all of the information will be accounted.

The timing chart is mapped out on a x and y plain. The ‘y’ plain has the state of the input on/off (1 or 0). The ‘x’ plain will show time.

Lets take a look at a timing chart for an On-Delay Timer. This is the basic operation for an Omron H3BR industrial timer.

Power –  When dealing with PLC’s we must consider when power to the unit is removed what happens to the current time and output conditions.
Start – In this case the start signal is momentary to start the time cycle. (t) We could modify this signal to be maintained until the output switches.
Output – The output will show when it turns on. This can also indicate the opposite, and show when it turns off.
Time – Time is shown by the relationship between the start signal and the output. Our example shows timing starts on the leading edge of the Start. This could have also been on the trailing edge.

Here is the same on-delay timing chart with some more detail. Several conditions are added to the chart.

These conditions prompt us to ask the following questions.
What happens when:

• Power is removed / restored
• Multiple start signals are received
• Do we need a Reset signal. If so what happens during its operation
• Do we need a display of the time. Present Value (PV) / Set Value (SV)

As you can see the timing chart is vital in determining how the sequence will be performed. This is the exact same method that I use when determining timing sequences in a PLC program.

Lets look at an example.

When we hit the start button, the warning light then comes on. After a fixed time the warning light goes off and the motor starts. The motor will run until the stop button is hit.

We will start by using the Start / Stop Circuit we did earlier.

You will notice that we have added an internal memory bit (C0) as our Start Sequence. This is a memory retentive bit, so we can use the (ST0) \$FirstScan to make this circuit non-memory retentive. If power goes off, or the PLC is put into program mode the circuit does not remember the last state. It will default to be off.
The sequence is as follows:

• Start pressed
• TMR starts to time (10seconds)
• Warning output comes on
• After TMR (10seconds)
• Warning output goes off
• Motor output comes on
• Stop pressed
• TMR is reset to 0
• Warning light off
• Motor is off

Every PLC has timers. They all have different types depending on what you are trying to achieve. It will all start with your Timing Chart.

Watch on YouTube : Learn PLC Programming – Free 8 – The Secret of Timers

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.

# Here’s a Quick Way to Wire NPN and PNP devices

Here’s a Quick Way to Wire NPN and PNP devices

I get asked often on how to wire NPN and PNP devices to the programmable logic controller. This can be confusing at first when looking at the wiring diagrams. I have managed to destroy a few sensors in the process….. so lets get started and I will share my experiences.

NPN and PNP refer to the transistor in the output device.
NPN – Negative Positive Negative Switching. Sometimes referred to as ‘Sinking’ the load.  People have told me that when the NPN sensor blows it has a tendency to blow in an open state. (No Signal)
PNP – Positive Negative Positive Switching. Sometimes referred to as ‘Sourcing’ the load. People have told me that when the PNP sensor blows it has a tendency to blow in a closed state. (Signal On)

When the sensor blows, (malfunctions) it usually will also take out the power supply. (Fuse) It generally does not matter if you use NPN or PNP sensors provided they are all connected to the PLC using isolated commons.

You cannot mix PNP and NPN sensors on the same common point for inputs to the PLC. If you do mix the sensors, then the different common points on the PLC must be isolated from each other. This means that the commons are not connected internally to each other. Not ensuring this takes place will provide a short across the power supply and blow your sensors and supply. In general, machines tend to use all NPN or all PNP only.

Colour coding of the wires vary. Do not always rely on the colour code of the wires for connection. Refer to the wire diagrams in the documentation.

The following is a wire diagram of an open collector PNP sensor. You will notice that the load appears between the 0V (Blue)  and Switching wire (Black). When connecting to the PLC, the PLC input acts as the load. The 0V (Blue) will be attached to the common input and the Switching wire (Black) will be attached to the input number.

The following is a wire diagram of an open collector NPN sensor. You will notice that the load appears between the +V (Brown)  and Switching wire (Black). When connecting to the PLC, the PLC input acts as the load. The +V (Brown) will be attached to the common input and the Switching wire (Black) will be attached to the input number.

As you can see a direct short will be created if NPN and PNP sensors are wired into the PLC on the same common. The following shows an example of wiring of the 3 wire sensors into a PLC with isolated commons.

Watch on YouTube : Wiring NPN Sensor to PLC

Watch on YouTube : Wiring PNP Sensor to PLC

Watch on YouTube : Wiring Contact (Discrete) PLC Inputs

Wiring Interposing Relays
: Wiring NPN and PNP Sensors into the PLC with an Interposing Relay
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.

# 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
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>

<meta HTTP-EQUIV=”Refresh” CONTENT=”300″>
<title>ACC Automation – Robust Logger</title>

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.

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.

OBJdbConnection.Open “Provider=Microsoft.ACE.OLEDB.12.0;DATA SOURCE=C:\AccRL\data\AccRL.accdb;Persist Security Info=False;”
with rs1
.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:
Graphs:
Gauges:
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.
Regards,
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.

# 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 .

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.

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

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.

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.
Lets take a look at the AccRL.asp file:

<html>
<meta HTTP-EQUIV=”Refresh” CONTENT=”300″>
<title>ACC Automation – Robust Logger</title>
<%
Dim StartTime, EndTime
StartTime = Timer

Dim OBJdbConnection
Dim rs1
Dim objCmd

OBJdbConnection.Open “Provider=Microsoft.ACE.OLEDB.12.0;DATA SOURCE=C:\AccRL\data\AccRL.accdb;Persist Security Info=False;”
with rs1
.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.
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.
Regards,
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.

# 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.
Select Programs and Features
Select Turn Windows features on or off
Select ASP after expanding Internet Information Services / World Wide Web Services / Application Development Features. This will select all of the other options.
Hit OK to install the services.
We now have IIS installed.
Under the following default directory you will find the location to put your web pages.
C:\inetpub\wwwroot\
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.
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.
Regards,
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.

# 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

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.

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
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
ProductionPointer = Val(Str((MbusByteArray(10) * 256) + MbusByteArray(11)))
If ProductionPointer <> 30 Then
ProductionPointerNow = ProductionPointerNow + 20
MbusStatus = 1
Else
MbusStatus = 4
End If
Case 1
.RecordSource = “Production_Log”
.Refresh
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
.Recordset.Update
.Recordset.MoveLast
.Refresh
End With ProductionPointerNow = ProductionPointerNow + 20
If ProductionPointer = ProductionPointerNow Then
MbusStatus = 2
End If
Case 2
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
.RecordSource = “Production_Log”
.Refresh
End With
Case 4
MinutePointer = Val(Str((MbusByteArray(10) * 256) + MbusByteArray(11)))
If MinutePointer <> 670 Then
MinutePointerNow = MinutePointerNow + 10
MbusStatus = 5
Else
MbusStatus = 8
End If
Case 5
.RecordSource = “Minute_Log”
.Refresh
End With
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)))
.Recordset.Update
.Recordset.MoveLast
.Refresh
End With MinutePointerNow = MinutePointerNow + 10
If MinutePointer <= MinutePointerNow Then
MbusStatus = 6
End If
Case 6
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
.RecordSource = “Minute_Log”
.Refresh
.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
.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)))
.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.
Regards,
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.

# 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)
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.

‘0000:    Transaction Identifier
‘0000:    Protocol Identifier
‘0006:    Message Length (6 bytes to follow)
’00:      The Unit Identifier
‘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.