39

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.

iammurtaza
  • 957
  • 3
  • 16
  • 31
  • I think you won't be available to work from user-mode with COM directly as it was in . If you want to use old connection, you should continue using 16-bit compiler and run your app in some dos emulator, i.e. dosbox. It will forward physical COM to the bios.h interfaces. – zabulus Apr 03 '13 at 17:53
  • I could not understand completely what you meant to say? Can I run my .exe file of my program which has been developed in Mingw in DosBox? If yes how? – iammurtaza Apr 03 '13 at 18:03
  • In your question you told about . Looks like this file was used in dos environment to communicate with COM port. This way of communicating on modern windows now isn't available. I assume you want the compile exe to be run in windows environment. In this case you should use another API. i.e. google for "CreateFile COM1" – zabulus Apr 03 '13 at 18:06

3 Answers3

68

Please take a look here:

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);
gigabot
  • 66
  • 5
ollo
  • 24,797
  • 14
  • 106
  • 155
  • You tell you can also use it on Linux, but you '#include '? I'm unsure if it will/won't work on Linux/OSX. – Paul Feb 15 '17 at 11:00
  • 2
    No, you can use the information / code from the link on both systems. The code from my answer is Windows only. – ollo Mar 12 '17 at 16:48
  • 1
    The second link is dead but another copy (going off of file name here) is at http://bd.eduweb.hhs.nl/micprg/pdf/serial-win.pdf – Seabass77 May 06 '19 at 19:36
  • second link is dead, any alternative please? – John Sall Dec 24 '20 at 09:03
3

For the answer above, the default serial port is

        serialParams.BaudRate = 9600;
        serialParams.ByteSize = 8;
        serialParams.StopBits = TWOSTOPBITS;
        serialParams.Parity = NOPARITY;
Mi Po
  • 1,123
  • 2
  • 12
  • 21
3

Or you can just use boost::asio::serial_port!

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Chris
  • 31
  • 2