Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

177 lines
3.6 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #define NUM 128
  4. DWORD main(int argc, char *argv[], char *envp[])
  5. {
  6. CHAR chBuffer[128];
  7. CHAR RdBuffer[128];
  8. DWORD UseBaud = 9600;
  9. WORD i;
  10. HANDLE hCommPort;
  11. DCB dcb;
  12. BOOL bRc;
  13. DWORD dwNumWritten,dwNumRead,dwErrors;
  14. envp;
  15. if (argc > 1)
  16. {
  17. sscanf(argv[1],"%d",&UseBaud);
  18. }
  19. printf("\n\n *** COMM TEST START ***\n\n");
  20. printf("Opening the comm port for read write\n");
  21. hCommPort = CreateFile(
  22. "COM1",
  23. GENERIC_READ|GENERIC_WRITE,
  24. 0, // exclusive
  25. NULL, // sec attr
  26. OPEN_EXISTING,
  27. 0, // no attributes
  28. NULL); // no template
  29. if (hCommPort == (HANDLE)-1)
  30. {
  31. printf("FAIL: OpenComm failed rc: %lx\n",hCommPort);
  32. return -1;
  33. }
  34. printf("Opening the comm port for read write: SUCCESS hCommPort=%lx\n",hCommPort);
  35. printf("Setting the line characteristics 9600,8,N,1 on comm \n");
  36. dcb.DCBlength = sizeof(DCB);
  37. // dcb.DCBversion = 0x0002; BUG BUG in spec not in header
  38. if (!GetCommState(hCommPort,&dcb))
  39. {
  40. printf("FAIL: Couldn't get the dcb: %d\n",GetLastError());
  41. return FALSE;
  42. }
  43. dcb.BaudRate = UseBaud;
  44. dcb.ByteSize = 8;
  45. dcb.Parity = NOPARITY;
  46. dcb.StopBits = ONESTOPBIT;
  47. bRc = SetCommState(hCommPort,&dcb);
  48. if (!bRc)
  49. {
  50. printf("FAIL: cannot set the comm state rc:%lx\n",bRc);
  51. return -1;
  52. }
  53. printf("Setting the line characteristics 9600,8,N,1 on comm: SUCCESS\n");
  54. printf("Filling the buffer with the known chars \n");
  55. for (i=0; i< NUM; i++)
  56. {
  57. //chBuffer[i] = 'a';
  58. chBuffer[i] = (CHAR)i;
  59. }
  60. printf("Filling the buffer with the known chars : SUCCESS\n");
  61. printf("Dumping the buffer before sending it to comm\n");
  62. for (i=0; i< NUM; i++)
  63. {
  64. //printf("%c",RdBuffer[i]);
  65. printf(" %d ",chBuffer[i]);
  66. }
  67. printf("\nDumping the buffer before sending it to comm SUCCESS\n");
  68. printf("Filling the Rdbuffer with the known chars (0xFF) to makeit dirty\n");
  69. for (i=0; i< NUM; i++)
  70. {
  71. RdBuffer[i] = 0xFF;
  72. }
  73. printf("Filling the Rdbuffer with the known chars (0xFF): SUCCESS\n");
  74. printf("Writting this buffer to the comm port\n");
  75. bRc = WriteFile( hCommPort,
  76. chBuffer,
  77. NUM,
  78. &dwNumWritten,
  79. NULL);
  80. if (!bRc)
  81. {
  82. printf("FAIL: cannot Write To the comm port:%lx\n",bRc);
  83. return -1;
  84. }
  85. printf("Writting this buffer to the comm port: SUCCESS rc:%lx, byteswritten:%lx\n",
  86. bRc,dwNumWritten);
  87. printf("Reading this buffer from the comm port\n");
  88. bRc = ReadFile( hCommPort,
  89. RdBuffer,
  90. NUM,
  91. &dwNumRead,
  92. NULL);
  93. if (!bRc)
  94. {
  95. printf("FAIL: cannot Read From the comm port:%lx\n",bRc);
  96. return -1;
  97. }
  98. printf("Reading this buffer from the comm port: SUCCESS rc:%lx, bytesread:%lx\n",
  99. bRc,dwNumRead);
  100. printf("Dumping the Rdbuffer with the comm data\n");
  101. for (i=0; i< NUM; i++)
  102. {
  103. //printf("%c",RdBuffer[i]);
  104. printf(" %d ",RdBuffer[i]);
  105. }
  106. printf("\nDumping the Rdbuffer with the comm data: SUCCESS\n");
  107. printf("Closing the comm port\n");
  108. bRc = ClearCommError(hCommPort,&dwErrors,NULL);
  109. printf("ClearCommError: rc= %lx and dwErrors=%lx\n",bRc,dwErrors);
  110. bRc = CloseHandle(hCommPort);
  111. if (!bRc)
  112. {
  113. printf("FAIL: cannot close the comm port:%lx\n",bRc);
  114. return -1;
  115. }
  116. printf("\n\n*** COMM TEST OVER*** \n\n");
  117. return 0;
  118. }