Bonjour,
J'utilise javax.comm pour lire un port COM avec mon appli java (j'ai le meme problème avec RXTX), lorsque j'ai une IOException (sans doute du à une erreur de connexion entre le Lantronix Wi-Port et mon PC où je redirige le flux vers le port COM); je ferme mon port et le reouvre et en general ca remarche. Mais parfois j'obtiens une PortInUseException lors de la réouverture du port alors que le portId.isCurrentlyOwned() renvoi "false".
Note: parfois le port apparaît miraculeusement comme non occupé et ca remarche.
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 Trying to open serial port 'COM20'! Successfully opened serial port 'COM20'! CA MARCHE- Tue Aug 20 16:48:52 CEST 2013 CA MARCHE- Tue Aug 20 16:48:54 CEST 2013 CA MARCHE- Tue Aug 20 16:48:56 CEST 2013 (plus tard) CA MARCHE- Tue Aug 20 18:55:47 CEST 2013 CA MARCHE- Tue Aug 20 18:55:49 CEST 2013 CA MARCHE- Tue Aug 20 18:55:51 CEST 2013 waiting ... waiting ... waiting ... waiting ... waiting ... echec ... java.io.IOException: write error at com.sun.comm.Win32SerialPort.write(Win32SerialPort.java:677) at com.sun.comm.Win32SerialPort.write(Win32SerialPort.java:664) at com.sun.comm.Win32SerialOutputStream.write(Win32SerialOutputStream.java:34) at java.io.DataOutputStream.write(Unknown Source) at projet.RS232.demandecontext1(RS232.java:774) at projet.RS232.readBytes(RS232.java:441) at projet.GUI$Thread1.run(GUI.java:1981) at java.lang.Thread.run(Unknown Source) trying to close system succes to close system Trying to to open serial port 'COM20'! Failed to open serial port 'COM20'! javax.comm.PortInUseException: Port currently owned by Unknown Windows Application at javax.comm.CommPortIdentifier.open(CommPortIdentifier.java:337) at projet.RS232.openConnection(RS232.java:333) at projet.GUI$Thread1.run(GUI.java:1988) at java.lang.Thread.run(Unknown Source) Trying to to open serial port 'COM20'! Failed to open serial port 'COM20'! javax.comm.PortInUseException: Port currently owned by Unknown Windows Application at javax.comm.CommPortIdentifier.open(CommPortIdentifier.java:337) at projet.RS232.openConnection(RS232.java:333) at projet.GUI$Thread1.run(GUI.java:1988) at java.lang.Thread.run(Unknown Source) Trying to to open serial port 'COM20'! Failed to open serial port 'COM20'! javax.comm.PortInUseException: Port currently owned by Unknown Windows Application at javax.comm.CommPortIdentifier.open(CommPortIdentifier.java:337) at projet.RS232.openConnection(RS232.java:333) at projet.GUI$Thread1.run(GUI.java:1988) at java.lang.Thread.run(Unknown Source)
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 //dans le GUI cmdSTART.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent evt){ lancer(); } private void lancer(){ if(rs232.openConnection()){ animated1=true; t1=new Thread(new Thread1()); t1.setPriority(7); t1.start(); marche=true; } } class Thread1 implements Runnable{ public void run(){ while(animated1){ try{ rs232.readBytes(); } catch(Exception e){ e.printStackTrace(); rs232.closeConnection(); boolean nbbyte=false; while(nbbyte==false){ nbbyte=rs232.openConnection(); try{ Thread.sleep(5000); } catch(InterruptedException e1){ e1.printStackTrace(); } } } } } }
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 public void readBytes() throws IOException,InterruptedException{ demandecontext1(); receptdata(); traitementdata(); try{ Thread.sleep(300); } catch(InterruptedException e){ e.printStackTrace(); } } private void demandecontext1() throws IOException,InterruptedException{ /*ligne 774*/out1x.write(91); out1x.write(91); Thread.sleep(100); out1x.write(48); Thread.sleep(100); out1x.write(65); Thread.sleep(100); out1x.write(48); Thread.sleep(100); out1x.write(48); Thread.sleep(100); out1x.write(48); Thread.sleep(100); out1x.write(49); Thread.sleep(100); out1x.write(93); Thread.sleep(1000); } private void receptdata() throws IOException,InterruptedException{ byte bufferByte[]=new byte[1024] int nbbyte=in1x.available(); int comp=0; while(nbbyte==0&&comp<5){ System.out.println("waiting ..."); Thread.sleep(1000); nbbyte=in1x.available(); comp++; } if(comp==5){ System.out.println("echec ..."); return; } nbbyte=in1x.read(bufferByte,0,nbbyte); System.out.println("CA MARCHE- "+new Date()); //+Gestion des bytes recu } public boolean openConnection(){ if(portId1x.isCurrentlyOwned()){ System.out.println("Port ouvert par une autre application"); System.out.println(portId1x.getCurrentOwner()); return false; } else{ try{ System.out.println("Trying to to open serial port '"+com1x+"'!"); boolean flag1=portId1x.isCurrentlyOwned(); if(flag1){ System.out.println("port ="+!flag1); } /*ligne 333*/ serialPort1x=(SerialPort) portId1x.open(appName,openTimeOut); System.out.println("Successfully opened serial port '"+serialPort1x.getName()+"'!"); // Setting the serial connection as requested... serialPort1x.setSerialPortParams(baudrate1,databits,stopbit,parity); in1x=new DataInputStream(serialPort1x.getInputStream()); out1x=new DataOutputStream(serialPort1x.getOutputStream()); return true; } catch(Exception e){ System.out.println("Failed to open serial port '"+serialPort1x[p].getName()+"'!"); e.printStackTrace(); return false; // JOptionPane.showMessageDialog(null,"Erreur lors de l'ouverture du port " // + com, "ERREUR", JOptionPane.ERROR_MESSAGE); // System.exit(1); // sortie de l'appli } } } public void closeConnection(){ try{ System.out.println("trying to close system "); in1x.close(); out1x.close(); serialPort1x.close(); System.out.println("succes to close system "); } catch(IOException e){ System.out.println("failed to close intrusion system "); System.out.println("Cannot open and / or close serial connection!Details:\n"+e.getMessage()); //System.exit(1); } }
Partager