Subversion Repositories Projects

Rev

Rev 178 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 178 Rev 184
Line 23... Line 23...
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