Boost Asio Serial Port Flush

вторник 04 декабряadmin
Boost Asio Serial Port Flush Rating: 6,7/10 7856 reviews
13 Oct 2008CPOL
This article introduces and explains the use of the serial_interface class. An easy way to add serial port interfaces to your C++ code.
Boost asio serial port flush system


Aug 11, 2012  Clone via HTTPS Clone with Git or checkout with SVN using the repository’s web address. Sep 07, 2009  A set of C classes to easiliy handle serial ports across different platforms, built on top of boost.asio. fedetft/serial-port. A set of C classes to easiliy handle serial ports across different platforms, built on top of boost.asio. fedetft/serial-port. Skip to content.

The above download,, contains a header and a source file (.hpp, and .cpp) that define the serial_interface class. This class was written to make it easier to implement and use serial ports in Windows for native C++ programs. While the references in the MSDN library for Programming Serial Connections are easy to follow, there may be times where the serial port should be configurable during runtime. This is a basic class, and does not support all of the features I would like, but it makes the initial setup much easier, thereby saving the software developer time.

The difficult part for the new COM programmer is the fact that Windows requires the user to fill out a structure containing over 20 variables, most of which will be set to 0, or NULL, or FALSE anyway. Using this structure, the user only needs to set the five most commonly used variables, which makes the setup process easier and faster.


I wrote this code for a project I was doing at work because I needed it. Once it was done, it occurred to me that others may need it as well, so I'm am making it available.

Using the code

Using this class is very easy. It does not rely on any third party libraries, so all you have to do is include the serial_interface.hpp and serial_interface.cpp files in your project, #include 'serial_interface.hpp' in files that need access to the serial port, get a pointer to the instance and handle, and start running.

Once the proper files are included in all the right places, using the class is very easy. The code looks like this:

You could then get a structure for your configuration variables. This step is optional since the class will use the following default variables if you don't specify them. The default values are:

  • COM port: COM1
  • Baud rate: 38400
  • Byte size: 8
  • Stop bits: 1
  • Parity: none

However, if you would like to change those, a call to get_serial_interface will return the current port settings. Note that the software will attempt to open with these settings in the constructor as a self-test. The code to get the settings looks like this:

The SI::serial_variables structure contains four members: baud, parity, bits, and stop_bits. These variables are all public, and can be easily changed. It is recommended that the users of the class make use of the constant variables included in Windows.h for these values, but any integer will work. For instance, to change the baud rate to 9600, use two stop bits, and enable parity, you could write:

Kubota b7001 manual pdf. I should point out that the definitions of these variables are in the MSDN documentation of the DCB structure (one of the things that this class attempts to encapsulate). The MSDN link to the article can be found here: MSDN.

Once you have configured the port the way you want it (or decided that the default values are acceptable), you simply set up the port with a call to set_serial_interface, passing the COM port you wish to use (COM1, COM2, and COM3 are constants available for convenience) and the serial_variables structure.

And, that's all there is to it! Now, you can get a pointer to the serial port handle using get_serial_handle and start reading and writing to the serial port. The code looks like this:

Brute forcing hacking. And, that's how you can set up a serial port in five lines of code.

Points of interest

Currently, the class does not support changing other factors of the configuration like timeouts or flow control. The reason for that is pretty simple: if you have to get access to those extra variables, then the encapsulation provided by this class is probably of little help to you, and you can use the standard Windows COM interface. The purpose of this class is to allow users with little knowledge of serial port configuration to get a COM port up and running quickly. It assumes that default values will be acceptable.

Also, the class makes use of the Singleton and p_impl* design patterns so that the software can only operate on one port at a time, and so that the user doesn't get lost in inaccessible functions if they are making use of the Visual Studio Intellisense feature.

Another feature of the class is that by making a call to set_serial_interface, the class will properly close out the current port and re-open with the new settings, allowing the user to configure the serial port on the fly, if needed.


This is the first release of this class, and I would welcome suggestions on any features that users would like to see implemented, or bugs that they have found. Feel free to contact me on CodeProject or on my regular blog. Thank you all.

Boost Serial Port Example

I'm working on a program which transmits data through serial port. I ended up making this code. It works good when run for the 1st time, afterwords some strange.

G-Eazy is an American rapper, songwriter and producer. He crossed over to the mainstream popularity with his re-interpolation of the rock & roll classic 'Runaround Sue.' • Post must be related to the G-Eazy only. RULES • Please follow • NO PERSONAL INFO For the sake of clarification: personal information does include emails, phone numbers and addresses. G-eazy epidemic lp zip. Arriving on-stage with a pompadour and a leather jacket, G-Eazy became known as 'the James Dean of rap', but his music is much less troubled than that name might imply.

Jason, If it is suitable for your application, I'd highly recommend implementing a callback-based asynchronous serial RX. Matrix algebra pdf. Has a great little example of how to implement asynch serial with a timeout. Guitar hero for pc free.

As you recognised, it will require a multi-threaded implementation to get the performance advantages, so you will need to put some thought where your recieved data will be buffered to make sure you aren't doing a lot of copying. As far as the boost::streambuff stuff goes, I personally prefer just to block out some memory as a char array - char mRXBuffermRXBuffSize and use boost::asio::buffer(mRXBuffer, mRXBuffSize) to pass the target buffer into asyncreadsome. In particular for RS232, I have always found the fact that the underlying data is a stream of bytes naturally maps a lot better onto a simple char array than any of the more complex data structures.