⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpleserial.cpp

📁 Java Communication example
💻 CPP
字号:
#include "simpleserial.h"

#include <windows.h>

// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>

JNIEXPORT jint JNICALL Java_SimpleSerial__1openSerialPort
  (JNIEnv *env, jobject obj, jstring comPort, jint baud, jint dataBits, jint stopBits, jint parity)
{
	DCB				dcb;
	HANDLE			hCom;
	DWORD			dwError;
	BOOL			fSuccess;
	COMMTIMEOUTS	timeouts;
	const char		*buffer;

	buffer = env->GetStringUTFChars(comPort, NULL);

	hCom = CreateFile(buffer, 
		GENERIC_READ | GENERIC_WRITE,
		0,    // comm devices must be opened w/exclusive-access 
	    NULL, // no security attributes 
	    OPEN_EXISTING, // comm devices must use OPEN_EXISTING 
	    0,    // not overlapped I/O 
	
		NULL  // hTemplate must be NULL for comm devices     
		);

	env->ReleaseStringUTFChars(comPort, buffer);

	if (hCom == INVALID_HANDLE_VALUE) {    
		dwError = GetLastError();
		return 0;
    // handle error 
	}
	
	fSuccess = GetCommState(hCom, &dcb);
	if (!fSuccess) {    // Handle the error. 
		CloseHandle(hCom);
		return 0;

	}

	dcb.BaudRate = baud;
	dcb.ByteSize = (unsigned char)dataBits;
	dcb.Parity = (unsigned char)parity;
	dcb.StopBits = (unsigned char)stopBits;
	fSuccess = SetCommState(hCom, &dcb);
	if (!fSuccess) {
		CloseHandle(hCom);
		return 0;
	}

	timeouts.ReadIntervalTimeout = MAXDWORD;
	timeouts.ReadTotalTimeoutMultiplier = 0;
	timeouts.ReadTotalTimeoutConstant = 0;
	timeouts.WriteTotalTimeoutConstant = 0;
	timeouts.WriteTotalTimeoutMultiplier = 0;

	fSuccess = SetCommTimeouts(hCom, &timeouts);
	if (!fSuccess) {
		CloseHandle(hCom);
		return 0;
	}

	FlushFileBuffers(hCom);
	PurgeComm(hCom, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);

	return (long)hCom;
}

/*
 * Class:     SimpleSerial
 * Method:    _readSerialPort
 * Signature: (I)[B
 */
JNIEXPORT jbyteArray JNICALL Java_SimpleSerial__1readSerialPort
  (JNIEnv *env, jobject obj, jint port)
{
	DWORD		numRead;
	signed char		buffer[512];		// this should be large enough;

	ReadFile((void*)port, buffer, 512, &numRead, NULL);
	
	jbyteArray		byteArray = env->NewByteArray(numRead);	

	env->SetByteArrayRegion(byteArray, 0, numRead, buffer);

	return byteArray;
}

/*
 * Class:     SimpleSerial
 * Method:    _writeSerialPort
 * Signature: (ILjava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_SimpleSerial__1writeSerialPort
  (JNIEnv *env, jobject obj, jint port, jstring string)
{
	DWORD			numWritten;
	int				size;
	const char		*buffer;

	size = env->GetStringLength(string);

	if (size == 0) {		// nothing to write
		return 0;
	}

	buffer = env->GetStringUTFChars(string, NULL);

	WriteFile((void*)port, buffer, size, &numWritten, NULL);

	env->ReleaseStringUTFChars(string, buffer);	

	return numWritten;
}


/*
 * Class:     SimpleSerial
 * Method:    _closeSerialPort
 * Signature: (I)V
 */
JNIEXPORT void JNICALL Java_SimpleSerial__1closeSerialPort
  (JNIEnv *, jobject, jint port)
{
	if (port) CloseHandle((void*)port);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -