COM port not sending any information

Why doesn't this return the the information that my arduino is sending on COM3?
No errors are thrown.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <windows.h>
#include <stdio.h>
#include <iostream>

using namespace std;

void main()
{
	char buffer[] = "00000000 00000000";
	bool breakloop = false;

	HANDLE SerialRead;
	printf("Setup serial handle \n");

	SerialRead = CreateFile(
		"COM3",
		GENERIC_READ,
		0,
		0,
		OPEN_EXISTING,
		0,
		NULL
		);

	while (breakloop == false){
		if (GetAsyncKeyState(0x54)){
			cout << "T Is pressed" << endl;
			breakloop = true;   
		}
		if (GetAsyncKeyState(0x46)){
			ReadFile(
				    SerialRead,
					buffer,
					17,
					0,
					0
				);
			cout << buffer << endl;
			//cout << "Passed Varialbe" << endl;
			char buffer[] = "00000000 00000000";
		}

	}
	CloseHandle(SerialRead);
}
You should check whether CreateFile(...)/ReadFile(...) failed or not. If it fails use GetLastError() to determine what the problem is. For ReadFile(...) you should also check how many bytes are received. In a non blocking mode it might be very well 0 so that you need to check again.

Further more you need change some setting using SetCommState(...):

https://msdn.microsoft.com/en-us/library/windows/desktop/aa363436%28v=vs.85%29.aspx

So that it matches the settings on the arduino.
Especially: Baud, data bits, stop bits, parity. For instance: 9600, 8, N, 1
Try this code to see the error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <windows.h>
#include <stdio.h>
#include <iostream>
#include <sstream>

using namespace std;

string GetLastWinError ()
{
  LPVOID lpMsgBuf;
  LPVOID lpDisplayBuf;
  DWORD dwLastError = GetLastError ();

  FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, dwLastError,
    MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf,
    0, NULL);

  std::ostringstream oss;
  oss << "Windows Error ";
  oss << dwLastError << " :" << (LPCSTR)lpMsgBuf;

  LocalFree (lpMsgBuf);

  return oss.str ();

}


void main ()
{
  char buffer[] = "00000000 00000000";
  bool breakloop = false;
  HANDLE SerialRead;


  printf ("Setup serial handle \n");
  SerialRead = CreateFile ("COM3", GENERIC_READ, 0, 0, OPEN_EXISTING, 0, NULL);
  if (SerialRead == INVALID_HANDLE_VALUE)
  {
    cerr << "\a" << GetLastWinError () << "\n";
    system ("pause");
    exit (EXIT_FAILURE);
  }

  while (breakloop == false)
  {
    if (GetAsyncKeyState (0x54))
    {
      cout << "T Is pressed" << endl;
      breakloop = true;
    }
    if (GetAsyncKeyState (0x46))
    {
      if (!ReadFile (SerialRead, buffer, 17, 0, 0))
      {
        cerr << "\a" << GetLastWinError () << "\n";
        system ("pause");
        exit (EXIT_FAILURE);
      }
      cout << buffer << endl;
      //cout << "Passed Varialbe" << endl;
      char buffer[] = "00000000 00000000";
    }

  }
  CloseHandle (SerialRead);
  SerialRead = INVALID_HANDLE_VALUE;
  system ("pause");
}
Topic archived. No new replies allowed.