How to read data from a com port. Serial Port Monitor COM Port Monitoring Software



This library is called SerialGate.dll. If you want to use examples from this and subsequent articles, download it (the library) and, if desired, its sources in the previous article in this section.

As a “training base,” I decided to consider solving a fairly common problem - transferring data between two computers. We will, of course, transfer data via the COM port.

As strange as it may seem, let's start with the hardware. In order to transfer data between two computers, they must be connected with a special cable. It's called a "null modem" cable for COM port. It is sold under the name "DB9 Null Modem Cable" in any radio parts store. A COM port - female connector is installed at both ends. Connect the ports of two computers with this cable.

Of course, some of the readers asked the question - I don’t have two computers, how can I try transmitting data via the COM port? In this case, the following option is perfect for testing and verification - use two COM ports of the same computer. Those. send data from the program to one port and read data from another, having previously connected them with a null modem cable.


Let's get down to programming. First, let's write a console application that will read a character from the keyboard and send it to the COM port. We create, as usual, in Microsoft Visual Studio console application and call it, for example, COMServer. The SerialGate.dll library must be connected to the empty blank. The sequence of actions is completely similar to working with the inpout32.dll library. Place files from the downloaded archive SerialGate.lib, SerialGate.h to the project folder and SerialGate.dll to the folder to executable program(Debug or Release). Next, add the lib and h file in the project itself. If you are too lazy to do all this, the project can be downloaded ready-made.

This is actually the program code itself. Let's consider what happens in it. First, an instance of the SerialGate class - sg - is created. Next, we try to open access to the COM port using the OpenPort() class method. To do this, you need to pass the COM port number (integer) to the function. That is, for example, if you have a COM port called COM3 installed on your computer, and you want to open it, you need to pass the number 3 as the first parameter to the function. The second parameter is the speed at which the port will operate when transferring data. The speed for COM ports is measured in bits per second and can take some value from a certain list. By default, the following speeds can be used in Windows:

110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200, 128000 and 256000 bps.

However, many virtual COM ports support much more high speeds(up to 1 MB/s). In this case, you need to use those speeds that are supported by one or another third-party port. The speeds listed above definitely work for any ports.

In the presented example, port "COM1" is opened at a speed of 57600 bps. Next, in an “endless loop,” a character is entered from the keyboard and sent to the COM port using the Send() class method. The function is passed two parameters: the address of the buffer in which the data to be sent is located, and the second parameter is the size of this data in bytes. To complete the input, just enter the “#” symbol - the program will exit the loop and use the Close() method to close the port.

#include #include #include "SerialGate.h" int main() ( int port = 1; int bool res = sg.Open(port, rate); if (res == false return 0; ) else ( printf("Open OK!..\n"); ) printf("Press key to send it to COM port.\n"); printf("Press "#" to exit.\n\n"); while (true) ( ​​char c = getch(); printf("%c", c); sg.Send(&c, sizeof (c)); if (c == "#") break ; ) sg.Close(); getch(); return 0; )

Here is a simple application for sending data. Now let's write a second program that will open the second COM port connected to the first cable and read data from there. Ready application project COMClient can be downloaded in the header of the article.

So, again we create an instance of the SerialGate class and open the “COM2” port at a speed of 57600 bps. Please note that in order to receive/transmit data, both ports involved in information exchange must be open at the same speed. Next, in an “endless” loop with an interval of one second, we read data from the COM port using the Recv() method. It needs to pass two parameters: the address of the buffer in which the read data will be placed and the size of this buffer in order to avoid going beyond the array. The function returns the number of bytes read. Then the read data is printed byte by byte and if the "#" character is found, then COMServer has finished working and this program needs to do the same.

#include #include #include "SerialGate.h" int main() ( int port = 2; int rate = 57600; SerialGate sg; bool res = sg.Open(port, rate); if (res == false ) ( printf ("Open Error..\n"); getch(); return 0; ) else ( printf("Open OK!..\n"); ) printf("Get data from COM port every 1 sec.\n\ n"); char buf; int dwBytesRead = 0; bool terminate = false; while (!terminate) ( Sleep(1000); dwBytesRead = sg.Recv(buf, sizeof (buf)); for (int i = 0; i if(buf[i] == "#") ( terminate = true ; break ; ) ) ) sg.Close(); return 0; )

It's time to test this entire system. Check if you have connected the ports with a cable. We compile and run both applications. In the COMServer program we type some text from the keyboard - with some delay it should appear in the COMClient program window.


In the following articles we will write Windows application for receiving/transmitting data and consider other features of the library.

© Ivanov Dmitry
May 2007

About how to beautifully present the data sent by Arduin to Serial. In my opinion, the guys proposed a very beautiful solution, which on the one hand looks quite simple, and on the other allows you to get an excellent result with a minimum of effort.

In the comments to the article, they expressed regret that such a solution would not work under Firefox and expressed the idea that “you can also write a simple web server with html output based on this thing.” I was hooked by this idea, a quick search on Google did not turn up a ready-made solution, and I decided to implement the idea myself. And this is what came of it.

Warning! The proposed solution should in no way be considered complete. Unlike Amperka's Serial Projector, it is a concept, a demonstration of a possible approach, a working prototype and nothing more.

Some time ago I did a project in which I used the accelerometers built into an Android smartphone to control servos connected to an Arduino. Then for these purposes I used the Scripting Layer for Android (SL4A) and RemoteSensors projects. It turns out that in standard library python includes the BaseHTTPServer package, with the help of which raising a web service in Python is a task of a couple of lines of code.

I didn't have any Arduino sensors at hand, so I used the internal thermometer built into the Arduino Uno as a source of displayed information. As far as I understand, it is not very accurate and is not at all intended for measuring ambient temperature, but it is quite suitable for prototyping.

After some googling, this sketch for the Arduino appeared:

// source: https://code.google.com/p/tinkerit/wiki/SecretThermometer long readTemp() ( long result; // Read temperature sensor against 1.1V reference ADMUX = _BV(REFS1) | _BV(REFS0) | _BV(MUX3); delay(2); // Wait for Vref to settle ADCSRA |= _BV(ADSC); // Convert while (bit_is_set(ADCSRA,ADSC)); result = ADCL; result |= ADCH