Subversion Repositories Projects

Rev

Go to most recent revision | Details | 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
28
        char comPort[5] = {'C','O','M'}; //Number and String Terminator to be added
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:
34
        comPort[3] = 48 + comNumber;    //COMx (x is ASCII coded)
35
        comPort[4] = 0;                                 //add string Terminator 'NUL'
36
 
37
        //Create Device File, initialize serial Port:
38
        hCom = CreateFile( comPort,     //open COMx Device (pointer to comPort-String)
39
                                        GENERIC_READ | GENERIC_WRITE,   //write/read permisions
40
                                        0,    // must be opened with exclusive-access
41
                                        NULL, // default security
42
                                        OPEN_EXISTING, // must use OPEN_EXISTING
43
                                        0,    // no overlapping
44
                                        NULL  // hTemplate must be NULL for comm devices
45
                                        );
46
 
47
        //Check, if CreateFile succeeded:
48
        if (hCom == INVALID_HANDLE_VALUE) {
49
                printf ("***ERR: CreateFile failed with error %d.\n", GetLastError());
50
                return EXIT_FAILURE;
51
        }
52
 
53
        //Get the currently active Device Control Block (DCB) for the COMx:
54
        SecureZeroMemory(&dcb, sizeof(DCB)); //initialize entire dcb-struct with zeros
55
        dcb.DCBlength = sizeof(DCB);                   
56
        fSuccess = GetCommState(hCom, &dcb); //write the current configuration to
57
                                                                                 //our dcb-struct
58
 
59
        //GetCommState returns 0, if error occured while getting the DCB
60
        if (!fSuccess) {
61
                printf ("***ERR: GetCommState failed with error %d.\n", GetLastError());
62
                return EXIT_FAILURE;
63
        }
64
 
65
        /*Change the values of the obtained dcb-struct to match the config of the
66
         *Mikrokopter. Currently the FlightControl communicates at:
67
         *57600 Baud (bps), 8 Data bits, no parity bit, 1 stop bit */
68
 
69
        dcb.BaudRate = CBR_57600;     // Set Baudrate
70
        dcb.ByteSize = 8;             // data size, transmit and receive
71
        dcb.Parity = NOPARITY;        // no parity bit
72
        dcb.StopBits = ONESTOPBIT;    // one stop bit
73
 
74
        //Send reconfigured dcb back to COMx Device-File:
75
        fSuccess = SetCommState(hCom, &dcb);
76
 
77
        //SetCommState returns a 0, if error occured while setting the DCB for COMx
78
        if (!fSuccess) {
79
                printf ("***ERR: SetCommState failed with error %d.\n", GetLastError());
80
      return EXIT_FAILURE;
81
        }
82
 
83
        //Configure the Read-Timeout behaviour of the initialized COMx-Port:
84
 
85
        /* The following Read-Timeout config determines the behaviour of the
86
        ReadFile cmd (applied to the initialized COMx-Port):
87
        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
89
        the UART-InputBuffer and the ReadFile function is called, the ReadFile
90
        function will return *immediatly* (BytesRead parameter is zero then).
91
        */
92
        timeouts.ReadIntervalTimeout            = MAXDWORD;
93
        timeouts.ReadTotalTimeoutMultiplier = 0;
94
        timeouts.ReadTotalTimeoutConstant       = 0;
95
 
96
        //Send the Timeout-Config to the COM-Port:
97
        fSuccess = SetCommTimeouts (hCom, &timeouts);
98
 
99
        //Check, if setting the timeout-settings was successfull:
100
        if(!fSuccess) {
101
                printf("***ERR: SetCommTimeouts failed with error %d.\n", GetLastError());
102
                return EXIT_FAILURE;
103
        }
104
 
105
 
106
        //Initialization & Configuration of COMx done!
107
        return EXIT_SUCCESS;
108
}
109
 
110
 
111
//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)
113
int getCharFromCom (char* inputBuffer) {
114
 
115
        int numOfBytesRead;
116
        ReadFile (hCom, inputBuffer, 1, &numOfBytesRead, NULL);
117
        //read char is stored in the passed char-buffer
118
 
119
        return numOfBytesRead;
120
 
121
}
122
 
123
 
124
//Function which writes [length] Byte to the Tx-Buffer of the initialized COM-Port:
125
//Returns something>0 if write failed, 0 if successfull
126
int sendStringToCom (char* toSend, int length) {
127
 
128
        int numOfBytesWritten;
129
 
130
        //write char to which the passed char-pointer points to
131
        return WriteFile (hCom, toSend, length, &numOfBytesWritten, NULL);
132
}