Skip to main content
summaryrefslogtreecommitdiffstats
blob: 69a1de31a3132dae6c80192a82b604733d1736cb (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2015 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     QNX Software Systems - Initial API and implementation
 *******************************************************************************/
#include "stdafx.h"

extern "C"
JNIEXPORT jlong JNICALL Java_org_eclipse_cdt_utils_serial_SerialPort_open0
(JNIEnv *env, jobject jobj, jstring portName, jint baudRate, jint byteSize, jint parity, jint stopBits)
{
	const wchar_t * cportName = (const wchar_t *) env->GetStringChars(portName, NULL);
	HANDLE handle = CreateFile(cportName,
		GENERIC_READ | GENERIC_WRITE,
		0,
		NULL,
		OPEN_EXISTING,
		FILE_FLAG_OVERLAPPED,
		NULL);

	if (handle != INVALID_HANDLE_VALUE) {
		DCB dcb = { 0 };

		if (!GetCommState(handle, &dcb)) {
			fprintf(stderr, "Error getting DCB: %S\n", cportName);
			return -1;
		}

		dcb.BaudRate = baudRate;
		dcb.ByteSize = (BYTE) byteSize;

		switch (parity) {
		case 0: // None
			dcb.fParity = FALSE;
			break;
		case 1: // Even
			dcb.fParity = TRUE;
			dcb.Parity = EVENPARITY;
			break;
		case 2: // Odd
			dcb.fParity = TRUE;
			dcb.Parity = ODDPARITY;
			break;
		}
		
		switch (stopBits) {
		case 0:
			dcb.StopBits = ONESTOPBIT;
			break;
		case 1:
			dcb.StopBits = TWOSTOPBITS;
			break;
		}

		if (!SetCommState(handle, &dcb)) {
			fprintf(stderr, "Error setting DCB: %S\n", cportName);
			return -1;
		}
	}

	return (jlong) handle;
}

extern "C"
JNIEXPORT void JNICALL Java_org_eclipse_cdt_utils_serial_SerialPort_close0
(JNIEnv *env, jobject jobj, jlong handle)
{
	CloseHandle((HANDLE) handle);
}

extern "C"
JNIEXPORT jint JNICALL Java_org_eclipse_cdt_utils_serial_SerialPort_read0
(JNIEnv *env, jobject jobj, jlong jhandle)
{
	OVERLAPPED olp = { 0 };

	olp.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
	if (olp.hEvent == NULL) {
		fprintf(stderr, "Error creating event\n");
		fflush(stderr);
		return -1;
	}

	char buff;
	DWORD nwritten;
	HANDLE handle = (HANDLE)jhandle;

	if (!ReadFile(handle, &buff, sizeof(buff), &nwritten, &olp)) {
		if (GetLastError() != ERROR_IO_PENDING) {
			fprintf(stderr, "Error reading from port: %d\n", GetLastError());
			fflush(stderr);
			return -1;
		}
		else {
			switch (WaitForSingleObject(olp.hEvent, INFINITE)) {
			case WAIT_OBJECT_0:
				if (!GetOverlappedResult(handle, &olp, &nwritten, FALSE)) {
					if (GetLastError() != ERROR_OPERATION_ABORTED) {
						fprintf(stderr, "Error waiting for read: %d\n", GetLastError());
						fflush(stderr);
					}
					return -1;
				}
				break;
			}
		}
	}

	CloseHandle(olp.hEvent);
	return buff;
}

extern "C"
JNIEXPORT void JNICALL Java_org_eclipse_cdt_utils_serial_SerialPort_write0
(JNIEnv *env, jobject jobj, jlong jhandle, jint b)
{
	OVERLAPPED olp = { 0 };

	olp.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
	if (olp.hEvent == NULL) {
		fprintf(stderr, "Error creating event\n");
		return;
	}

	char buff = (char) b;
	DWORD nwritten;
	HANDLE handle = (HANDLE) jhandle;

	if (!WriteFile(handle, &buff, sizeof(buff), &nwritten, &olp)) {
		if (GetLastError() != ERROR_IO_PENDING) {
			fprintf(stderr, "Error writing to port\n");
		}
		else {
			switch (WaitForSingleObject(olp.hEvent, INFINITE)) {
			case WAIT_OBJECT_0:
				if (!GetOverlappedResult(handle, &olp, &nwritten, FALSE)) {
					fprintf(stderr, "Error waiting for write\n");
				}
			}
		}
	}

	CloseHandle(olp.hEvent);
}

Back to the top