I know. Not exacly an Arduino specific topic. Nonetheless I'll try...
I've been trying to establish a serial link between my Arduino and a c++/DirectX application I'm making. I can't get the c++ code to read the serial output from Arduino properly...
My Arduino is intermittently sending out bytes of 0's and 1's like this:
void setup()
{
Serial.begin(115200);
}
void loop()
{
Serial.print(0xff, BYTE);
Serial.print(0x00, BYTE);
}
The processing sketch below prints out 0 255 0 255 0....as it should:
import processing.serial.*;
Serial port;
int val;
void setup()
{
port = new Serial(this, Serial.list()[1], 115200);
}
void draw() {
val = port.read();
println(val);
}
However I have been trying to do the same using C++ (Visual C++ 2008 Express) based on these instruction:
And after HOURS of struggle I've been able to OPEN the COM port, detect that data is being send and OCCASIONALLY get one or two bytes of correct data:255 and 0 (= 0xff and 0x00). But most of the time I get 15 and 248 (= 0x0f and 0xf8) or other crap data.
I've been trying varios ways...Here is the simple way:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <commdlg.h>
#include <windef.h>
#define READ_TIMEOUT 500
void main()
{
TCHAR* currentuser = L"COM8";
LPCWSTR *lstr = (LPCWSTR *)(¤tuser);
HANDLE hComm;
DWORD dwRead;
OVERLAPPED osReader = {0};
DWORD dwRes;
hComm = CreateFile( *lstr,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if (hComm == INVALID_HANDLE_VALUE) printf("*Error opening port\n");
else printf("*Port opened succesfully\n");
osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (osReader.hEvent == NULL)
printf("*Error creating overlapped event; abort\n");
BYTE inByte = 1;
for(int i=0; i<100; i++) {
if(!ReadFile(hComm,&inByte,1,&dwRead,&osReader))
dwRes = WaitForSingleObject(osReader.hEvent, READ_TIMEOUT);
printf("%02x\n", inByte);
}
CloseHandle(osReader.hEvent);
// Wait for user
int a;
scanf("%d", &a);
}
...And here is the more complicated version:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <commdlg.h>
#include <windef.h>
#define READ_TIMEOUT 500
#define READ_BUF_SIZE 200
void HandleASuccessfulRead(char lpBuf[], DWORD dwRead);
void main()
{
TCHAR* currentuser = L"COM8"; // L"\\\\.\\COM8";
LPCWSTR *lstr = (LPCWSTR *)(¤tuser);
HANDLE hComm;
char lpBuf[READ_BUF_SIZE];
for(int i=0; i<READ_BUF_SIZE; i++) lpBuf[i]=0x01;
DWORD dwRead;
BOOL fWaitingOnRead = FALSE;
OVERLAPPED osReader = {0};
DWORD dwRes;
hComm = CreateFile( *lstr,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if (hComm == INVALID_HANDLE_VALUE) printf("*Error opening port\n");
else printf("*Port opened succesfully\n");
// Create the overlapped event. Must be closed before exiting
// to avoid a handle leak.
osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (osReader.hEvent == NULL)
printf("*Error creating overlapped event; abort\n");
if (!fWaitingOnRead) {
printf("*Issue read operation\n");
if (!ReadFile(hComm, lpBuf, READ_BUF_SIZE, &dwRead, &osReader)) {
if (GetLastError() != ERROR_IO_PENDING) // read not delayed?
printf("*Error in communications\n");
else {
fWaitingOnRead = TRUE;
printf("*Waiting on read\n");
}
}
else {
printf("*Read completed immediately\n");
HandleASuccessfulRead(lpBuf, dwRead);
}
}
if (fWaitingOnRead) {
dwRes = WaitForSingleObject(osReader.hEvent, READ_TIMEOUT);
switch(dwRes)
{
// Read completed.
case WAIT_OBJECT_0:
if (!GetOverlappedResult(hComm, &osReader, &dwRead, FALSE))
printf("*Error in communications\n");
else
// Read completed successfully.
HandleASuccessfulRead(lpBuf, dwRead);
// Reset flag so that another opertion can be issued.
fWaitingOnRead = FALSE;
break;
case WAIT_TIMEOUT:
// Operation isn't complete yet. fWaitingOnRead flag isn't
// changed since I'll loop back around, and I don't want
// to issue another read until the first one finishes.
// This is a good time to do some background work.
break;
default:
// Error in the WaitForSingleObject; abort.
// This indicates a problem with the OVERLAPPED structure's
// event handle.
break;
}
}
// Wait for user
int a;
scanf("%d", &a);
}
void HandleASuccessfulRead(char lpBuf[], DWORD dwRead) {
printf("*Handle succesfull read\n");
byte b;
for(int i=0; i<READ_BUF_SIZE; i++) {
b = lpBuf[i];
printf("%02x ", b);
}
}
I was hoping that someone here could give me a hint?!