commw32.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2014 Quoc-Viet Nguyen. All rights reserved.
  2. // This software may be modified and distributed under the terms
  3. // of the BSD license. See the LICENSE file for details.
  4. // Test serial communication in Win32
  5. // gcc commw32.c
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <windows.h>
  9. static const char* port = "COM4";
  10. static printLastError() {
  11. char lpBuffer[256] = "?";
  12. FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
  13. NULL,
  14. GetLastError(),
  15. MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
  16. lpBuffer,
  17. sizeof(lpBuffer)-1,
  18. NULL);
  19. printf("%s\n", lpBuffer);
  20. }
  21. int main(int argc, char** argv) {
  22. HANDLE handle;
  23. DCB dcb = {0};
  24. COMMTIMEOUTS timeouts;
  25. DWORD n = 0;
  26. char data[512];
  27. int i = 0;
  28. handle = CreateFile(port,
  29. GENERIC_READ | GENERIC_WRITE,
  30. 0,
  31. 0,
  32. OPEN_EXISTING,
  33. 0,
  34. 0);
  35. if (handle == INVALID_HANDLE_VALUE) {
  36. printf("invalid handle %d\n", GetLastError());
  37. printLastError();
  38. return 1;
  39. }
  40. printf("handle created %d\n", handle);
  41. dcb.BaudRate = CBR_9600;
  42. dcb.ByteSize = 8;
  43. dcb.StopBits = ONESTOPBIT;
  44. dcb.Parity = NOPARITY;
  45. // No software handshaking
  46. dcb.fTXContinueOnXoff = 1;
  47. dcb.fOutX = 0;
  48. dcb.fInX = 0;
  49. // Binary mode
  50. dcb.fBinary = 1;
  51. // No blocking on errors
  52. dcb.fAbortOnError = 0;
  53. if (!SetCommState(handle, &dcb)) {
  54. printf("set comm state error %d\n", GetLastError());
  55. printLastError();
  56. CloseHandle(handle);
  57. return 1;
  58. }
  59. printf("set comm state succeed\n");
  60. // time-out between charactor for receiving (ms)
  61. timeouts.ReadIntervalTimeout = 1000;
  62. timeouts.ReadTotalTimeoutMultiplier = 0;
  63. timeouts.ReadTotalTimeoutConstant = 1000;
  64. timeouts.WriteTotalTimeoutMultiplier = 0;
  65. timeouts.WriteTotalTimeoutConstant = 1000;
  66. if (!SetCommTimeouts(handle, &timeouts)) {
  67. printf("set comm timeouts error %d\n", GetLastError());
  68. printLastError();
  69. CloseHandle(handle);
  70. return 1;
  71. }
  72. printf("set comm timeouts succeed\n");
  73. if (!WriteFile(handle, "abc", 3, &n, NULL)) {
  74. printf("write file error %d\n", GetLastError());
  75. printLastError();
  76. CloseHandle(handle);
  77. return 1;
  78. }
  79. printf("write file succeed\n");
  80. printf("Press Enter when ready for reading...");
  81. getchar();
  82. if (!ReadFile(handle, data, sizeof(data), &n, NULL)) {
  83. printf("read file error %d\n", GetLastError());
  84. printLastError();
  85. CloseHandle(handle);
  86. return 1;
  87. }
  88. printf("received data %d:\n", n);
  89. for (i = 0; i < n; ++i) {
  90. printf("%02x", data[i]);
  91. }
  92. printf("\n");
  93. CloseHandle(handle);
  94. printf("closed\n");
  95. return 0;
  96. }