Initial release
[fw/sdcc] / device / examples / serialcomm / windows / serial.cpp
1 // Elementary functions for for serial communication for Visual C / MFC
2 // Bela Torok / bela.torok@kssg.ch, March 2001
3
4 // This version is using the CTS/RTS protocol only, with 8 databits + no parity.
5 // This file was tested with ser_ir_cts_rts.c (in device/lib)
6
7 // Todo: Imporve the function SerialInit to support communication with no_protocol & XON/XOFF protocol,
8 // 7 databits, even & odd parity, 1, 1.5 & 2 stopbits, etc...
9
10 #include <stdio.h>
11 #include <time.h>
12
13 //#define VC_EXTRALEAN          // Exclude rarely-used stuff from Windows headers
14
15 #include <afxwin.h>    // serial.cpp : Defines the entry point for the console application.
16
17 //#include "stdafx.h"
18 #include <string.h>
19
20 #include "serial.h"
21
22 // Flow control flags
23
24 #define FC_DTRDSR       0x01
25 #define FC_RTSCTS       0x02
26 #define FC_XONXOFF      0x04
27
28 // ascii definitions
29
30 #define ASCII_BEL       0x07
31 #define ASCII_BS        0x08
32 #define ASCII_LF        0x0A
33 #define ASCII_CR        0x0D
34 #define ASCII_XON       0x11
35 #define ASCII_XOFF      0x13
36
37         // variables used with the com port
38         BOOL                    bPortReady;
39         DCB                             dcb;
40         COMMTIMEOUTS    CommTimeouts;
41         BOOL                    bWriteRC;
42         BOOL                    bReadRC;
43         DWORD                   iBytesWritten;
44         DWORD                   iBytesRead;
45
46 HANDLE SerialInit(char *ComPortName, int BaudRate) 
47 {
48         HANDLE hCom;
49         
50         hCom = CreateFile(ComPortName, 
51                 GENERIC_READ | GENERIC_WRITE,
52                 0, // exclusive access
53                 NULL, // no security
54                 OPEN_EXISTING,
55                 0, // no overlapped I/O
56                 NULL); // null template 
57
58         bPortReady = SetupComm(hCom, 2, 128); // set buffer sizes
59
60
61         bPortReady = GetCommState(hCom, &dcb);
62         dcb.BaudRate = BaudRate;
63         dcb.ByteSize = 8;
64         dcb.Parity = NOPARITY;
65 //      dcb.Parity = EVENPARITY;
66         dcb.StopBits = ONESTOPBIT;
67         dcb.fAbortOnError = TRUE;
68
69         // set XON/XOFF
70         dcb.fOutX = FALSE;                                      // XON/XOFF off for transmit
71         dcb.fInX        = FALSE;                                        // XON/XOFF off for receive
72         // set RTSCTS
73         dcb.fOutxCtsFlow = TRUE;                                        // turn on CTS flow control
74         dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;        // 
75         // set DSRDTR
76         dcb.fOutxDsrFlow = FALSE;                                       // turn on DSR flow control
77         dcb.fDtrControl = DTR_CONTROL_ENABLE;   // 
78 //      dcb.fDtrControl = DTR_CONTROL_DISABLE;  // 
79 //      dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;        // 
80
81         bPortReady = SetCommState(hCom, &dcb);
82
83         // Communication timeouts are optional
84
85         bPortReady = GetCommTimeouts (hCom, &CommTimeouts);
86
87         CommTimeouts.ReadIntervalTimeout = 5000;
88         CommTimeouts.ReadTotalTimeoutConstant = 5000;
89         CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
90         CommTimeouts.WriteTotalTimeoutConstant = 5000;
91         CommTimeouts.WriteTotalTimeoutMultiplier = 1000;
92
93         bPortReady = SetCommTimeouts (hCom, &CommTimeouts);
94
95         return hCom;
96 }
97
98 char SerialGetc(HANDLE *hCom)
99 {
100         char rxchar;
101         BOOL    bReadRC;
102         static  DWORD   iBytesRead;
103
104         bReadRC = ReadFile(*hCom, &rxchar, 1, &iBytesRead, NULL);
105
106         return rxchar;
107 }
108
109 void SerialPutc(HANDLE *hCom, char txchar)
110 {
111         BOOL    bWriteRC;
112         static  DWORD   iBytesWritten;
113         
114         bWriteRC = WriteFile(*hCom, &txchar, 1, &iBytesWritten,NULL);
115 }
116
117 char* SerialGets(HANDLE *hCom)
118 {
119         static char rxstring[256];
120         char c;
121         int pos = 0;
122
123         while(pos <= 255) {
124                 c = SerialGetc(hCom);
125                 if(c == '\r') continue;         // discard carriage return
126                 rxstring[pos++] = c;
127                 if(c == '\n') break;
128         }
129         rxstring[pos] = 0;
130
131         return  rxstring;
132 }
133
134 void SerialPuts(HANDLE *hCom, char *txstring)
135 {
136         BOOL    bWriteRC;
137         static  DWORD   iBytesWritten;
138
139         bWriteRC = WriteFile(*hCom, txstring, strlen(txstring), &iBytesWritten,NULL);
140
141 }
142
143 void sleep( int _wait)
144 {
145         clock_t goal;
146         goal = clock() + _wait;
147         while( goal > clock() );
148 }
149