You can download the example program written in Visual Basic 6.0 from the following link:
MsCommSample.zip
Sample includes detailed explanations as comment lines, so I am not going to explaing details of coding in this post.
To test the software you need to have a null modem cable if you want to communicate two computers.
You can make your own cable by soldering pins of two female DB9 connectors as follows.
Connector 1 Connector 2
Pin 2 <---------------------> Pin 3
Pin 3 <---------------------> Pin 2
Pin 5 <---------------------> Pin 5
If you just want to test how data is sent and received on your computer, you just need one connector; short circuit pin 2 and pin 3:
Connector 1
Pin 2 <--
|
Pin 3 <--
Make the cables at your own risk, short circuiting any pins other than 2 and 3 may damage serial port of your computer.
By default, software opens the Com1 port in 9600 bauds, no parity, 8 data bits and one stop bit without handshaking protocol. You can change it in source codes.
You also need to have Visual Basic 6.0 software to edit, debug and recompile program.
How to Code for RS232 in Visual Basic? Part 3 – Example
How to Code for RS232 in Visual Basic? Part 2 – Getting Data From Serial Port
How to Code for RS232 in Visual Basic? Part 1 – Serial Communication Basics
In this post series, I would like to talk about receiving data from serial port. It is one of the FAQ of industrial automation forums, but it is difficult to find a proper code or advice.
I am not going to talk about in details of a specific communication protocol, it may be a subject for another series of post.
I only want to describe a sample method, and give an example by Visual Basic 6.0. As I will talk about the method, you can apply it for .Net languages or any other programming languages such as Delphi.
We can choose a sample problem; I think reading data from a barcode scanner may be a right and simple example.
Now, let’s have a little bit theory about RS232 communications, it is designed to communicate for long distances without using many cables. RS232 defines the electrical signal levels, and those signals may be converted into other defined standards such as RS485 or RS422, so I am going to describe it with logical signal levels.
Serial communication is just like a dialog between two people. Both of them have ears and mouths to listen and speak.
When Alice talks, Bob hears with his ears.
The RS232 port has a transmitting pin and a receiving pin. If you want to establish a communication between two RS232 devices, you must connect one device’s transmitting pin to other device’s receiving pin. Just as talking and listening.
There is also one more pin which is used as a voltage reference, it is called ground. So only three cables are enough to make a serial communication. If communication should be done for one way only, two cables are enough, one for data and one for voltage reference.
When sending a byte, transmitting pin sends a single start bit signal to inform that a new byte is coming.
Data bits sent after start bit, number of data bits can be 7 or 8 and it depends on the settings that you decide for communication.
It has information about number of logic 1 signals, even or odd numbers.
And finally stop bits come; number of stop bits can be 1 or 2. It can be considered as a guard time between data packages.
Most important parameter in serial communications is the baud rate, which is the number of bits per second. If you set your communication parameters as 8 data bits and 1 stop bit, you should use 11 bits for each byte packet (1 start + 8 data + 1 parity + 1 stop) and with the speed of 9600 baud, you can send up to 872 bytes per second.
In serial devices, an IC named UART makes the communication and buffers some amount of data inside.
In next post, I am planning to tell a pseudo code for receiving data from serial port.
Developing An ActiveX for WinCC Flexible
Several months ago, a customer requested a product tracing system which should work with SIEMENS PLC.
They already had a WinCC flexible licence and requested the project in WinCC.
First of all, I could not find a way to use RS232 comm ports with WinCC, so I decided to use SAX Comm library for RS232 communication.
It was not possible because I could never be able to compile the software even without any script code in it. Just adding SAX Comm object was enough to crash WinCC development environment. WinCC was raising the following error:
“Fatal ERROR: screen item Screen_1 in screen Screen_1 contains inconsistent data and should be deleted.”
Then I tried to code my own ActiveX, embedded SAX Comm object and mapped some properties and events, however result did not changed, WinCC was still crashing.
I decided to walk step by step, I realised that WinCC had a really pain about ActiveX, so I have started with a totally empty ActiveX and successfully compiled the WinCC project.
Then I started to add properties one by one, and with the first property of ActiveX caused a crash again.
In WinCC programming manual, it says that it is possible to read and write a property of an ActiveX within a script, but it was not true.
Reading or writing a property inside a script directly causes a crash.
I have found that the only way to use properties of An ActiveX is using ‘Internal Tags’ of WinCC. If you want to send data inside of an ActiveX, you create an internal tag and assign it to the one of the properties of ActiveX.
With Internal tags, I was successful to get data from PLC to my ActiveX, however I realised the major problem when I finished developing my ActiveX, and it was not possible to send data from ActiveX to WinCC (so PLC)
It was not possible to send by properties, because assigning ActiveX property to an internal tag was working one way only. There was not any ‘direction’ option.
Then I tried to send data by triggering ActiveX events, it was possible to trigger events of ActiveX but it was not possible to send some data by parameters of event.
So I decided to use ActiveX events as digital outputs, I have used them without sending any parameters.
For Boolean data, triggering was not a problem; I have created 2 events for each Boolean output and altered a Boolean internal tag on those events.
My final challenge was sending an integer code to a test device. Boolean outputs were easy but of course it is not convenient to create 255 events for a byte value.
So I created following events for sending byte information to WinCC:
Case Study: Power Measurement System with Accurate Timing
PC Stability
My graduation thesis was about designing a smart card reader / writer circuit and making necessary software development for it. I have tried to design a low cost reader, so I decided to use parallel port of PC instead of using a micro controller.
My advisor encouraged me to develop the software in Linux, that is the story of 'How I met your Linux'. My first experience on linux was on Mandrake Linux 8.
One day, I have wondered how stable was the PC, and decided to make a little test. I have wrote a small software in C and compiled with GCC to produce square wave with the highest frequency that PC can handle.
I have visualised the wave in oscilloscope. PC was able to produce square wave approximately at 200 kHz. Frequency was decreasing as the CPU was loaded, as I expected.
Then I wondered the result in Windows. Rebooted the PC with Windows 98, wrote an equivalent code in assembly and executed. I was expecting to have a little bit higher frequency because code was written in assembly.
Result was surprising for me, it was impossible to lock to the signal to display it on oscilloscope. Frequency was so unsteady that it was impossible to see with auto trigger or manual trigger.
Then I have tried to close some windows and terminated some background processes such as anti-virus. Frequency become more stable but it was still difficult to catch complete cycles more than five seconds. Finally, screen saver disturbed all the frequency.
I have measured the frequency with a frequency counter and it was nearly same as in Linux version, maximum 200 kHz.
Today, we are developing most of the automation projects on Windows operating system, so I wanted to make a small test before sending this post.
I wrote the following code:
Object Oriented Design in Industrial Automation - Example
When I was a PLC programmer, object oriented programming was an unknown concept for me. I did not mind about it since I have become a PC programmer.
In this post, I would like to talk about some benefits of object oriented programming with a simple case study.
An object is described as a computer representation of an existing real world thing or event. Object orinted programming languages gives the opportunity of dealing with these virtual objects, however if you don't design your system as 'object oriented', you can't use the benefits of object oriented approach.
Many PLC programmers have their programming library, most of the times they 'copy and paste' their rungs from previous or existing projects in to the new projects.
"DRY - Don't repeat yourself" philosophy is against for information duplication because duplication may increase the difficulty of change and decrease inconsistency. So, DRY philosophy should be agains 'copy and paste'.
Object oriented design helps you to not to repeat yourself.
Before giving an example, I would like to tell some concepts in object oriented programming, first concept is 'class'.
Class can be considered as a set of objects, and classes are also objects too. For example, a book can be considered as a class made up of multiple page objects. There are some properties of the book such as page count or colour. Also, there are some possible actions can be done with a book, such as reading, or placing on a shelf.
However, in programming classes are not object at all. They are definitons of object and its properties. With this point of view, a class can be considered just as a cookie cutter.
Once you design a cookie cutter, and you produce many cookies with it. The disadvantage of object oriented design can be seen here, if you are going to make only one or two cookies, building a cookie cutter is not convenient. But if you want to make dozens of cookies, a cookie cutter may be a good idea.
It is also possible to extend capabilities or properties of a class and make a superior class. For example, a dictionary is a special book that you can search words in it. So we can talk about the class 'dictionary' which can be derrived from class 'book'. All the actions can be done with a book is still valid with a dictionary, we can read it or place it on a shelf. This is inheritance, which aids reuse of code.
When you read a book, you don't mind how that book was written or how that book was published. You only see the pages of the book which can be considered as 'interface'. This is the principal of information hiding and mostly named as encapsulation.
Information hiding principle has two advantages:
- Programmer does not need to be so experienced
- It is difficult to corrupt the object, because its internal structure is protected
Let's back to our 'book' object. We are able to read books including dictionaries, almanacs, novels etc. Reading action can work on many different types of books. This is called polymorphism. You can ask many different objects to perform the same action.
Before starting to an example on real world of automation, let's revise the concepts we told about:
- Object
- Class
- Inheritance
- Encapsulation
- Polymorphism
Because we are going to check if it is possible to apply them in automation systems design.
A multiple rack store system is a common structure. Mostly, two or more shuttles are used to put or take goods. FIFO can be achieved by this system, and sometimes one of the racks runs in reverse direction to make it possible to take out a good in the middle of the rack.
Well, I think this system is common, becase I have seen at least five and they were used for different purposes such as automatic storage and retrieval system or quality assurance test system.
Anyway, we are going to model such a system below:

