IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Entrée/Sortie Java Discussion :

Test si il y a quelque chose sur le port COM


Sujet :

Entrée/Sortie Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    70
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : Belgique

    Informations forums :
    Inscription : Novembre 2007
    Messages : 70
    Par défaut Test si il y a quelque chose sur le port COM
    Bonjour,

    J'aimerais savoir comment tester si ma carte est branchée sur le port COM avec javax.comm. Etant donné qu'elle ne doit pas retourner d'écho ou d'ACK, comment puis-je simplement tester que ma carte est branchée ou non ?

    Merci à vous.

    PS : cela me servirait à désactiver des choses dans l'affichage.

  2. #2
    Membre très actif
    Avatar de william44290
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Juin 2009
    Messages
    400
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France

    Informations professionnelles :
    Activité : Responsable de service informatique

    Informations forums :
    Inscription : Juin 2009
    Messages : 400
    Par défaut
    Bonjour, avec les les bits de contrôle tu devrais pouvoir vérifier que quelque chose est connecté au port com.

    le rebouclage classique tient par DTR/DSR et CTS/RTS il est probable qu'en testant le CTS tu pourrais répondre à ta question.

    Ci-joint un code perso.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
     
    package wSerial;
     
     
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.util.Enumeration;
    import java.util.TooManyListenersException;
     
    import javax.comm.CommPortIdentifier;
    import javax.comm.NoSuchPortException;
    import javax.comm.PortInUseException;
    import javax.comm.SerialPort;
    import javax.comm.SerialPortEvent;
    import javax.comm.SerialPortEventListener;
    import javax.comm.UnsupportedCommOperationException;
     
    import wOutils.Gp;
    import wPtime.TimeEllaspe;
     
    import com.sun.comm.Win32Driver;
     
    public class Serial extends Thread implements SerialPortEventListener {
    	public static int LENGTH_TRAME=1024;
    	public static String ACKPF="ackPF";
    	// Gestion de la fin de trame
    	// Sur une Chaine
    	public static final int ACKCONTROL_STRING=0;
    	// Sur un CR LF
    	public static final int ACKCONTROL_STRINGCRLF=1;
    	// Sur un BYTE de controle (souvent 05 ETX)
    	public static final int ACKCONTROL_BYTE=2;
    	// Sur un Chaine encapsulée par des byte de controle
    	public static final int ACKCONTROL_STX_BYTES_ETX1_ETX2=3;
    	// Sur un BYTE de controle (Simple ACK/NACK)
    	public static final int ACKCONTROL_ACKPF=4;
    	//
    	OutputStream out=null;
    	InputStreamReader in=null;
    	SerialPort serialPort;
    	String portCom=null;
    	boolean debug=false;
    	boolean open=false;
    	boolean trace=false;
    	//
    	int ackControl;
    	boolean ackPF=false;
    	byte[] buffRet=new byte[LENGTH_TRAME];
    	int prefix;
    	int suffix1;
    	int suffix2;
    	int trameLength;
    	private long sleep;
    	public Serial(Gp gpAbc,String portCom,int ackControl){
    		this.portCom=portCom.trim();
    		if (this.portCom.charAt(this.portCom.length()-1)==':'){
    			this.portCom=this.portCom.substring(0, this.portCom.length()-1);
    		}
    		this.ackControl=ackControl;
    		if (gpAbc.m_user.equals("9000DEV")){
    			this.trace=true;
    		}
    		buffRet[0]='\0';
    		start();
    	}
    	public void run(){
    		setSleep(150);
    		open();
    	}
    	public boolean open(){
    		Win32Driver w32Driver = new Win32Driver();
    		w32Driver.initialize();
     
    		CommPortIdentifier portId=null;
    		boolean ret=true;
    		if (ret){
    			if (portCom==null) {
    				portCom=listePortsDispo().getName();
    				if (portCom==null){
    					Gp.MB("Port = "+portCom+", Inconnu");
    					ret=false;
    				}
    			}
    		}
    		if (ret){
    			try {
    				portId=CommPortIdentifier.getPortIdentifier(portCom);
    			} catch (NoSuchPortException arg0) { 
    				Gp.MB("Port = "+portCom+", Inconnu ou indisponible");
    				ret=false;
    			}
    		}
    		if (ret){
    			try {
    				int timeOut=2000;
    				serialPort = (SerialPort) portId.open("ModeEvenement", timeOut);
    			} catch (PortInUseException e) {
    				Gp.MB("Port = "+portId.getName()+", Occupé");
    				ret=false;
    			}
    		}
    		if (ret){
    			serialPort.setOutputBufferSize( 40000 );
    			if (!isCTS()){
    				Gp.MB("Port = "+portId.getName()+", Brancher ou Allumer le périphérique, Merci");
    				ret=false;
    			}
    		}
    		if (ret){
    			try {
    				in=new InputStreamReader(serialPort.getInputStream());
    			} catch (IOException arg0) { arg0.printStackTrace(); }
    		}
    		if (ret){
    			try {
    				serialPort.addEventListener(this);
    			} catch (TooManyListenersException arg0) { arg0.printStackTrace(); }
    			serialPort.notifyOnDataAvailable(true);
    			serialPort.notifyOnBreakInterrupt(true);
    			serialPort.notifyOnCarrierDetect(true);
    			serialPort.notifyOnCTS(true);
    			serialPort.notifyOnDataAvailable(true);
    			serialPort.notifyOnDSR(true);
    			serialPort.notifyOnFramingError(true);
    			serialPort.notifyOnOutputEmpty(true);
    			serialPort.notifyOnOverrunError(true);
    			serialPort.notifyOnParityError(true);
    			serialPort.notifyOnRingIndicator(true);
    		}
    		open=ret;
    		return ret;
    	}
    	public boolean isOpenTimeOut(){
    		boolean ret=open;
    		TimeEllaspe te=new TimeEllaspe(1);
    		while (!ret){
    			if (te.testTimeOut()){
    				if (te.isTimeOut()){
    					ret=false;
    				}
    				break;
    			} else {
    				ret=open;
    			}
    		}
    		return ret;
    	}
    	public boolean isOpen() {
    		return open;
    	}
    	public boolean open(int speed, String parity, int nbbit,int nbstop){
    		boolean ret=open();
    		if (ret){
    			setPortParams(speed, parity, nbbit,nbstop);
    		}
    		return ret;
    	}
    	public void setPortParams(int speed, String parity, int nbbit,int nbstop){
    		int iparity=0;
    		if (parity=="N") {
    			// SANS
    			iparity=SerialPort.PARITY_NONE;
    		} else if (parity=="O") {
    			// IMPAIR
    			iparity=SerialPort.PARITY_ODD;
    		} if (parity=="E") {
    			// PAIR
    			iparity=SerialPort.PARITY_EVEN;
    		} 
    		try {
    			serialPort.setSerialPortParams(speed,nbbit,nbstop,iparity);
    		} catch (UnsupportedCommOperationException arg0) { arg0.printStackTrace(); }
    	}
    	public void setTrameParams(int prefix, int length, int suffix1,int suffix2) {
    		this.prefix=prefix;
    		this.trameLength=length;
    		this.suffix1=suffix1;
    		this.suffix2=suffix2;
    	}
    	public void close(){
    		if (open){
    			try {
    				if (out!=null){
    					out.close();
    				}
    				if (in!=null){
    					in.close();
    				}
    			} catch (IOException arg0) { arg0.printStackTrace(); }
    			serialPort.removeEventListener();
    			serialPort.close();
    			open=false;
    		}
    	}
     
    	public CommPortIdentifier listePortsDispo()
    	{
    		CommPortIdentifier ret=null;
    		CommPortIdentifier tmp;
    		Enumeration<?> portList=CommPortIdentifier.getPortIdentifiers();
    		if (portList==null)
    		{
    			System.err.println("Aucun port de communication détecté" );
    			return null;
    		}
    		while (portList.hasMoreElements())
    		{
    			tmp=(CommPortIdentifier)portList.nextElement();
    			if (ret==null){ // Valorise le premier port com trouvé
    				ret=tmp;
    				//break;
    			}
    		}
    		return ret;
    	}
    	public int getAckControl() {
    		return ackControl;
    	}
    	public void setAckControl(int ackControl) {
    		this.ackControl = ackControl;
    	}
    	public void setDebug(boolean enable){
    		debug=enable;
    	}
    	public void write(String txt) {
    		if (debug){
    			switch(ackControl){
    			case ACKCONTROL_ACKPF:
    				Gp.ad(""+txt);
    			case ACKCONTROL_BYTE:
    				//Gp.ad(""+txt);
    				break;
    			}
    		}
    		try {
    			out=serialPort.getOutputStream();
    			out.write(txt.getBytes());
    			out.flush();
    		} catch (IOException arg0) { arg0.printStackTrace(); }
    	}
    	public void write(int i) {
    		if (debug){
    			switch(ackControl){
    			case ACKCONTROL_ACKPF:
    				Gp.ad(""+(char)i);
    				break;
    			case ACKCONTROL_BYTE:
    				//Gp.ad(""+i);
    				break;
    			}
    		}
    		try {
    			out=serialPort.getOutputStream();
    			out.write(i);
    			out.flush();
    		} catch (IOException arg0) { arg0.printStackTrace(); }		
    	}
    	public void serialEvent(SerialPortEvent ev) {
    		switch (ev.getEventType()) {
    		case SerialPortEvent.BI :
    			//System.out.println("BI");
    			break;
    		case SerialPortEvent.OE :
    			//System.out.println("OE");
    			break;
    		case SerialPortEvent.FE :
    			//System.out.println("FE");
    			break;
    		case SerialPortEvent.PE :
    			//System.out.println("PE");
    			break;
    		case SerialPortEvent.CD :
    			//System.out.println("CD");
    			break;
    		case SerialPortEvent.CTS :
    			//System.out.println("CTS");
    			break;
    		case SerialPortEvent.DSR :
    			//System.out.println("DSR");
    			break;
    		case SerialPortEvent.RI :
    			//System.out.println("RI");
    			break;
    		case SerialPortEvent.OUTPUT_BUFFER_EMPTY :
    			//System.out.println("OBE");
    			break;
    		case SerialPortEvent.DATA_AVAILABLE :
    			readData();
    		}
    	}
    	private void readData() {
    		// Transfert les Datas du port vers un buffer interne
    		byte[] bufferRead=new byte[128];
    		try {  // Laisse du temps pour que TOUTE la trame arrive suite à l'interruption DATA_AVAILABLE
    			Thread.sleep(sleep);
    		} catch (InterruptedException arg0) { arg0.printStackTrace(); } 
     
    		// TOUTE la trame est là !!!!!
    		int ibuffer=0;
    		try {
    			int c = in.read() & 0xff;
    			while(c!=-1 && bufferRead.length>ibuffer){
    				bufferRead[ibuffer]=(byte)c;
    				ibuffer++;
    				if (debug){
    					switch(ackControl){
    					case ACKCONTROL_STRING:
    						Gp.ad("<< "+(char) c);
    						break;
    					case ACKCONTROL_ACKPF:
    						Gp.ad("<< "+(char) c);
    						break;
    					case ACKCONTROL_BYTE:
    						Gp.ad("<< "+ c);
    						break;
    					case ACKCONTROL_STX_BYTES_ETX1_ETX2:
    						Gp.ad("<< "+(char) c);
    						break;
    					}
    				}
    				if (in.ready()) {
    					c=in.read()& 0xff;
    				} else {
    					c=-1;
    				}
    			}
    		} catch (IOException arg0) { arg0.printStackTrace(); }
     
    		// Transfert le buffer interne
    		switch(ackControl){
    		case ACKCONTROL_STRING:
    			break;
    		case ACKCONTROL_STRINGCRLF:
    			break;
    		case ACKCONTROL_ACKPF:
    			String ctrl=ACKPF+(char) 13+(char) 10;
    			buffRet[0]='\0';
    			//String retour="";
    			boolean comp=true;
    			for (int i=0;i<ACKPF.length();i++){
    				buffRet[i]=bufferRead[i];
    				//retour=retour+(char)bufferRead[i];
    				if (ctrl.charAt(i)!=(char)buffRet[i]){
    					comp=false;
    					break;
    				}
    			}
     
    			if (comp){
    				setAckPF(true);
    			}
    			break;
    		case ACKCONTROL_BYTE:
    			for (int i=0;i<bufferRead.length;i++){
    				buffRet[i]=bufferRead[i];
    			}
    			setAckPF(true);
    			break;
    		case ACKCONTROL_STX_BYTES_ETX1_ETX2:
    			buffRet[0]='\0';
    			int i=0;
    			for (;i<bufferRead.length;i++){
    				if (bufferRead[i]==prefix){
    					i++;
    					break;
    				}
    			}
    			//for (int j=0;j<buffRet.length && i<bufferRead.length;j++){
    			for (int j=0;j<buffRet.length ;j++){
    				if (bufferRead[i]!=suffix1){
    					buffRet[j]=bufferRead[i];
    					buffRet[j+1]='\0';
    					i++;
    				} else {
    					i++;
    					break;
    				}
    			}
    			for (;i<bufferRead.length;i++){
    				if (bufferRead[i]==suffix2){
    					setAckPF(true);
    					break;
    				}
    			}
    			break;
    		}
    	}
    	public long getSleep() {
    		return sleep;
    	}
    	public void setSleep(long sleep) {
    		this.sleep = sleep;
    	}
    	public void setRTS(boolean enable){
    		if (debug){
    			Gp.ad("RTS "+enable+(char) 13);
    		}
    		serialPort.setRTS(enable);
    	}
    	public void setDTR(boolean enable){
    		if (debug){
    			Gp.ad("DTR "+enable+(char) 13);
    		}
    		serialPort.setDTR(enable);
    	}
    	public boolean isCTS(){
    		boolean ret=serialPort.isCTS();
    		if (debug){
    			Gp.ad("isCTS "+ret+(char) 13);
    		}
    		return ret;
    	}
    	public byte[] getBTrame(){
    		return buffRet;
    	}
    	public String getSTrame() {
    		String ret="";
    		for (int i=0;i<trameLength && buffRet[i]!='\0';i++){
    			ret=ret+(char)buffRet[i];
    		}
    		buffRet[0]='\0';
    		return ret;
    	}
    	public void setAckPF(boolean ackPF) {
    		this.ackPF = ackPF;
    	}
    	public boolean isAckPF() {
    		return ackPF;
    	}
    	public boolean isTrace() {
    		return trace;
    	}
    	public void setTrace(boolean trace) {
    		this.trace = trace;
    	}
    	public String getPortCom() {
    		isOpenTimeOut();
    		return portCom;
    	}
    }

  3. #3
    Modérateur

    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    12 585
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 12 585
    Par défaut
    On peut plus ou moins détecter que quelque chose est présent dans le port COM... Mais pas vérifier quoi.

    Il y a une raison pour laquelle aucun programme ne le devinait jamais, à l'époque.
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Novembre 2007
    Messages
    70
    Détails du profil
    Informations personnelles :
    Âge : 37
    Localisation : Belgique

    Informations forums :
    Inscription : Novembre 2007
    Messages : 70
    Par défaut
    Je vous remercie pour votre aide.

    Je vais m'inspirer d'une partie de ton code william44290, merci

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Encercler quelque chose sur une image
    Par piotrr dans le forum Word
    Réponses: 3
    Dernier message: 11/01/2018, 12h55
  2. test d'envoi sur le port com
    Par sylviek dans le forum Entrée/Sortie
    Réponses: 3
    Dernier message: 15/10/2006, 13h08
  3. Comment obtenir cette fenetre me permettant de chercher quelque chose sur mon disque
    Par budylove dans le forum Balisage (X)HTML et validation W3C
    Réponses: 11
    Dernier message: 15/05/2006, 15h27

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo