10.03.2023
Home / Reviews / Description of the COM port. What is a COM port. Modem and COM port

Description of the COM port. What is a COM port. Modem and COM port

Recently, the serial method of data transmission is replacing the parallel one.
You don't have to look far for examples: the appearance of USB and SATA buses speaks for itself.
Indeed, parallel bus is hard to scale (longer cable, higher bus clock rate), it's not surprising that technologies are turning to parallel buses in the back.

Serial interfaces

Today, there are a great many different serial data interfaces.
In addition to the already mentioned USB and SATA, we can also recall at least two well-known RS-232 and MIDI standards (aka GamePort).
All of them are united by the same - the serial transmission of each bit of information, or the Serial Interface.
Such interfaces have a great many advantages, and the most important of them is a small number of connecting wires, and, consequently, a lower price.

Data transfer

Serial communication can be implemented in two ways: asynchronous and synchronous.

Synchronous data transmission involves synchronizing the operation of the receiver and transmitter by including clock information in the transmitted signal or by using a special sync line.
The receiver and transmitter must be connected with a special synchronization cable, which ensures that the devices operate on the same frequency.

Asynchronous transmission involves the use of special bits that mark the beginning and end of data - start (logical zero) and stop (logical one) bits.
It is also possible to use a special parity bit, which determines an even or odd number of transmitted single bits (depending on the accepted convention).
On the receiving side, this bit is analyzed, and if the parity bit does not match the number of single bits, then the data packet is sent again.

It is worth noting that such a check allows you to detect an error only if only one bit was transmitted incorrectly, if several bits were incorrectly transmitted, this check already becomes incorrect.
Sending the next data packet can occur at any time after sending the stop bit, and, of course, must begin with the start bit.
Can not understand anything?

Well, if everything Computer techologies were simple, then any housewife would have long ago sculpted new protocols in parallel with dumplings ...
Let's try to look at the process in a different way.
Data is transmitted in packets, approximately like IP packets, information bits go along with the data, the number of these bits can vary from 2 to 3 and a half.
And a half?!
Yes, you heard right, with a half!

The stop bit, or rather the transmitted signal corresponding to the stop bit, can have a duration greater than the signal corresponding to the one bit, but less than for two bits.
So, a packet always starts with a start bit, which is always zero, followed by data bits, then a parity bit, and then a stop bit, which is always one.
Then, after some arbitrary period of time, the march of the beats on Moscow continues.

This method of transmission implies that the receiver and transmitter must operate at the same speed (well, or almost at the same speed), otherwise the receiver will either not have time to process the incoming data bits, or take the old bit for a new one.
In order to avoid this, each bit is strobed, that is, it is sent synchronously with a special signal - a “strobe” generated inside the device.
There are a number of specific speeds for asynchronous devices - 50, 75, 110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600 and 115200 bits per second.

You have probably heard that “baud” is used as a unit for measuring the data transfer rate - the frequency of changing the state of the line, and this value will coincide with the data transfer rate only if the signal can have one of two values.
If several bits are encoded in one signal change (and this is the case with many modems), the transmission rate and the line change frequency will be completely different values.

Now a few words about the mysterious term "data packet".
Under the package this case refers to the set of bits transmitted between the start and stop bits.
Their number can vary from five to eight.
One might wonder why exactly five to eight bits?
Why not transfer at once, say, a kilobyte of data within a packet?

The answer is obvious: when transmitting small data packets, we may lose by sending three service bits with them (from 50 to 30 percent of the data), but if the packet is corrupted during transmission, we can easily recognize this (remember about the parity bit?) and quickly transmit him again.
But in a kilobyte of data, it will be difficult to detect an error, and it will be much more difficult to transmit it.

An example of an asynchronous serial data transfer device is a computer COM port, a favorite Trussardi-designed modem, and a mouse connected to the same port, which for some reason narrow-minded secretaries always try to shove into PS / 2.
All these devices work on the RS-232 interface, or rather on its asynchronous part, since the standard also describes synchronous data transfer.

AMD Radeon Software Adrenalin Edition Driver 19.9.2 Optional

