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.
Simple PC – PLC Communications Part V - Expect The Unexpected
The handshake used for PC – PLC communication works well if everything is perfect. But what happens if something happens that violates the handshake?
In some countries power loss is an ordinary part of life. Generally PC is supplied by UPS, but PLC is not. In a power loss, PLC lefts the communication unexpectedly.
Even if you are using an industrial PC, it is not stable as a PLC. PC or the software running on PC may be frozen. In this case, PC or software may need a restart.
In the other hand, physical conditions may also violate the handshake. Imagine that boxes crashing on station:
It is possible; sensors may be dirty or defected, or any other unexpected reason may cause this.
Most PLC systems use non-volatile memory for holding data permanently. For a PLC system, it is not difficult at all.
If we are programming the PC, it is obvious that we have to store some information in a file or in database to avoid failure after power loss.
- Store all global variables (handshakes last time interval, all counters etc. and the most important; the last data you wrote in to PLC) in to a text file or database as soon as they changed.
- Restore all global variables on start up.
- Do not execute 'Write all outputs' before restoring all global variables.
VB6.0 example:
Public Property Get Data As Integer
Public Property Let Data(ByVal New_Data As Integer)
End Property
'Get' routine is executed whenever you attempt to read property 'Data' as
A = Data
or
And 'Let' routine is executed whenever you attempt to write property 'Data' as:
or
Use 'Property' as a global variable, add a record routine inside 'Let' and never mind again.
For the example above, restoring the 'm_Data' global variable instead of restoring 'Data' is works faster because restoring 'Data' will cause re-recording the 'Data' again and in some cases it may cause a violation error as 'File Already Open'
Rule 3 is also important. Think about the handshake graphics:
C may assign anything in the memory unless variable is initialised.
If it is not possible to control write and reads?
Some SCADA systems also uses 'Read All Inputs' and 'Write All Outputs' routines for PC – PLC communications. However some of them may execute those routines in a separate thread so it may be possible to write a global variables (mostly named as 'Tag' in SCADA systems) value into PLC before initialising or restoring it.
One solution is using a cyclic counter in PLC. PLC has an increasing counter and PC echoes it into another data memory of PLC. If counter value is equal to the echo, PLC increases the counter and waits the same value from PC. If PC can not update in a certain time, PLC understands that PC has failed, and the data from PC is untrusted. This method also can be used to understand if PC is still working.
Fragmanted Information
This may be dangerous if automation system designers don't take sufficient precautions.
Assume that PC is writing a receipt into PLC data memories, and 'receipt ready' flag is at the beginning of the data memory area.
PLC may apply the receipt as soon as it realises 'receipt ready' flag is set, however receipt may not be ready on time. This results of this design may be difficult to debug.
How can we avoid fragmented information?
- We can use checksum at the beginning or at the end of data memory area reserved for PC's write operations. However, there is still a probability that something may leak because there are too many possibilities to produce the same checksum value.
- We can use the counter solution stated above. Reserve two data memories in the data memory area which is reserved for PC's write. Let the PC fill two data memories with the echo from PLC. If both data memories are same and fresh, PLC can trust that information is consistent and trustable.
Our next post will be about customers. 'Customer is always right and mostly undefined'. And we will have another series with a case study. We are going to examine a data collection system to use for assembly line balancing and reporting the bottle necks on an assembly line.