Our second object is shuttle. There are two shuttles and each shuttle can give and take boxes in reverse or forward direction.
Our third object is a little bit hidden. Racks are also objects, and they consist of conveyors.
Now, let's focus on our objects capabilities. For example, each conveyor can conduct a box from one conveyor to the next one. Also, each conveyor take boxes.
Many PLC programmers use at least two output flags to give information to the next conveyors code:
1 - I am ready to give
2 - I am giving
and use at least two input flags to give information from previous conveyors code:
1 - I am ready to take
2 - I am taking
Logic is easy, if target conveyor is ready to take and source conveyor is ready to give, box should move to the next conveyor.
We can think about two more objects on the basic conveyor object: a transmitter and a receiver. Of course, these are not real objects, just functionalities but thinking them as seperate objects will be convenient for us.
It is obvious that, transmitter object has 'Transmit' method, and receiver object has 'Receive' method. By designing them as methods, we won't involve in handshaking between two conveyors, remember the encapsulation.
Most object oriented PC programming languages present a notation for objects and methods as follows:
Transmitter.Transmit()
Receiver.Receive()
And these two objects are belong to a conveyor object, so in programming notation, it also can be presented as:
Rack.Transmitter.Transmit()
Rack.Receiver.Receive()
If you realise an inconsistency or a bug in your logic, you do not need to code for all conveyors, just change the transmit or receive method and all objects using these methods will be get changed. Maintenance of object oriented code is easy.
And what about shuttles? Shuttles are also conveyors and also should have transmitter and receiver. So, to transfer a box from shuttle to a conveyor, we should be able to write as:
Shuttle.Transmitter.Transmit()
Rack.Receiver.Receive()
However, shutles have one more fuınctionality, they can move in Y direction to travel between racks, so we can think for a move method for shuttle:
Shuttle.MoveY()
Our another object is rack. A rack is made up of 8 conveyors. Rack can take boxes from shuttle and give boxes to another shuttle. It seems hat we can directly map the rearmost conveyors receive method as racks receive methods and map the farthermost conveyors transmit method as racks transmit method. Mapping is done once, and programmer can use it as:
Rack.Transmit()
without minding that it is actually:
FarthermostConveyor.Transmit()
Many experienced programmers realise one more object: Eight racks form another big object, the 'layer'. Programmers must anticipate that system may be upgraded to a multilayer stock system in next years.
And the final object is the system itself, having several methods such as:
System.Store()
System.Retrieve()
It would be nice to retrieve a box of chocolates just by executing a single command:
System.Retrieve("The yummy one")
Conclusions:
PLC programmers are working with objects for many years, each conveyor, machine, system is an object. Object oriented design and object oriented programming are different concepts.
In our example, we designed our system in object oriented approach but could not give example with rungs. Actually, object oriented PC programming languages present tools and facilities to implement an object oriented system as a code, however the code running at the back is not so different from procedural programming
For example, in object oriented way, you create a Shuttle class. Then you create a specific shuttle object, such as 'InputShuttle'.
You do it as:
____________
Class Shuttle
Method MoveY()
...
...
End Class
Dim InputShuttle As Shuttle
...
InputShuttle.MoveY
__________
And in procedural way, you write a function for moving any shuttle, and state the shuttle to be moved as a function parameter:
__________
Function MoveY(Shuttle As TShuttle)
...
End Function
Dim InputShuttle As Shuttle
...
MoveY(InputShuttle)
...
_____________
With this point of view, function blocks can be used in procedural way.
PLC software development environments and languages does not supply neccessary support for object oriented programming, however you can still design your system in abject oriented approach to recude the amount of code you wrote.
I hope, PLC vendors intruduce new functionalities to their development tools for supporting object oriented programming.
For the next post, I am planning to post something about PC stability. Then I am planning to post a case study, 'Power measurement test system - Beating the speed limits'