2

Je fais un projet TCP en Java. Je veux construire une connexion TCP en Java avec la communication série d'un microcontrôleur. Je veux faire cela avec Multithreading. Mais quand j'exécute mon serveur et mon client, j'envoie un message de mon client à mon serveur. Ensuite, je l'erreur suivante dans le tcpserver:"java.lang.UnsupportedOperationException: pas encore supporté."

"Exception in thread "Thread-0" java.lang.UnsupportedOperationException: Not supported yet. 
    at serialPort.openPort(serialPort.java:30) 
    at Client.run(TCPserver.java:63)" 

Voici mon TCPservercode:

import java.io.*; 
import java.net.*; 
import java.io.IOException; 
import jssc.SerialPort; 
import jssc.SerialPortException; 
import java.util.Scanner; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import jssc.SerialPortList; 
import jssc.*; 
import jssc.SerialPort; 
import jssc.SerialPortException; 

class TCPServer 
{ 
    public static void main(String argv[]) throws Exception 
    { 
     ServerSocket welcomeSocket = new ServerSocket(6789); 
     SerialPort serialPort = new SerialPort("COM3"); 

     while(true) 
     { 
      Socket connectionSocket = welcomeSocket.accept(); 
      if (connectionSocket != null) 
      { 
       Client client = new Client(connectionSocket); 
       client.start(); 
      } 
     } 
    } 
} 

class Client extends Thread 
{ 
    private Socket connectionSocket; 
    private String clientSentence; 
    private String capitalizedSentence; 
    private BufferedReader inFromClient; 
    private DataOutputStream outToClient; 

    public Client(Socket c) throws IOException 
    { 
     connectionSocket = c; 
    } 

    public void run() 
    { 
     try 
     {  
      inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream())); 
      outToClient = new DataOutputStream(connectionSocket.getOutputStream()); 
      clientSentence = inFromClient.readLine(); 
      capitalizedSentence = clientSentence.toUpperCase() + '\n'; 
      outToClient.writeBytes(capitalizedSentence); 
      serialPort.openPort();//Open serial port 

      serialPort.setParams(SerialPort.BAUDRATE_115200, 
          SerialPort.DATABITS_8, 
          SerialPort.STOPBITS_1, 
          SerialPort.PARITY_NONE); 
      System.out.println("Received: " + clientSentence); 
      serialPort.writeString(clientSentence + "\r\n"); 
      //Thread.sleep(1000); 
      String buffer = serialPort.readString();//Read 10 bytes from seri 
      // Thread.sleep(1000); 

      outToClient.writeBytes(buffer); 
      System.out.println(buffer); 
      serialPort.closePort(); 
      connectionSocket.close(); 

     } catch (IOException ex) { 
      Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex); 
     } 

    } 

} 

et mon code tcpclient:

import java.io.*; 
import java.net.*; 

class TCPClient 
{ 
    public static void main(String argv[]) throws Exception 
    { 
     while(true){ 
     String sentence;    
     String modifiedSentence;  

     Socket clientSocket = new Socket("localhost", 6789); 

     BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in)); 
     DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());   
     BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 

     sentence = inFromUser.readLine(); 
     outToServer.writeBytes(sentence + '\n'); 
     modifiedSentence = inFromServer.readLine(); 
     System.out.println("FROM SERVER: " + modifiedSentence); 

     clientSocket.close(); 
     } 
    } 
} 

Mon origine Code de communication série:

package sensornode_Jens; 

import java.io.IOException; 
import jssc.SerialPort; 
import jssc.SerialPortException; 
import java.util.Scanner; 
import jssc.SerialPortList; 
import jssc.*; 
import jssc.SerialPort; import jssc.SerialPortException; 


public class Main { 

public static void main(String[] args) { 

    // getting serial ports list into the array 
String[] portNames = SerialPortList.getPortNames(); 

if (portNames.length == 0) { 
    System.out.println("There are no serial-ports :(You can use an emulator, such ad VSPE, to create a virtual serial port."); 
    System.out.println("Press Enter to exit..."); 
    try { 
     System.in.read(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
    return; 
} 

for (int i = 0; i < portNames.length; i++){ 
    System.out.println(portNames[i]); 
    //// 

    SerialPort serialPort = new SerialPort("/dev/ttyACM0"); 
    try { 
     serialPort.openPort();//Open serial port 
     serialPort.setParams(SerialPort.BAUDRATE_115200, 
          SerialPort.DATABITS_8, 
          SerialPort.STOPBITS_1, 
          SerialPort.PARITY_NONE);//Set params. Also you can set params by this string: serialPort.setParams(9600, 8, 1, 0); 
     Scanner input = new Scanner(System.in); 
     String scommand = input.nextLine(); 

     serialPort.writeString(scommand + "\r\n"); 

     String buffer = serialPort.readString();//Read 10 bytes from serial port 

     System.out.println(buffer); 

     serialPort.closePort(); 

    } 
    catch (SerialPortException ex) { 
     System.out.println(ex); 
    } 
} 
} 
} 

Can anybo dy m'aider?

+2

Veuillez fournir des informations sur l'importation 'jssc.SerialPort'. D'où vient-elle: quelle bibliothèque et quelle version? –

+0

J'utilise la librairie jssc, la nouvelle version – belmen

+0

Pouvez-vous nous montrer le code du fichier "serialPort.java"? Je suppose que c'est un cours que vous avez écrit, ou? –

Répondre

0

En regardant à travers la page Google Code pour JSSC sur: https://code.google.com/archive/p/java-simple-serial-connector/

En examinant les exemples qu'ils utilisent:

SerialPort serialPort = new SerialPort("COM1"); 

que je vois mis dans la classe TCPServer mais pas dans la classe client où

serialPort.openPort(); 

est appelée.

Vous devrez peut-être initialiser serialPort dans votre classe Client au lieu de votre classe TCPServer, avant d'appeler openPort.

Je n'ai trouvé aucune référence à la méthode openPort lançant une exception UnsupportedOperationException.

+0

je pense que ce n'est pas sur le client .. l'erreur .. mais sur le côté tcpserver parce que mon client fonctionne comme il shoud .. quand j'utilise le code tcpserver à partir de ce lien -> http: //ndm.wikidot. com/cnsoltcpmultithreading, et j'utilise le même code pour le cliend il n'y a pas d'erreurs .. mais quand j'importe le code former mon port série et je bundle togheter alors je reçois une erreur .. – belmen