Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serial Port (RS -232) Connection in C++

I have done serial port RS-232 connection in C++ using 16-bit compiler (I was using Turbo C++ IDE). It included header file bios.h which contain all the required functions for reading values from the port. Now I want to read value from serial port using C++ 32-bit Mingw compiler. I am using Dev CPP as my IDE. Here I could not find bios.h. Are there any special header files available for this purpose in Mingw? I am using 32-bit compiler now because in my college project I got to use Exception handling which I guess is not supported in Turbo C. Please help me out.

like image 469
iammurtaza Avatar asked Apr 03 '13 17:04

iammurtaza


People also ask

Is RS-232 a COM port?

RS232 is a popular communications protocol for connecting modems and data acquisition devices to computers. RS232 devices can be plugged straight into the computer's serial port (also known as the COM or Comms port).

Is RS-232 the same as serial?

RS-232 signals are similar to your microcontroller's serial signals in that they transmit one bit at a time, at a specific baud rate, with or without parity and/or stop bits. The two differ solely at a hardware level.

Is RS485 a serial port?

RS232, RS422, RS423, and RS485 are all essentially physical layer protocols. They are all serial communication protocols and are ubiquitous device interfaces.

What does RS stands for in RS-232 and RS485?

RS-232 and RS-485 are standards for serial interfaces that each specify the transmission media and a defined set of logic levels, data rates and timings.


1 Answers

Please take a look here:

  • RS-232 for Linux and Windows 1)
  • Windows Serial Port Programming 2)
  • Using the Serial Ports in Visual C++ 3)
  • Serial Communication in Windows

1)You can use this with Windows (incl. MinGW) as well as Linux. Alternative you can only use the code as an example.

2)Step-by-step tutorial how to use serial ports on windows

3)You can use this literally on MinGW

Here's some very, very simple code (without any error handling or settings):

#include <windows.h>  /* ... */   // Open serial port HANDLE serialHandle;  serialHandle = CreateFile("\\\\.\\COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);  // Do some basic settings DCB serialParams = { 0 }; serialParams.DCBlength = sizeof(serialParams);  GetCommState(serialHandle, &serialParams); serialParams.BaudRate = baudrate; serialParams.ByteSize = byteSize; serialParams.StopBits = stopBits; serialParams.Parity = parity; SetCommState(serialHandle, &serialParams);  // Set timeouts COMMTIMEOUTS timeout = { 0 }; timeout.ReadIntervalTimeout = 50; timeout.ReadTotalTimeoutConstant = 50; timeout.ReadTotalTimeoutMultiplier = 50; timeout.WriteTotalTimeoutConstant = 50; timeout.WriteTotalTimeoutMultiplier = 10;  SetCommTimeouts(serialHandle, &timeout); 

Now you can use WriteFile() / ReadFile() to write / read bytes. Don't forget to close your connection:

CloseHandle(serialHandle); 
like image 54
ollo Avatar answered Sep 19 '22 15:09

ollo