The new AMD Radeon Software Adrenalin Edition 19.9.2 Optional driver improves performance in Borderlands 3 and adds support for Radeon Image Sharpening.

Cumulative windows update 10 1903 KB4515384 (added)

On September 10, 2019, Microsoft released a cumulative update for Windows 10 version 1903 - KB4515384 with a number of security improvements and a fix for a bug that broke Windows work Search and caused high CPU usage.

A COM port is most often used to connect the microcontroller to a computer. In this article, we will show how to send control commands from a computer and send data from a controller.

Preparation for work

Most microcontrollers have multiple I/O ports. The UART protocol is the most suitable for communication with a PC. It is a serial asynchronous data transfer protocol. To convert it to a USB interface, the board has a USB-RS232 converter - FT232RL.
You only need an Arduino-compatible board to run the examples in this article. We use . Make sure your board has an LED connected to pin 13 and a reset button.

For example, let's upload a code to the board that displays an ASCII table. ASCII is an encoding for representing decimal digits, the Latin and national alphabets, punctuation marks, and control characters.

int symbol = 33 ; void setup() ( Serial. begin(9600 ) ; Serial. println(" ASCII Table ~ Character Map " ) ; ) void loop() ( Serial. write(symbol) ; Serial. print(" , dec: " ) ; Serial .print(symbol) ; Serial.print(" , hex: " ) ; Serial.print(symbol, HEX) ; Serial.print(" , oct: " ) ; Serial.print(symbol, OCT) ; Serial.print( " , bin: " ) ; Serial.println(symbol, BIN) ; if (symbol == 126 ) ( while (true) ( ​​continue ; ) ) symbol+ + ; )

The symbol variable stores the symbol code. The table starts at 33 and ends at 126, so symbol is initially set to 33.
To start the operation of the UART port, use the function Serial.begin(). Its only parameter is speed. The speed must be negotiated on the transmitting and receiving sides in advance, since the transmission protocol is asynchronous. In this example, the speed is 9600bps.
Three functions are used to write a value to a port:

  1. Serial.write()– writes data to the port in binary form.
  2. Serial.print() can have many values, but all of them serve to display information in a human-friendly form. For example, if the information specified as a parameter to pass is enclosed in quotes, the terminal program will display it unchanged. If you want to display any value in certain system calculus, then you need to add a service word: BIN-binary, OCT - octal, DEC - decimal, HEX - hexadecimal. For example, Serial.print(25,HEX).
  3. Serial.println() does the same as Serial.print(), but still translates the string after displaying the information.

To check the operation of the program, it is necessary that the computer has a terminal program that receives data from the COM port. IN Arduino IDE already built in. To call it, select Tools->Port Monitor from the menu. The window of this utility is very simple:

Now click the restart button. The MK will reboot and display the ASCII table:

Pay attention to this part of the code:

if (symbol = = 126 ) ( while (true) ( ​​continue ; ) )

It stops the execution of the program. If you exclude it, the table will be displayed indefinitely.
To consolidate the acquired knowledge, try writing an infinite loop that will send your name to the serial port once a second. Add step numbers to the output and don't forget to translate the line after the name.

Sending commands from PC

Before doing this, you need to get an idea of ​​how a COM port works.
First of all, all exchange occurs through the memory buffer. That is, when you send something from a PC to a device, the data is placed in some special section of memory. As soon as the device is ready, it reads the data from the buffer. The function allows you to check the state of the buffer serial.avaliable(). This function returns the number of bytes in the buffer. To subtract these bytes, you need to use the function Serial.read(). Let's see how these functions work with an example:

int val = 0 ; void setup() ( Serial. begin(9600 ) ; ) void loop() ( if (Serial. available() > 0 ) ( val = Serial. read() ; Serial. print(" I received: " ) ; Serial. write(val) ; Serial.println() ; ) )

