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.