-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJSPC.cpp
185 lines (115 loc) · 3.73 KB
/
JSPC.cpp
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "com_github_barismeral_ports_SerialPort.h"
#include <windows.h>
/*
* Class: com_github_barismeral_ports_SerialPort
* Method: openPort
* Signature: (Ljava/lang/String;)J
*/
JNIEXPORT jlong JNICALL Java_com_github_barismeral_ports_SerialPort_openPort
(JNIEnv *env, jobject obj, jstring str){
const char prefix[] = "\\\\.\\"; // port prefix \\.\\
const char* port = env->GetStringUTFChars(str, JNI_FALSE);
const int strSize = strlen(port);
const int symboSize = strlen(prefix)+1;
char portFullName[symboSize + strSize];
strcpy(portFullName, prefix);
strcat(portFullName, port);
HANDLE hComm = CreateFile(
portFullName,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
NULL,
0);
if(hComm==INVALID_HANDLE_VALUE){ // invalid port return -1
return (jlong)-1;
}
else if(GetLastError() == ERROR_ACCESS_DENIED){ // Return 5
return (jlong)5;
}
else{
DCB *dcb = new DCB();
GetCommState(hComm,dcb);
}
return (jlong)hComm; // return port handle
}
/*
* Class: com_github_barismeral_ports_SerialPort
* Method: closePort
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL Java_com_github_barismeral_ports_SerialPort_closePort
(JNIEnv *env, jobject obj, jlong port){
HANDLE hComm = (HANDLE)port;
return CloseHandle(hComm);
}
/*
* Class: com_github_barismeral_ports_SerialPort
* Method: setParam
* Signature: (JIIII)Z
*/
JNIEXPORT jboolean JNICALL Java_com_github_barismeral_ports_SerialPort_setParam
(JNIEnv *env, jobject obj, jlong port, jint baud, jint bits, jint par, jint stop){
HANDLE HComm = (HANDLE)port;
DCB *dcb = new DCB();
dcb->BaudRate = baud;
dcb->ByteSize = bits;
dcb->Parity = par;
dcb->StopBits = stop;
SetCommState(HComm,dcb);
COMMTIMEOUTS *commTime = new COMMTIMEOUTS();
commTime->ReadIntervalTimeout = 10;
commTime->ReadTotalTimeoutConstant = 0;
commTime->ReadTotalTimeoutMultiplier = 0;
commTime->WriteTotalTimeoutConstant = 0;
commTime->WriteTotalTimeoutMultiplier = 0;
return SetCommTimeouts(HComm,commTime);
}
/*
* Class: com_github_barismeral_ports_SerialPort
* Method: write
* Signature: (J[C)Z
*/
JNIEXPORT jboolean JNICALL Java_com_github_barismeral_ports_SerialPort_write
(JNIEnv *env, jobject obj, jlong port, jbyteArray bytes){
HANDLE hComm = (HANDLE) port;
jboolean status = JNI_FALSE;
jbyte *buffer = env->GetByteArrayElements(bytes,JNI_FALSE);
if(WriteFile(hComm,buffer,(DWORD)env->GetArrayLength(bytes),NULL,NULL)){
status = JNI_TRUE;
}
else {
status = JNI_FALSE;
}
env->ReleaseByteArrayElements(bytes,buffer,0);
return status;
}
/*
* Class: com_github_barismeral_ports_SerialPort
* Method: read
* Signature: (JI)[B
*/
JNIEXPORT jbyteArray JNICALL Java_com_github_barismeral_ports_SerialPort_read
(JNIEnv *env, jobject obj, jlong port, jint size){
HANDLE hComm = (HANDLE) port;
PurgeComm(hComm,PURGE_TXCLEAR);
jbyteArray array = env->NewByteArray(size);
jbyte buffer[size];
//(DWORD)size
if(ReadFile(hComm,buffer,size,NULL,NULL))
{
env->SetByteArrayRegion(array,0,size,buffer);
}
return array;
}
/*
* Class: com_github_barismeral_ports_SerialPort
* Method: purgePort
* Signature: (J)Z
*/
JNIEXPORT jboolean JNICALL Java_com_github_barismeral_ports_SerialPort_purgePort
(JNIEnv *env, jobject obj, jlong port){
HANDLE hComm = (HANDLE) port;
return PurgeComm(hComm,PURGE_TXCLEAR);
}