Hello,

I'm new to C++ and I need help figuring out how to send information to the rs232 port. It's going to be one way communication (PC to device). I've tried reading other tutorials, but none of them clearly explain what's going on in each part, so I often get lost. Could some write up a simple example of sending a single byte of data and show where you set up the baud rate and other stuff. Also what's the best way to produce a time delay in C++?

Also I'm trying to create the program for both windows and Linux, so if you can, I'd appreciate it if you can show an example for both OS.

Thanks

I just have a few more questions.....

Here's the example code provided:

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

int main(int argc, char *argv[])
{
   DCB dcb;
   HANDLE hCom;
   BOOL fSuccess;
   TCHAR *pcCommPort = TEXT("COM2");

   hCom = CreateFile( pcCommPort,
                    GENERIC_READ | GENERIC_WRITE,
                    0,    // must be opened with exclusive-access
                    NULL, // default security attributes
                    OPEN_EXISTING, // must use OPEN_EXISTING
                    0,    // not overlapped I/O
                    NULL  // hTemplate must be NULL for comm devices
                    );

   if (hCom == INVALID_HANDLE_VALUE) 
   {
       // Handle the error.
       printf ("CreateFile failed with error %d.\n", GetLastError());
       return (1);
   }

   // Build on the current configuration, and skip setting the size
   // of the input and output buffers with SetupComm.

   SecureZeroMemory(&dcb, sizeof(DCB));
   dcb.DCBlength = sizeof(DCB);
   fSuccess = GetCommState(hCom, &dcb);

   if (!fSuccess) 
   {
      // Handle the error.
      printf ("GetCommState failed with error %d.\n", GetLastError());
      return (2);
   }

   // Fill in DCB: 57,600 bps, 8 data bits, no parity, and 1 stop bit.

   dcb.BaudRate = CBR_57600;     // set the baud rate
   dcb.ByteSize = 8;             // data size, xmit, and rcv
   dcb.Parity = NOPARITY;        // no parity bit
   dcb.StopBits = ONESTOPBIT;    // one stop bit

   fSuccess = SetCommState(hCom, &dcb);

   if (!fSuccess) 
   {
      // Handle the error.
      printf ("SetCommState failed with error %d.\n", GetLastError());
      return (3);
   }

   _tprintf (TEXT("Serial port %s successfully reconfigured.\n"), pcCommPort);
   return (0);
}

1. This code shows me how to set up the connection, now how do a send an array of bits (or bytes)?

2. Is the second parameter of createFile a boolean?

3. Is 'COM2' an array of characters?

TCHAR *pcCommPort = TEXT("COM2");

ignore #3... I thought it was ' ' instead of ""

Would this work...

FileWriteValue( hCom, {true,false,true,false,true,false,true,false} ) ;

would it send 10101010 ?

>>would it send 10101010 ?
No. And you want is WriteFile(), not FileWriteValue(), whatever that is.

>>2. Is the second parameter of createFile a boolean?
No. Read the description of each parameter here in MSDN

>>3. Is 'COM2' an array of characters?
Yes.

Well I figured out most the parameters, but one. I couldn't find the class (or what ever else it could be) LPCVOID. I know it's supposed to hold the data but what is LPCVOID?

So if I'm sending one byte, does this look correct....

WriteFile(hCom, ????, 1 , NULL, NULL)

>> I know it's supposed to hold the data but what is LPCVOID?
See this list of windows.h data types.

>>WriteFile(hCom, ????, 1 , NULL, NULL)
That is incorrect. Read very carefully about each of the parameters from here.

The second parameter is a pointer to the buffer that contains the data to be written. The next parameter is the number of bytes to be written. The next parameter is a pointer to a DWORD that will contain the number of bytes actually written when the function returns.

I'm finding the documentation to be frustrating, I'm so accustomed to Java documentation.

ok, so if I want to send the two bytes 01000000 10000000, is this correct

// by buffer I'm assuming they mean an array?
// and by number of bytes, I'm assuming the array size????
int[2] data = {2x0, 1x0};


WriteFile(hCom, data, 2 , NULL, NULL) ;

I'm sorry if I sound naive, but it's been forever since I did C and Java is so much more easier than this.

>>WriteFile(hCom, data, 2 , NULL, NULL) ;
The third parameter is wrong -- it is supposed to be the number of BYTES, not the number of integers. To send two integers, do it like this: WriteFile(hCom, (char*)data, sizeof(data), NULL, NULL) ; If you only want to send 2 bytes instead of 2 integers

char data[2] = {0x01, 0x02};
DWORD dwBytesWritten = 0;
WriteFile(hCom, (char*)data, sizeof(data), &dwBytesWritten, NULL) ;

And as I said before, THE FOURTH PARAMETER CAN NOT BE NULL.

Thanks

Is it just me, or does it seem that c++ is unfriendlier than Java. -_-

Thanks

Is it just me, or does it seem that c++ is unfriendlier than Java. -_-

Its just you :) I don't know the first thing about java so I don't really know. But you will find such differences in every computer language.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.