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.4KB

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