Simple single-color 8x8x8 LED Cube with AVRs
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

winSerial.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_38400;
  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. int availableSerialPorts(char *ports) {
  88. int i, c = 0;
  89. char portName[6];
  90. for (i = 0; i < 20; i++) {
  91. sprintf(portName, "COM%d", i + 1);
  92. if (serialOpen(portName) == 0) {
  93. // success
  94. ports[i] = 1;
  95. c++;
  96. serialClose();
  97. } else {
  98. ports[i] = 0;
  99. }
  100. }
  101. return c;
  102. }
  103. // Last element has to be NULL
  104. char** getSerialPorts(const char *search) {
  105. int i, num, c = 0, s;
  106. char ports[20];
  107. char **portList;
  108. num = availableSerialPorts(ports);
  109. portList = (char **)malloc((num + 1) * sizeof(char *));
  110. for (i = 0; i < 20; i++) {
  111. // if ports[n] == 1 -> COMn+1 does exist
  112. if (ports[i] != 0) {
  113. if (i < 9) {
  114. s = 5;
  115. } else {
  116. s = 6;
  117. }
  118. portList[c] = (char *)malloc(s * sizeof(char));
  119. sprintf(portList[c], "COM%d", i + 1);
  120. c++;
  121. }
  122. }
  123. portList[c] = NULL;
  124. return portList;
  125. }