Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


Serial Communication using C++ HELP!


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 mKatz   Members   -  Reputation: 729

Like
0Likes
Like

Posted 31 July 2012 - 12:04 PM

I'm creating a serial communication program and need a good library, any suggestions?

Sponsor:

#2 BeerNutts   Crossbones+   -  Reputation: 2978

Like
2Likes
Like

Posted 31 July 2012 - 12:38 PM

What do you mean by "serial communication program"? Do you mean serial port communication? If so, what OS are you using? Windows's API is to open it using the COM name, and build the DCB data, like this:
    DCB dcb;
    HANDLE hComm;
    hComm = CreateFile( "COM1", 
					    GENERIC_READ | GENERIC_WRITE,
					    0,
					    0,
					    OPEN_EXISTING,
					    FILE_FLAG_OVERLAPPED,
					    0);
					   
    if (hComm == INVALID_HANDLE_VALUE)
    {
	    // error opening port; abort
	    printf("Error opening %s for communicating!\n", "COM1");
	    exit(2);
    }
   
   
    FillMemory(&dcb, sizeof(dcb), 0);
    dcb.DCBlength = sizeof(dcb);
  
    if (!BuildCommDCB("115200,n,8,1", &dcb))
...

If it's another OS, then someone else will have to answer you.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#3 mKatz   Members   -  Reputation: 729

Like
0Likes
Like

Posted 31 July 2012 - 12:44 PM

Yes, a Serial port communication program, on windows. I need a library to link to for correct syntax's

#4 mKatz   Members   -  Reputation: 729

Like
0Likes
Like

Posted 31 July 2012 - 02:06 PM

Here is my code, now I'm not the greatest at this, but any help would be greatly appreciated. I'm trying to connect to COM3 and it won't connect. I've attached my code, along with the error message.

// serial.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}


#undef UNICODE
#include "stdafx.h"
#include <windows.h>



void PrintError( LPCSTR str)
{
LPVOID lpMessageBuffer;
int error = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //The user default language
(LPTSTR) &lpMessageBuffer,
0,
NULL
);
printf("%s: (%d) %s\n\n",str,error,lpMessageBuffer);
LocalFree( lpMessageBuffer );
}

int main(int argc, char* argv[])
{
// open port for I/O
HANDLE h = CreateFile(argv[1],
GENERIC_READ|GENERIC_WRITE,
0,NULL,
OPEN_EXISTING,0,NULL);

if(h == INVALID_HANDLE_VALUE) {
PrintError("E012_Failed to open port");
} else {
// set timeouts
COMMTIMEOUTS cto = { 1, 100, 1000, 0, 0 };
DCB dcb;
if(!SetCommTimeouts(h,&cto))
PrintError("E013_SetCommTimeouts failed");

// set DCB
memset(&dcb,0,sizeof(dcb));
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = 19200;
dcb.fBinary = 1;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fOutxCtsFlow = 1;
dcb.fRtsControl = DTR_CONTROL_HANDSHAKE;

dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;

if(!SetCommState(h,&dcb))
PrintError("E014_SetCommState failed");


char buf[7];
DWORD read = 0;
DWORD write=1; // Number of bytes to write to serial port
buf[0] = 72; // Decmial value to write to serial port
WriteFile(h,buf,write,&write,NULL); // write is updated with the number of bytes written

ReadFile(h,buf,sizeof(buf),&read,NULL); // read is updated with the number of bytes read
DWORD i;
for (i=0; i<read; i++)
printf("%i ", (unsigned char)buf[i]);

CloseHandle(h);
}
system("pause");
return 0;
}

Attached Thumbnails

  • comm.jpg


#5 ApochPiQ   Moderators   -  Reputation: 16069

Like
0Likes
Like

Posted 31 July 2012 - 02:34 PM

What file name are you using to try to open the port?

#6 mKatz   Members   -  Reputation: 729

Like
0Likes
Like

Posted 31 July 2012 - 02:37 PM

Can you explain the question a little more?

#7 ApochPiQ   Moderators   -  Reputation: 16069

Like
0Likes
Like

Posted 31 July 2012 - 02:40 PM

Your code is using the first argument passed on the command line as the name of the file it's trying to open to read the serial port. What are you specifying for the value of that argument?

#8 BeerNutts   Crossbones+   -  Reputation: 2978

Like
0Likes
Like

Posted 31 July 2012 - 03:10 PM

I'm assuming you're using "COM3" as arg[1]? If so, do you have another program already using COM3? Are you sure COM3 is a valid com port on your machine?
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#9 ChaosEngine   Crossbones+   -  Reputation: 2449

Like
0Likes
Like

Posted 01 August 2012 - 03:53 PM

If you're looking for a library that allow your code to be portable, you could try Boost.Asio. It has support for serial comms but I haven't used it in years and it may be overkill for what you are trying to achieve.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

#10 mKatz   Members   -  Reputation: 729

Like
0Likes
Like

Posted 02 August 2012 - 02:06 PM

yes BeerNutts I am, but no, I'm very new to this and I'm very sure its a valid com port. I've used putty to connect to it and was able to communicate.

ChaosEngine, thank you for your post, I will look into soon.

Edited by JonBMN, 02 August 2012 - 02:07 PM.


#11 mKatz   Members   -  Reputation: 729

Like
0Likes
Like

Posted 02 August 2012 - 02:09 PM

Your code is using the first argument passed on the command line as the name of the file it's trying to open to read the serial port. What are you specifying for the value of that argument?


I figured out this is more of a library so i'll have to create a program that uses this. Not sure if I'm even going to keep it rather than just write a brand new code library with program. Any more help would be amazing though and its only help me learn!

#12 ApochPiQ   Moderators   -  Reputation: 16069

Like
0Likes
Like

Posted 02 August 2012 - 05:21 PM

What happens if you attempt to open COM1? What happens if you enter "type com1" at a command prompt?




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS