Serial C++

Hi, I have no idea how to start programming C++ for a serial port.
I need some help/ examples for me to start. I tried looking through the net but I have no luck.
I have previously written some simple C++ programs that I can use the keyboard or scanner.

I have a PLC connected through RS232-USB cable to my PC (COM3).
It will send a simple string text like 12A456789, 1288U
It is transmitted through 9600 speed, 8 data bits, 1 stop bit, even parity and flow control none.

Once the program receives the string text, I need to do a simple comparison and save the text to a txt file according to the content received. EG, if contains 1288U, save as 1288U SN.txt. if contains 1288I, save as 1288I SN.txt

If possible, once the save is completed, the program will send a confirmed message to the PLC (COM3).
Ultimately, I will use bartender commander to extract the txt file for label printing.

Any kind souls can help me with the serial port portion of the C++ program? It will be greatly appreciated. Thanks.
You might look at boost:

http://www.boost.org/doc/libs/1_49_0/doc/html/boost_asio/reference.html

looks a bit over complicated though.


When you say COM3 it's certainly Windows.
You can open a serial port like a file:

1
2
3
DWORD dwDesiredAccess = GENERIC_READ | GENERIC_WRITE
CreateFile("COM3", dwDesiredAccess, 0, 0,
      OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)


The required settings (like speed, data bits ...) you set with SetCommState():

http://msdn.microsoft.com/en-us/library/ms886806.aspx
I might be wrong, but when you send strings like that on the serial port, the protocol is generally ASCII, so you might need to set your serial port to 7 data bits
I've been using boost.asio for serial port communication for several years, I find it easier than other ways.
Topic archived. No new replies allowed.