Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.76 KB | None | 0 0
  1. /*
  2. * Main.c
  3. *
  4. * Created on: 17 jun. 2019
  5. * Author: Gebruiker
  6. */
  7.  
  8. /*
  9. * Main.c
  10. *
  11. * Created on: 3 jun. 2019
  12. * Author: Gebruiker
  13. */
  14.  
  15. //====================================================================================================//
  16. // Serial Port Programming using Win32 API in C //
  17. // (Writes data to serial port) //
  18. //====================================================================================================//
  19. //====================================================================================================//
  20. // www.xanthium.in //
  21. // Copyright (C) 2014 Rahul.S //
  22. //====================================================================================================//
  23. //====================================================================================================//
  24. // The Program runs on the PC side and uses Win32 API to communicate with the serial port or //
  25. // USB2SERIAL board and writes the data to it. //
  26. //----------------------------------------------------------------------------------------------------//
  27. // Program runs on the PC side (Windows) and transmits a single character. //
  28. // Program uses CreateFile() function to open a connection serial port(COMxx). //
  29. // Program then sets the parameters of Serial Comm like Baudrate,Parity,Stop bits in the DCB struct. //
  30. // After setting the Time outs,the Program writes a character to COMxx using WriteFile(). //
  31. //----------------------------------------------------------------------------------------------------//
  32. // BaudRate -> 9600 //
  33. // Data formt -> 8 databits,No parity,1 Stop bit (8N1) //
  34. // Flow Control -> None //
  35. //====================================================================================================//
  36. //====================================================================================================//
  37. // Compiler/IDE : Microsoft Visual Studio Express 2013 for Windows Desktop(Version 12.0) //
  38. // : gcc 4.8.1 (MinGW) //
  39. // Library : Win32 API,windows.h, //
  40. // Commands : gcc -o USB2SERIAL_Write_W32 USB2SERIAL_Write_W32.c //
  41. // OS : Windows(Windows 7) //
  42. // Programmer : Rahul.S //
  43. // Date : 30-November-2014 //
  44. //====================================================================================================//
  45. //====================================================================================================//
  46. // Sellecting the COM port Number //
  47. //----------------------------------------------------------------------------------------------------//
  48. // Use "Device Manager" in Windows to find out the COM Port number allotted to USB2SERIAL converter- //
  49. // -in your Computer and substitute in the "ComPortName[]" array. //
  50. // //
  51. // for eg:- //
  52. // If your COM port number is COM32 in device manager(will change according to system) //
  53. // then //
  54. // char ComPortName[] = "\\\\.\\COM32"; //
  55. //====================================================================================================//
  56. #include "Keyboard.h"
  57. #include <stdio.h>
  58. #include <conio.h>
  59. #include <Windows.h>
  60. #include <string.h>
  61.  
  62. HANDLE hComm; // Handle to the Serial port
  63. BOOL Status;
  64. char ComPortName[] = "COM1"; // Name of the Serial port(May Change) to be opened,
  65.  
  66. void initialize() {
  67.  
  68. printf("\n +==========================================+");
  69. printf("\n | TheOrder66 presents: Robot Run |");
  70. printf("\n +==========================================+");
  71. printf("\n");
  72. printf("Voer Compoort in 'COMX':\n");
  73. scanf("%s", ComPortName);
  74. /*----------------------------------- Opening the Serial Port --------------------------------------------*/
  75.  
  76. hComm = CreateFile(ComPortName, // Name of the Port to be Opened
  77. GENERIC_READ | GENERIC_WRITE, // Read/Write Access
  78. 0, // No Sharing, ports cant be shared
  79. NULL, // No Security
  80. OPEN_EXISTING, // Open existing port only
  81. 0, // Non Overlapped I/O
  82. NULL); // Null for Comm Devices
  83.  
  84. if (hComm == INVALID_HANDLE_VALUE)
  85. printf("\n Error! - Port %s can't be opened", ComPortName);
  86. else
  87. printf("\n Port %s Opened\n ", ComPortName);
  88.  
  89. /*------------------------------- Setting the Parameters for the SerialPort ------------------------------*/
  90.  
  91. DCB dcbSerialParams = { 0 }; // Initializing DCB structure
  92. dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
  93.  
  94. Status = GetCommState(hComm, &dcbSerialParams); //retreives the current settings
  95.  
  96. if (Status == FALSE)
  97. printf("\n Error! in GetCommState()");
  98.  
  99. dcbSerialParams.BaudRate = CBR_9600; // Setting BaudRate = 9600
  100. dcbSerialParams.ByteSize = 8; // Setting ByteSize = 8
  101. dcbSerialParams.StopBits = 1; // Setting StopBits = 1
  102. dcbSerialParams.Parity = 0; // Setting Parity = None
  103.  
  104. Status = SetCommState(hComm, &dcbSerialParams); //Configuring the port according to settings in DCB
  105.  
  106. if (Status == FALSE) {
  107. printf("\n Error! in Setting DCB Structure");
  108. } else {
  109. printf("\n Setting DCB Structure Successfull\n");
  110. printf("\n Baudrate = %d", dcbSerialParams.BaudRate);
  111. printf("\n ByteSize = %d", dcbSerialParams.ByteSize);
  112. printf("\n StopBits = %d", dcbSerialParams.StopBits);
  113. printf("\n Parity = %d", dcbSerialParams.Parity);
  114. }
  115.  
  116. /*------------------------------------ Setting Timeouts --------------------------------------------------*/
  117.  
  118. COMMTIMEOUTS timeouts = { 0 };
  119.  
  120. timeouts.ReadIntervalTimeout = 50;
  121. timeouts.ReadTotalTimeoutConstant = 50;
  122. timeouts.ReadTotalTimeoutMultiplier = 10;
  123. timeouts.WriteTotalTimeoutConstant = 50;
  124. timeouts.WriteTotalTimeoutMultiplier = 10;
  125.  
  126. if (SetCommTimeouts(hComm, &timeouts) == FALSE)
  127. printf("\n Error! in Setting Time Outs");
  128. else
  129. printf("\n\n Setting Serial Port Timeouts Successfull");
  130.  
  131. }
  132.  
  133. void writeChar(char *input) {
  134. /*----------------------------- Writing a Character to Serial Port----------------------------------------*/
  135. char lpBuffer[2]; // lpBuffer should be char or byte array, otherwise write wil fail
  136. strcpy(lpBuffer, input);
  137. DWORD dNoOFBytestoWrite; // No of bytes to write into the port
  138. DWORD dNoOfBytesWritten = 0; // No of bytes written to the port
  139.  
  140. dNoOFBytestoWrite = sizeof(lpBuffer); // Calculating the no of bytes to write into the port
  141.  
  142. Status = WriteFile(hComm, // Handle to the Serialport
  143. lpBuffer, // Data to be written to the port
  144. dNoOFBytestoWrite, // No of bytes to write into the port
  145. &dNoOfBytesWritten, // No of bytes written to the port
  146. NULL);
  147.  
  148. if (Status == TRUE)
  149. printf("\n\n %s - Written to %s", lpBuffer, ComPortName);
  150. else
  151. printf("\n\n Error %d in Writing to Serial Port", GetLastError());
  152.  
  153. }
  154.  
  155. void receiveChar() {
  156.  
  157. char TempChar; // Temperory Character
  158. DWORD dwEventMask; // Event mask to trigger
  159. DWORD NoBytesRead; // Bytes read by ReadFile()
  160. char SerialBuffer[256]; // Buffer Containing Rxed Data
  161. int i = 0;
  162.  
  163. /*------------------------------------ Setting Receive Mask ----------------------------------------------*/
  164.  
  165. Status = SetCommMask(hComm, EV_RXCHAR); //Configure Windows to Monitor the serial device for Character Reception
  166.  
  167. if (Status == FALSE)
  168. printf("\n\n Error! in Setting CommMask");
  169. else
  170. printf("\n\n Setting CommMask successfull");
  171.  
  172. /*------------------------------------ Setting WaitComm() Event ----------------------------------------*/
  173.  
  174. printf("\n\n Waiting for Data Reception");
  175.  
  176. Status = WaitCommEvent(hComm, &dwEventMask, NULL); //Wait for the character to be received
  177.  
  178. /*-------------------------- Program will Wait here till a Character is received ------------------------*/
  179.  
  180. if (Status == FALSE) {
  181. printf("\n Error! in Setting WaitCommEvent()");
  182. } else //If WaitCommEvent()==True Read the RXed data using ReadFile();
  183. {
  184. printf("\n\n Characters Received");
  185. do {
  186. Status = ReadFile(hComm, &TempChar, sizeof(TempChar), &NoBytesRead,
  187. NULL);
  188. SerialBuffer[i] = TempChar;
  189. i++;
  190. } while (NoBytesRead > 0);
  191.  
  192. /*------------Printing the RXed String to Console----------------------*/
  193.  
  194. printf("\n\n ");
  195. int j = 0;
  196. for (j = 0; j < i - 1; j++) // j < i-1 to remove the dupliated last character
  197. printf("%c", SerialBuffer[j]);
  198.  
  199. }
  200.  
  201. }
  202.  
  203. int main() {
  204.  
  205. setvbuf(stdout, NULL, _IONBF, 0);
  206. setvbuf(stderr, NULL, _IONBF, 0);
  207.  
  208. initialize();
  209. //char opdracht;
  210. //int counter = 0;
  211.  
  212. while (1) {
  213.  
  214. KeyboardState keyboardstate = getKeyboardState();
  215.  
  216. if (keyboardstate.right) {
  217. printf("rechts-2");
  218. char d[2] = "d";
  219. writeChar(d); //stuur w
  220. Status = TRUE;
  221. }
  222.  
  223. if (keyboardstate.left) {
  224. printf("links-2");
  225. char a[2] = "a";
  226. writeChar(a); //stuur w
  227. Status = TRUE;
  228. }
  229.  
  230. if (keyboardstate.up) {
  231. printf("omhoog-2");
  232. char w[2] = "w";
  233. writeChar(w); //stuur w
  234. Status = TRUE;
  235. }
  236.  
  237. if (keyboardstate.down) {
  238. printf("omlaag-2");
  239. char s[2] = "s";
  240. writeChar(s); //stuur w
  241. Status = TRUE;
  242. }
  243.  
  244. if (keyboardstate.quit)
  245. break;
  246.  
  247. if (keyboardstate.no) {
  248.  
  249. printf("NULL");
  250. char x[2] = "x";
  251. writeChar(x); //stuur w
  252. Status = TRUE;
  253.  
  254. //delay
  255. }
  256.  
  257. Sleep(16);
  258.  
  259. }
  260.  
  261. return (0);
  262.  
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement