Rev 178 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
178 | Alpin | 1 | /* This file is part of MoteCtrl |
2 | http://www.mikrokopter.de/ucwiki/en/MoteCtrl |
||
3 | */ |
||
4 | |||
5 | //Include header files, which provide resources, pc_serial_port.c needs: |
||
6 | #include <windows.h> //Windows API, to interface with the serial port |
||
7 | #include <stdio.h> //Standard I/O (printf for instance) |
||
8 | |||
9 | #include "pc_serial_port.h" |
||
10 | |||
11 | |||
12 | /* |
||
13 | Compiler & Linker Porperties: |
||
14 | Set "Character Set"-CompilerFlag to "Not Set" or "Multi Byte Character Set" to |
||
15 | avoid Errors about Windows API functions that can handle Unicode paramters. |
||
16 | In Visual Studio: Properties of Project -> Configuration Properties -> General |
||
17 | */ |
||
18 | |||
19 | |||
20 | HANDLE hCom; //Handle, which contains inf about the opened Device File |
||
21 | //(the opened COM-Port, returned by CreateFile function) |
||
22 | |||
23 | //Initialize & Configure COMx-Port |
||
24 | //return value: EXIT_FAILURE=1, EXIT_SUCCESS=0 (stdlib macros) |
||
25 | int init_com_port(int comNumber) { |
||
26 | |||
27 | DCB dcb; //Device Control Block, contains Configuration for COM-Port |
||
184 | Alpin | 28 | char comPort[6] = {'C','O','M'}; //Number and String Terminator to be added |
178 | Alpin | 29 | int fSuccess; //Stores inf about file-actions (action succeeded or failed) |
30 | COMMTIMEOUTS timeouts; //Struct, which stores inf about the timeout |
||
31 | //behaviour of the COM-Port |
||
32 | |||
33 | //Get the Com-Port Number: |
||
184 | Alpin | 34 | if( (comNumber>0) && (comNumber<10) ) { |
178 | Alpin | 35 | |
184 | Alpin | 36 | comPort[3] = 48 + comNumber; //COMx (x is ASCII coded) |
37 | comPort[4] = 0; //add string Terminator 'NUL' |
||
38 | comPort[5] = 0; |
||
39 | printf("-->using %s\n", comPort); |
||
40 | } |
||
41 | |||
42 | else { |
||
43 | if(comNumber>=10 && comNumber<=99) { |
||
44 | |||
45 | comPort[3] = 48 + (comNumber/10); |
||
46 | comPort[4] = 48 + (comNumber%10); |
||
47 | comPort[5] = 0; |
||
48 | printf("-->using %s\n", comPort); |
||
49 | } |
||
50 | |||
51 | else { |
||
52 | printf("COM Port must be between 1 and 99\n"); |
||
53 | return EXIT_FAILURE; |
||
54 | } |
||
55 | } |
||
56 | |||
178 | Alpin | 57 | //Create Device File, initialize serial Port: |
58 | hCom = CreateFile( comPort, //open COMx Device (pointer to comPort-String) |
||
59 | GENERIC_READ | GENERIC_WRITE, //write/read permisions |
||
60 | 0, // must be opened with exclusive-access |
||
61 | NULL, // default security |
||
62 | OPEN_EXISTING, // must use OPEN_EXISTING |
||
63 | 0, // no overlapping |
||
64 | NULL // hTemplate must be NULL for comm devices |
||
65 | ); |
||
66 | |||
67 | //Check, if CreateFile succeeded: |
||
68 | if (hCom == INVALID_HANDLE_VALUE) { |
||
69 | printf ("***ERR: CreateFile failed with error %d.\n", GetLastError()); |
||
70 | return EXIT_FAILURE; |
||
71 | } |
||
72 | |||
73 | //Get the currently active Device Control Block (DCB) for the COMx: |
||
74 | SecureZeroMemory(&dcb, sizeof(DCB)); //initialize entire dcb-struct with zeros |
||
75 | dcb.DCBlength = sizeof(DCB); |
||
76 | fSuccess = GetCommState(hCom, &dcb); //write the current configuration to |
||
77 | //our dcb-struct |
||
78 | |||
79 | //GetCommState returns 0, if error occured while getting the DCB |
||
80 | if (!fSuccess) { |
||
81 | printf ("***ERR: GetCommState failed with error %d.\n", GetLastError()); |
||
82 | return EXIT_FAILURE; |
||
83 | } |
||
84 | |||
85 | /*Change the values of the obtained dcb-struct to match the config of the |
||
86 | *Mikrokopter. Currently the FlightControl communicates at: |
||
87 | *57600 Baud (bps), 8 Data bits, no parity bit, 1 stop bit */ |
||
88 | |||
89 | dcb.BaudRate = CBR_57600; // Set Baudrate |
||
90 | dcb.ByteSize = 8; // data size, transmit and receive |
||
91 | dcb.Parity = NOPARITY; // no parity bit |
||
92 | dcb.StopBits = ONESTOPBIT; // one stop bit |
||
93 | |||
94 | //Send reconfigured dcb back to COMx Device-File: |
||
95 | fSuccess = SetCommState(hCom, &dcb); |
||
96 | |||
97 | //SetCommState returns a 0, if error occured while setting the DCB for COMx |
||
98 | if (!fSuccess) { |
||
99 | printf ("***ERR: SetCommState failed with error %d.\n", GetLastError()); |
||
100 | return EXIT_FAILURE; |
||
101 | } |
||
102 | |||
103 | //Configure the Read-Timeout behaviour of the initialized COMx-Port: |
||
104 | |||
105 | /* The following Read-Timeout config determines the behaviour of the |
||
106 | ReadFile cmd (applied to the initialized COMx-Port): |
||
107 | If a Byte is in the Hardware UART-Buffer of the initialized COM-Port and |
||
108 | the ReadFile function is called, this byte is read. If theres no byte in |
||
109 | the UART-InputBuffer and the ReadFile function is called, the ReadFile |
||
110 | function will return *immediatly* (BytesRead parameter is zero then). |
||
111 | */ |
||
112 | timeouts.ReadIntervalTimeout = MAXDWORD; |
||
113 | timeouts.ReadTotalTimeoutMultiplier = 0; |
||
114 | timeouts.ReadTotalTimeoutConstant = 0; |
||
115 | |||
116 | //Send the Timeout-Config to the COM-Port: |
||
117 | fSuccess = SetCommTimeouts (hCom, &timeouts); |
||
118 | |||
119 | //Check, if setting the timeout-settings was successfull: |
||
120 | if(!fSuccess) { |
||
121 | printf("***ERR: SetCommTimeouts failed with error %d.\n", GetLastError()); |
||
122 | return EXIT_FAILURE; |
||
123 | } |
||
124 | |||
125 | |||
126 | //Initialization & Configuration of COMx done! |
||
127 | return EXIT_SUCCESS; |
||
128 | } |
||
129 | |||
130 | |||
131 | //Function which reads 1 Byte of the Rx-Buffer of the initialized COM-Port: |
||
132 | //Return value: Number of the bytes read (should be 0 or 1) |
||
133 | int getCharFromCom (char* inputBuffer) { |
||
134 | |||
135 | int numOfBytesRead; |
||
136 | ReadFile (hCom, inputBuffer, 1, &numOfBytesRead, NULL); |
||
137 | //read char is stored in the passed char-buffer |
||
138 | |||
139 | return numOfBytesRead; |
||
140 | |||
141 | } |
||
142 | |||
143 | |||
144 | //Function which writes [length] Byte to the Tx-Buffer of the initialized COM-Port: |
||
145 | //Returns something>0 if write failed, 0 if successfull |
||
146 | int sendStringToCom (char* toSend, int length) { |
||
147 | |||
148 | int numOfBytesWritten; |
||
149 | |||
150 | //write char to which the passed char-pointer points to |
||
151 | return WriteFile (hCom, toSend, length, &numOfBytesWritten, NULL); |
||
152 | } |