After the code is loaded into the microcontroller's memory, open the COM port monitor. Type one character and press Enter. In the received data field you will see: “I received:X”, where instead of X will be the character you entered.
The program spins indefinitely in the main loop. At the moment when a byte is written to the port, the Serial.available() function takes the value 1, that is, the condition is fulfilled Serial.available() > 0. Next function Serial.read() reads this byte, thereby clearing the buffer. After that, using the functions already known to you, the output occurs.
Using the Arduino IDE's built-in COM port monitor has some limitations. When sending data from the board to the COM port, the output can be organized in an arbitrary format. And when sending from the PC to the board, the transfer of characters occurs in accordance with the ASCII table. This means that when you enter, for example, the character “1”, the binary “00110001” (that is, “49” in decimal) is sent through the COM port.
Let's change the code a little and check this statement:

int val = 0 ; void setup() ( Serial. begin(9600 ) ; ) void loop() ( if (Serial. available() > 0 ) ( val = Serial. read() ; Serial. print(" I received: " ) ; Serial. println(val, BIN) ; ) )

After downloading, in the port monitor when sending “1”, you will see in response: “I received: 110001”. You can change the output format and see what the board accepts with other characters.

Device control via COM port

Obviously, by commands from a PC, you can control any functions of the microcontroller. Download the program managing work LED:

int val = 0 ; void setup() ( Serial. begin(9600 ) ; ) void loop() ( if (Serial. available() > 0 ) ( val = Serial. read() ; if (val= = "H" ) digitalWrite(13 , HIGH) ; if (val= = "L" ) digitalWrite(13 , LOW) ; ) )

When the “H” character is sent to the COM port, the LED on the 13th output lights up, and when the “L” is sent, the LED will go out.
If, based on the results of receiving data from the COM port, you want the program to perform different actions in the main loop, you can check the conditions in the main loop. For example.

A COM port, or serial port, is a bidirectional serial interface that is designed to exchange byte data. At first, this port was used to connect the terminal, and then for the modem and mouse. Now it is customary to use it to connect the source as well as to communicate with the processing of embedded computing systems.

Usage

So, before we talk in more detail about what a COM port is, we need to look into the past to understand its meaning. Literally 15 years ago, a method was used to connect devices to a computer using a special standard connector located on the rear panel. system block using a special RS-232 serial cable. This method has many disadvantages. Such a cable, by modern standards, provides an extremely low data transfer rate - about a hundred kilobits per second. In addition to when the physical connection of the connectors was made, it was necessary to turn off the equipment, and they themselves were attached to each other with screws that ensured reliability, while their dimensions differed in considerable size.

A bit of history

The COM port on computers of that time was traditionally numbered 1 or 2, since there were usually no more than two of them. Additional ports could be installed if needed. When the user has configured software, it was required not to confuse and correctly install exactly the one to which the connection was provided necessary equipment. Each COM port required the correct speed settings, as well as a number of other mysterious parameters that were known only to a narrow circle of specialists. For the connection of the equipment to be successful, all the necessary parameters had to be found out from somewhere or experimentally selected, since in this case there was no automatic configuration. In addition, the connection via the COM port allowed the connection of any software with arbitrary external equipment, even completely incompatible ones, which caused a huge number of errors during the settings process.

Modernity

Now the connection through the COM port is completely supplanted by more modern method, which does not require special knowledge for implementation, namely through a USB port. This method is devoid of all the disadvantages mentioned earlier. However, modern standards for the compatibility of connecting all kinds of GPS equipment and very heterogeneous software were formed quite a long time ago around the concept of COM ports, which have become archaic at the moment.

This is due to the fact that initially almost any equipment, including GPS, was external, and its connection to the computer was made via a serial cable connected to one of the hardware ports. During the configuration process, the user was required to correctly select the port number and the speed of data transmission through it. At that time, the main standard for transmitting data from a GPS receiver to a program, which is now called NMEA-0183, arose. In fact, this standard requires all developers of even the most modern hardware and software to exchange data via COM ports. And all this in the conditions of modern computers, as well as on PDAs, the USB standard has long been the main one. And one more feature is that recently GPS receivers have increasingly been installed directly inside the device case, that is, there is no connecting cable between it and the main device.

Virtual COM ports

A way out was invented, namely, "virtual" COM ports were developed. It turns out that the internal device of the PDA, for example, a GPS receiver, is simulated in software in the form of a COM port, while not being such in hardware terms. At the same time, a program that is designed to interface through such a standard makes no difference how it is implemented. Here the presence of a virtual simulation is allowed, and not the obligatory presence of a hardware implementation. So it is possible to ensure the compatibility of old-style GPS programs with modern equipment.

