forked from netdata-be/libnodave
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setportw.c
136 lines (116 loc) · 3.78 KB
/
setportw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
Part of Libnodave, a free communication libray for Siemens S7 300/400.
(C) Thomas Hergenhahn ([email protected]) 2004.
Libnodave is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
Libnodave is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#define debug 0
#define ThisModule "setPort : "
#ifdef BCCWIN
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#include "nodave.h"
extern int daveDebug;
/*
You may wonder why a pair of identical file handles is set up and
returned. It's for compatibility with an extended UNIX version of
this code, which can use two separate pipes for reads and writes.
*/
__declspec(dllexport)
HANDLE __stdcall setPort(char * devname, char * baud,char parity /*, HANDLE * wfd*/){
HANDLE hComm;
DCB dcb;
hComm = CreateFile( devname,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_FLAG_WRITE_THROUGH,
0);
if (daveDebug & daveDebugOpen) {
printf("setPort %s\n",devname);
printf("setPort %s\n",baud);
printf("setPort %c\n",parity);
}
// printf("Handle to %s opened! %d\n",devname,hComm);
GetCommState(hComm,&dcb);
// printf("got Comm State. %d\n ",dcb.BaudRate);
dcb.ByteSize = 8;
dcb.fOutxCtsFlow=FALSE;
dcb.fOutxDsrFlow=FALSE;
// dcb.fDtrControl=DTR_CONTROL_DISABLE; // this seems to be the evil. Guess do not understand the meaning of this parameter
dcb.fDtrControl=DTR_CONTROL_ENABLE;
dcb.fDsrSensitivity=FALSE;
dcb.fInX=FALSE;
dcb.fOutX=FALSE;
dcb.fNull=FALSE;
dcb.fAbortOnError=FALSE;
dcb.fBinary=TRUE;
dcb.fParity=TRUE;
dcb.fOutxCtsFlow=FALSE;
dcb.fOutxDsrFlow=FALSE;
// dcb.fRtsControl=FALSE; // this seems to be the evil. Guess do not understand the meaning of this parameter
dcb.fRtsControl=TRUE;
dcb.fTXContinueOnXoff=TRUE;
dcb.StopBits=2; ///that was 2 !!!
if (0==strncmp(baud,"115200",6))
dcb.BaudRate = CBR_115200;
else if (0==strncmp(baud,"57600",5))
dcb.BaudRate = CBR_57600;
else if (0==strncmp(baud,"38400",5))
dcb.BaudRate = CBR_38400;
else if (0==strncmp(baud,"19200",5))
dcb.BaudRate = CBR_19200;
else if (0==strncmp(baud,"9600",4))
dcb.BaudRate = CBR_9600;
else if (0==strncmp(baud,"4800",4))
dcb.BaudRate = CBR_4800;
else if (0==strncmp(baud,"2400",4))
dcb.BaudRate = CBR_2400;
else if (0==strncmp(baud,"1200",4))
dcb.BaudRate = CBR_1200;
else if (0==strncmp(baud,"600",3))
dcb.BaudRate = CBR_600;
else if (0==strncmp(baud,"300",3))
dcb.BaudRate = CBR_300;
else if (daveDebug & daveDebugPrintErrors) {
printf(ThisModule "illegal Baudrate: %s\n", baud);
}
parity=tolower(parity);
if (parity == 'e')
dcb.Parity = 2;
else if (parity == 'o')
dcb.Parity = 1;
else if (parity == 'n')
dcb.Parity = 0;
else if (daveDebug & daveDebugPrintErrors) {
printf(ThisModule "illegal parity mode:%c\n", parity);
}
SetCommState(hComm,&dcb);
// printf("got Comm State. %d\n ",dcb.BaudRate);
//printf("Comm State set.\n");
// *wfd=hComm;
return hComm;
}
__declspec(dllexport)
int __stdcall closePort(HANDLE port){
int res=CloseHandle(port);
return res;
}
#endif
/*
Changes:
12/17/2004 1st Version for Windows.
04/03/2005 Hopefully really fixed COM port setting.
05/08/2005 Removed printfs for quiet operation.
*/