The online racing simulator
#1 - VeGe-
Visual C++ 2010 Express - Serial Communication
I've been googling and researching for hours how to get my console Visual C++ Express program to transmit simple ASCII data to serial port. And without any success.

MSDN library doesn't help me as the example is unusable. I think it's for Express 2008. Everything else I've found is too old or just so complicated that I can't understand a word.

All I want to do is set up my serial port settings (COM3 57600 8-N-2, no handshake) and send few strings through serial port. How hard can it be?

Code is only slighty modified from DarkTimes's excellent tutorial.

#include <iostream>
#include <winsock2.h> // WinSock2 library (may need to add WS2_32.lib to project dependencies).

#define BUFFER_SIZE 512 // Receive buffer size (basically maximum packet size in UDP).
#define HOST "127.0.0.1" // Host to connect to.
#define PORT 35555 // Port to connect to the host through.

// Define types used by InSim.
typedef unsigned char byte;
typedef unsigned short word;
typedef struct
{
int X, Y, Z;
} Vec;
typedef struct
{
float X, Y, Z;
} Vector;

// Include InSim header.
#include "InSim.h"

void OutGaugePacketReceived(const OutGaugePack packet);

int main(int argc, char *argv[])
{
// Initialise WinSock version 2.2.
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
WSACleanup();
std::cerr << "Error: Failed to init WinSock" << std::endl;
return EXIT_FAILURE;
}

// Create UDP socket.
SOCKET sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock == INVALID_SOCKET)
{
WSACleanup();
std::cerr << "Error: Could not create socket." << std::endl;
return EXIT_FAILURE;
}

// Bind to receive UDP packets.
sockaddr_in saddr;
saddr.sin_family = AF_INET;
saddr.sin_addr.s_addr = inet_addr(HOST);
saddr.sin_port = htons(PORT);
if (bind(sock, (sockaddr *)&saddr, sizeof(sockaddr)) == SOCKET_ERROR)
{
closesocket(sock);
WSACleanup();
std::cerr << "Error: Could not connect to LFS" << std::endl;
return EXIT_FAILURE;
}

// Packet receive loop.
char recvbuf[BUFFER_SIZE];
memset(recvbuf, 0, sizeof(recvbuf)); // Set recvbuf to zero.
int bytes = 0;
do
{
bytes = recv(sock, recvbuf, BUFFER_SIZE, 0);
if (bytes > 0)
OutGaugePacketReceived((OutGaugePack &)recvbuf);
else if (bytes == 0)
std::cerr << "Error: Lost connection with LFS" << std::endl;
else
std::cerr << "Error: " << WSAGetLastError() << std::endl;
} while (bytes > 0);

// Cleanup and exit.
closesocket(sock);
WSACleanup();
return EXIT_SUCCESS;
}

void OutGaugePacketReceived(const OutGaugePack packet)
{
unsigned mask = 1 << DL_SHIFT;
if (packet.ShowLights & mask)
{
std::cout << "Shift light on!" << std::endl;
}

std::cout << (int)packet.RPM << std::endl;
}

The (int)packet.RPM shows revs beautifully in the console, I just need to put it through Serial Port along with speed and various other data. This wasn't a problem for me in VB, but out dated OutGauge lib forced me to look another ways to do this.
The example you linked to is for C++ .NET, while the OutGauge code is written for native C++. I've never done any serial port programming before, but with a quick search I found some examples and a PDF tutorial on the subject. Those should get you started for how to do it without .NET.
#3 - VeGe-
Ah, ok, I see. I'm kinda confused with whole .NET. It's C++ but then it isn't C++...

Tried that PDF tutorial but it was overwhelming. That other "some example" looked very nice easy-to-use library but it was written in plain C and it never compiled without great amount of cryptical errors. I'm not capable of modifying it so it would actually work.


\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(288): error C2065: 'port_settings' : undeclared identifier
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(288): warning C4133: 'function' : incompatible types - from 'int *' to 'LPDCB'
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(295): error C2065: 'port_settings' : undeclared identifier
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(295): warning C4133: 'function' : incompatible types - from 'int *' to 'LPDCB'
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(302): error C2275: 'COMMTIMEOUTS' : illegal use of this type as an expression
c:\program files (x86)\microsoft sdks\windows\v7.0a\include\winbase.h(755) : see declaration of 'COMMTIMEOUTS'
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(302): error C2146: syntax error : missing ';' before identifier 'Cptimeouts'
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(302): error C2065: 'Cptimeouts' : undeclared identifier
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(304): error C2065: 'Cptimeouts' : undeclared identifier
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(304): error C2224: left of '.ReadIntervalTimeout' must have struct/union type
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(305): error C2065: 'Cptimeouts' : undeclared identifier
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(305): error C2224: left of '.ReadTotalTimeoutMultiplier' must have struct/union type
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(306): error C2065: 'Cptimeouts' : undeclared identifier
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(306): error C2224: left of '.ReadTotalTimeoutConstant' must have struct/union type
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(307): error C2065: 'Cptimeouts' : undeclared identifier
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(307): error C2224: left of '.WriteTotalTimeoutMultiplier' must have struct/union type
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(308): error C2065: 'Cptimeouts' : undeclared identifier
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(308): error C2224: left of '.WriteTotalTimeoutConstant' must have struct/union type
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(310): error C2065: 'Cptimeouts' : undeclared identifier
\visual studio 2010\projects\outgauge\outgaugetest\rs232.c(310): warning C4133: 'function' : incompatible types - from 'int *' to 'LPCOMMTIMEOUTS'

So still no success for me. I would love to find that sort of easy to use library for C++.
#4 - VeGe-
Tried the C-version with MinGW. It compiles great but for some reason I only get error that it's unable to open port. Maybe something to do with Windows 7 I'm using? But very easy to use:

#include <stdio.h>
#include <stdlib.h>
#include "rs232.h"

int main()
{
OpenComport(3, 9600);

SendByte(3, 'S');

char i;

for(i=0x30;i<0x40;i++){
SendByte(3, i);
}

CloseComport(3);
}

Wish there were something like this for C++ too...
#6 - Stuff
You should be able to mix C and C++ with no problems. I'm still new at everything but a lot of the examples I see do it all the time. For example, strcpy, strlen, printf, etc are all C functions yet they're in a C++ app with OOP and templates.. Hmm

One valuable thing I've done recently is start using the Boost libraries for a lot of common things. They even have one for serial port communications that may work for ya. Found that by searching Stack Overflow.
#8 - VeGe-
Thanks for the help! Got over the problem using C#, found it easier than C++. Maybe I'll try C++ some day and hopefully this thread can help some one else too!
I just answered 1 question and got 11 Rep

FGED GREDG RDFGDR GSFDG