Changes made

At the same time, the management of the COM port has not changed significantly. The user, in the old manner, must make complex settings almost manually. However, a modern COM port is no longer that bulky device located on the rear panel of the system unit, but a completely different device. And here the whole point is that from a software point of view, all their implementations look faceless, that is, there is no difference between virtual and real ports. For software, the ports differ only by the numbers assigned to them by PDA manufacturers on a completely random basis. For example, the ASUS receiver is usually located on COM5, while PocketLOOX 560 shows the receiver on COM8. It turns out that a program that wants to receive data from a GPS receiver does not initially have any reliable information about the conditional number, under which the port appears, which is prescribed appropriate for the receiver on this PDA.

How does it all work?

Given that among all available COM ports, you can automatic search suitable, the procedure for such a survey is rather unreliable and rather cumbersome. This is due to the fact that the devices displayed in the system as COM ports can be quite diverse and have nothing to do with GPS, they can completely unpredictably respond to such a survey. For example, on a PDA there are ports associated with an internal cellular modem, with USB, with an infrared port, as well as with other elements. Accessing them by a program designed to work with a specific device can lead to a completely unpredictable reaction, as well as to various malfunctions, which often causes the PDA to freeze. That is why an attempt to open a COM port can lead to unexpected situations, up to turning on Bluetooth or And there may be more incomprehensible cases.

COM port operation

For COM ports, an asynchronous universal transceiver chip is used as the basis. This microcircuit exists in several varieties: Intel 16550A, 16550, 16450, 8250. For each COM port, it contains data receiver and transmitter registers, as well as a number of control registers that can be accessed through BIOS, Windows and MS DOS programs. At latest versions microchip has a set of buffers for temporary storage of transmitted and received data. Thanks to this possibility, it is possible to interrupt the work of the central processor less often, as well as to coordinate the speed of data transmission.

Main settings

The COM port device assumes the following characteristic features:

The base address of the port for input and output of information;

Hardware interrupt numbers;

The size of one block of information;

The speed at which data is transmitted;

Honesty detection mode;

Information flow management method;

The number of stop bits.

How to check the computer's COM port? What to pay attention to?

As mentioned earlier, this type of port is a bi-directional interface for a bit-level serial way. A distinctive characteristic in comparison with the parallel port here is the transfer of data bit by bit. The anatomy of a COM port is such that it is not the only one on a computer that uses a serial data transfer method. For example, interfaces such as Ethernet or USB also use a similar principle, but it so happened historically that it is customary to call the port of the RS232 standard serial.

Very often it is required to open a COM port for repair and diagnostics of a computer, while it also needs to be checked for operability. It's very easy to burn an element. Most often this happens due to the fault of the user, who disconnects the device incorrectly, pulling out the connector while the interface is connected. The easiest way to check if the interface is working is to connect a mouse to it. However, it is so difficult to get a complete picture, since the manipulator uses only half of the signal lines out of eight available. Only the use of a special plug and program will allow a performance check. For these purposes, there is already a specially developed software.

Serial ports are loved by developers for their ease of maintenance and use.

And of course, writing to the console of the terminal program is all good, but I want my own application, which, by pressing a key on the screen, performs the actions you need;)

In this article I will describe how to work with com port in C++.

The solution is simple, but for some reason a working example was not found immediately. For sim I save it here.

Of course, you can use cross-platform solutions like QSerial - a library in Qt, I probably will, but in the future. Now we are talking about "pure" Windows C++. We will write in Visual Studio. I have 2010, although this does not play any role ...

Create a new console Win32 project.

Include header files:

#include #include using namespace std;

We declare a com port handler:

HANDLE hSerial;

I do it globally so I don't have to worry about pointers when passing it to functions.

