Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

winSerial.c 3.4KB

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