Simple single-color 8x8x8 LED Cube with AVRs
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.

winSerial.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. *
  3. * winSerial.c
  4. *
  5. * Windows 16 (& 32 & 64?) compatible serial port library
  6. * Uses 8 databits, no parity, 1 stop bit, no handshaking
  7. *
  8. * Copyright 2011 Thomas Buck <xythobuz@me.com>
  9. * Copyright 2011 Max Nuding <max.nuding@gmail.com>
  10. * Copyright 2011 Felix Bäder <baeder.felix@gmail.com>
  11. *
  12. * This file is part of LED-Cube.
  13. *
  14. * LED-Cube is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * LED-Cube is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  26. */
  27. #include <stdio.h>
  28. #include <windows.h>
  29. #define BAUD CBR_19200;
  30. HANDLE hSerial = INVALID_HANDLE_VALUE;
  31. // Open the serial port
  32. int serialOpen(char *port) {
  33. DCB dcbSerialParams = {0};
  34. COMMTIMEOUTS timeouts = {0};
  35. hSerial = CreateFile(port, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  36. if (hSerial == INVALID_HANDLE_VALUE) {
  37. return -1;
  38. }
  39. dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
  40. if (!GetCommState(hSerial, &dcbSerialParams)) {
  41. CloseHandle(hSerial);
  42. hSerial = INVALID_HANDLE_VALUE;
  43. return -1;
  44. }
  45. dcbSerialParams.BaudRate = BAUD;
  46. dcbSerialParams.ByteSize = 8;
  47. dcbSerialParams.StopBits = ONESTOPBIT;
  48. dcbSerialParams.Parity = NOPARITY;
  49. if (!SetCommState(hSerial, &dcbSerialParams)) {
  50. CloseHandle(hSerial);
  51. hSerial = INVALID_HANDLE_VALUE;
  52. return -1;
  53. }
  54. timeouts.ReadIntervalTimeout = 1000;
  55. timeouts.ReadTotalTimeoutConstant = 1000;
  56. timeouts.ReadTotalTimeoutMultiplier = 1000;
  57. timeouts.WriteTotalTimeoutConstant = 1000;
  58. timeouts.WriteTotalTimeoutMultiplier = 1000;
  59. if (!SetCommTimeouts(hSerial, &timeouts)) {
  60. CloseHandle(hSerial);
  61. hSerial = INVALID_HANDLE_VALUE;
  62. return -1;
  63. }
  64. return 0;
  65. }
  66. // Write to port. Returns number of characters sent, -1 on error
  67. ssize_t serialWrite(char *data, size_t length) {
  68. DWORD dwBytesWritten = 0;
  69. if (!WriteFile(hSerial, data, length, &dwBytesWritten, NULL)) {
  70. return -1;
  71. }
  72. return dwBytesWritten;
  73. }
  74. // Read from port. Return number of characters read, 0 if none available, -1 on error
  75. ssize_t serialRead(char *data, size_t length) {
  76. DWORD dwBytesRead = 0;
  77. if (!ReadFile(hSerial, data, length, &dwBytesRead, NULL)) {
  78. return -1;
  79. }
  80. return dwBytesRead;
  81. }
  82. // Close the serial Port
  83. void serialClose(void) {
  84. CloseHandle(hSerial);
  85. hSerial = INVALID_HANDLE_VALUE;
  86. }
  87. // Last element has to be NULL
  88. char** getSerialPorts(void) {
  89. LPTSTR ports;
  90. DWORD num;
  91. char **files;
  92. int i, j = 0, start, k;
  93. #ifdef UNICODE
  94. ports = (LPTSTR)malloc(100 * sizeof(WCHAR));
  95. #else
  96. ports = (LPTSTR)malloc(100 * sizeof(CHAR));
  97. #endif
  98. num = QueryDosDevice(NULL, ports, 100);
  99. files = (char **)malloc(num * sizeof(char *));
  100. for (i = 0; i < num; i++) {
  101. start = j;
  102. j = 0;
  103. while (ports[start + j] != '\0') {
  104. j++;
  105. }
  106. j++; // \0
  107. files[i] = (char *)malloc(j * sizeof(char));
  108. for (k = 0; k < j; k++) {
  109. files[i] = ports[start + k];
  110. }
  111. }
  112. return files;
  113. }