int _tmain(int argc, _TCHAR* argv) (

I can't stand the Windows style of programming. They called everything in their own way and sit rejoicing ...

Now the magic of declaring a string with the port name. The matter is that it is not able to transform char itself.

LPCTSTR sPortName = L"COM1";

Working with serial ports in Windows works like with a file. Opening the first com port for writing/reading:

HSerial = ::CreateFile(sPortName,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);

Checking the functionality:

If(hSerial==INVALID_HANDLE_VALUE) ( if(GetLastError()==ERROR_FILE_NOT_FOUND) ( cout<< "serial port does not exist.\n"; } cout << "some other error occurred.\n"; }

Now you need to configure the connection parameters:

DCB dcbSerialParams = (0); dcbSerialParams.DCBlength=sizeof(dcbSerialParams); if (!GetCommState(hSerial, &dcbSerialParams)) ( cout<< "getting state error\n"; } dcbSerialParams.BaudRate=CBR_9600; dcbSerialParams.ByteSize=8; dcbSerialParams.StopBits=ONESTOPBIT; dcbSerialParams.Parity=NOPARITY; if(!SetCommState(hSerial, &dcbSerialParams)) { cout << "error setting serial port state\n"; }

On msdn, it is advised to first get the parameters, and then change them. We are still learning, so we do as requested.

Now let's declare the string that we will pass and the variables necessary for this:

Char data = "Hello from C++"; // string to pass DWORD dwSize = sizeof(data); // size of this string DWORD dwBytesWritten; // here will be the number of bytes actually transferred

Sending a string. Let me remind you that the example is the simplest, so I don’t do any special checks:

BOOL iRet = WriteFile(hSerial,data,dwSize,&dwBytesWritten,NULL);

I also decided to display the string size and the number of bytes sent to control:

Cout<< dwSize << " Bytes in string. " << dwBytesWritten << " Bytes sended. " << endl;

At the end of the program, we make an infinite loop of reading data:

While(1) ( ReadCOM(); ) return 0; )

Now the read function:

Void ReadCOM() ( DWORD iSize; char sReceivedChar; while (true) ( ​​ReadFile(hSerial, &sReceivedChar, 1, &iSize, 0); // get 1 byte if (iSize > 0) // print cout if something is received<< sReceivedChar; } }

That's actually the whole example.

In computing, a serial port is a serial communication interface through which information is transmitted or output at a time. For most of the history of personal computers, data was transferred through serial ports to devices such as modems, terminals, and various peripherals.

While interfaces such as Ethernet, FireWire, and USB all send data as a serial stream, the term "serial port" usually identifies hardware more or less compliant with the RS-232 standard, designed to interface with a modem or similar communication device.

Modern computers without serial ports may require serial converters to ensure compatibility with RS-232 serial devices. Serial ports are still used in applications such as industrial automation systems, scientific instruments, point of sale systems, and some industrial and consumer products. Server computers can use the serial port as a management or diagnostic console. Network equipment (such as routers and switches) often use the serial console for configuration. Serial ports continue to be used in these areas because they are simple, cheap, and their console features are highly standardized and widespread.

COM port pinout (RS232)

There are 2 types of com port, a 25-pin old connector and a newer 9-pin connector that replaced it.

Below is a diagram of a typical standard 9-pin RS232 connector with connectors, this type of connector is also called a DB9 connector.

  1. Carrier Detect (DCD).
  2. Receive Data(RXD).
  3. Data transmission(TXD).
  4. Receiver Ready to Exchange (DTR).
  5. Ground(GND).
  6. Source Ready to Exchange (DSR).
  7. Request to Send(RTS).
  8. Ready to Transfer (CTS).
  9. Call signal(RI).

RJ-45 to DB-9 Serial Port Adapter Pin Information for Switch

The console port is an RS-232 serial interface that uses an RJ-45 connector to connect to a control device such as a PC or laptop. If your laptop or PC does not have a DB-9 connector pin and you want to connect your laptop or PC to the switch, use a combination of RJ-45 and DB-9 adapter.

DB-9RJ-45
Getting Data2 3
Data transfer3 6
Willingness to exchange4 7
Earth5 5
Earth5 4
Willingness to exchange6 2
Transfer request7 8
Transfer Ready8 1

Wire colors:

1 Black
2 Brown
3 Red
4 Orange
5 Yellow
6 Green
7 Blue
8 Gray (or white)