• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
JonBMN

Serial Communication using C++ HELP!

11 posts in this topic

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

Share this post


Link to post
Share on other sites
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:
[code]
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))
...
[/code]

If it's another OS, then someone else will have to answer you.
2

Share this post


Link to post
Share on other sites
Yes, a Serial port communication program, on windows. I need a library to link to for correct syntax's
0

Share this post


Link to post
Share on other sites
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.

[CODE]
// 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;
}
[/CODE]
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
If you're looking for a library that allow your code to be portable, you could try [url="http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio.html"]Boost.Asio[/url]. 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.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1343767247' post='4964988']
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?
[/quote]

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!
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0