serial communication with c++

Hi everyone, I just want to send a single byte to the serial port using c++. Has anyone figured out a simple way to do this? I downloaded the CSerial class at Serial library for C++ - CodeProject

my c++ code is the following:

#define STRICT
#include <tchar.h>

#include <windows.h>

#include "Serial.h"


int WINAPI _tWinMain
          (
           HINSTANCE /*hInst*/, 
           HINSTANCE /*hInstPrev*/, 
           LPTSTR    /*lptszCmdLine*/, 
           int       /*nCmdShow*/
          )
{
    CSerial serial;

    // Attempt to open the serial port (COM1)

    serial.Open(_T("COM3"));

    // Setup the serial port (9600,N81) using hardware handshaking

    serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
    serial.SetupHandshaking(CSerial::EHandshakeOff);

   

    serial.Write("k");

    // Close the port again

    serial.Close();
    return 0;
}

I can tell that the code is connecting to the arduino because the Tx Led lights up briefly when I run my code, however it's not sending the character k to the arduino. Can anyone please help me to easily send one byte over the serial port using c++? I would greatly appreciate it.
Thanks in advance! :wink:

The Arduino is busy resetting and running the bootloader for a short while when the port is first opened. It's possible your command is being lost because it is sent right after the .open(), during this window.

If this is in fact the issue, pausing for a short delay (a couple hundred ms) after the open and before sending the command may help.

-br

http://entropymouse.com

i put a system("pause") right after serial.Open(_T("COM3")); and that still didn't work.

Also I wasn't asking for an easy way to connect to the serial port, i know bitlash is very easy use. What I was asking for was a simple c++ code that would allow me to send a byte over the serial port. I only want to use c++ code to connect to the serial port .

You can use the APIs CreateFile and ReadFile/WriteFile for reading/writing a comport.

I only want to use c++ code to connect to the serial port

A c++ coding forum (or google) might be the place to start.

i put a system("pause") right after serial.Open(_T("COM3")); and that still didn't work.

It's been awhile for me but doesn't system("PAUSE") wait for user input before moving on? Or are you actually giving it input and still not getting anything afterward?

Just asking the easiest things first since they tend to be the fastest to check.

While we're asking questions, shouldn't these:

    serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
    serial.SetupHandshaking(CSerial::EHandshakeOff);

come before:

    serial.Open(_T("COM3"));

Once the port is opened, I'm not sure you can set the baud rate, etc. Typically, those are set before the port is opened.

Pretty sure that was fine the way it was, you need a handle to the comport first before you can set anything else. As mentioned before, don't use system("pause") but ::Sleep(ms) for example where ms=milliseconds.