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:
Importance of State Analysis In Automation Systems - - Part 2
Here is the second part of state analysis. In this post, we are making a state analysis for a simple single station for both PC and PLC.
Importance of State Analysis In Automation Systems - Part 1
One of the major problems in PC supported automation systems is deadlock problem.
Sometimes, systems fall into a deadlock, making it impossible to free up without intervention or you can miss your products without recording their information into database.
In this post, I want to give a poor design sample which may cause a deadlock with a presentation.
Next post, we will make a state analysis for a single task station.