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 186 187 188 189 190
|
#include <jni.h>
#include "testClass.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <iostream.h>
/*=============================================================================
Définition de constantes
=============================================================================*/
#define RX_SIZE 4096 /* taille tampon d'entrée */
#define TX_SIZE 4096 /* taille tampon de sortie */
#define MAX_WAIT_READ 5000 /* temps max d'attente pour lecture (en ms) */
/*=============================================================================
Variables globales.
=============================================================================*/
/* Handle du port COM ouvert */
HANDLE g_hCOM = NULL;
/* Délais d'attente sur le port COM */
COMMTIMEOUTS g_cto =
{
MAX_WAIT_READ, /* ReadIntervalTimeOut */
0, /* ReadTotalTimeOutMultiplier */
MAX_WAIT_READ, /* ReadTotalTimeOutConstant */
0, /* WriteTotalTimeOutMultiplier */
0 /* WriteTotalTimeOutConstant */
};
/* Configuration du port COM */
DCB g_dcb =
{
sizeof(DCB), /* DCBlength */
921600, /* BaudRate */
TRUE, /* fBinary */
FALSE, /* fParity */
FALSE, /* fOutxCtsFlow */
FALSE, /* fOutxDsrFlow */
DTR_CONTROL_ENABLE, /* fDtrControl */
FALSE, /* fDsrSensitivity */
FALSE, /* fTXContinueOnXoff */
FALSE, /* fOutX */
FALSE, /* fInX */
FALSE, /* fErrorChar */
FALSE, /* fNull */
RTS_CONTROL_ENABLE, /* fRtsControl */
FALSE, /* fAbortOnError */
0, /* fDummy2 */
0, /* wReserved */
0x100, /* XonLim */
0x100, /* XoffLim */
8, /* ByteSize */
NOPARITY, /* Parity */
ONESTOPBIT, /* StopBits */
0x11, /* XonChar */
0x13, /* XoffChar */
'?', /* ErrorChar */
0x1A, /* EofChar */
0x10 /* EvtChar */
};
/*=============================================================================
Fonctions du module.
=============================================================================*/
/*
* Class: ComRS232
* Method: OpenCOM
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_testClass_OpenCOM(JNIEnv * env, jobject obj, jstring port);
/*
* Class: ComRS232
* Method: CloseCom
* Signature: ()I
*/
JNIEXPORT jboolean JNICALL Java_testClass_CloseCom(JNIEnv *env, jobject obj);
/***********************************************************
* OpenCOM : Ouverture et configuration du port COM
* entrée : port : nom du port COM à ouvrir
* retour : 1 si l'opération à réussi, 0 sinon
* *****************************************************/
JNIEXPORT jint JNICALL Java_testClass_OpenCOM(JNIEnv * env, jobject obj, jstring port){
//(env)->MonitorEnter(obj);
const char * portCOMM=env->GetStringUTFChars(port,0);
cout<<"code C : ouverture du port "<<portCOMM<<endl;
g_hCOM = CreateFile(portCOMM, GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, NULL);
if(g_hCOM == INVALID_HANDLE_VALUE)
{
cout<<"erreur à l'ouverture du port"<<endl;
return 0;
}
/* affectation taille des tampons d'émission et de réception */
SetupComm(g_hCOM, RX_SIZE, TX_SIZE);
/* configuration du port COM */
if(!SetCommTimeouts(g_hCOM, &g_cto) || !SetCommState(g_hCOM, &g_dcb))
{
cout<<"Erreur lors de la configuration du port "<<portCOMM;
CloseHandle(g_hCOM);
return 0;
}
/* on vide les tampons d'émission et de réception, mise à 1 DTR */
PurgeComm(g_hCOM, PURGE_TXCLEAR|PURGE_RXCLEAR|PURGE_TXABORT|PURGE_RXABORT);
EscapeCommFunction(g_hCOM, SETDTR);
printf("CODE C : OPENCOM fin");
cout<<"CODE C : OPEN COM fIN "<<endl;
env->ReleaseStringUTFChars(port,portCOMM);
//(env)->MonitorExit(obj);
return 1;
}
/**************************************************************
* CloseCom : fermeture du port COM
* retour : 1 si réussi, 0 sinon
*/
JNIEXPORT jboolean JNICALL Java_testClass_CloseCom(JNIEnv *env, jobject obj){
(env) -> MonitorEnter(obj);
/* fermeture du port COM */
cout<<"\n FERMeTURE DU PORT \n"<<endl;
return CloseHandle(g_hCOM);
(env)->MonitorExit(obj);
}
/*
* Class: testClass
* Method: readCOM
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_testClass_readCOM(JNIEnv *env, jobject obj){
(env) -> MonitorEnter(obj);
cout<<"1"<<endl;
byte buffer;
DWORD* pBytesRead;
cout<<"1"<<endl;
ReadFile(g_hCOM,&buffer,1,pBytesRead,NULL);
cout<<"1"<<endl;
//printf("valeur lue : %d",buffer);
cout<<"valeur lue : "<<(int)buffer<<"\n"<<endl;
(env)->MonitorExit(obj);
return buffer;
}
/*
* Class: testClass
* Method: WriteCOM
* Signature: (B)Z
*/
JNIEXPORT jboolean JNICALL Java_testClass_WriteCOM(JNIEnv *env, jobject obj, jbyte byte){
(env) -> MonitorEnter(obj);
int buffer=byte;
cout<<"valeur à écrire : "<<(int)byte<<"\n"<<endl;
DWORD* pBytesWritten;
BOOL bol=WriteFile(g_hCOM,&buffer,1,pBytesWritten,NULL);
(env)->MonitorExit(obj);
return bol;
} |
Partager