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