2

我正在java中制作一个TCP项目。 我想在java中使用来自微控制器的串行通信建立TCP连接。 我想用多线程来做到这一点。“java.lang.UnsupportedOperationException:不支持。”

但是,当我运行我的服务器和客户端,并从客户端发送消息到我的服务器。然后,我有以下错误的TCPserver下:

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

这里是我的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); 
     } 

    } 

} 

和我的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(); 
     } 
    } 
} 

我原来的串口通信代码:

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 can Anybo dy帮助我?

+2

请提供关于'jssc.SerialPort'导入的信息。它来自哪里:哪个库和版本? –

+0

我使用jssc库文件,最新版本 – belmen

+0

你能告诉我们文件“serialPort.java”的代码吗?我认为这是你写的一堂课,或者? –

回答

0

通过谷歌代码页寻找JSSC上:https://code.google.com/archive/p/java-simple-serial-connector/

在审查他们使用的例子:

SerialPort serialPort = new SerialPort("COM1"); 

与我在TCPSERVER类请参见设置,但不是在客户端类,其中

serialPort.openPort(); 

被调用。

因此,在调用openPort之前,您可能需要在Client类中初始化serialPort而不是TCPServer类。

我发现没有引用抛出UnsupportedOperationException的openPort方法。

+0

我认为这不是在客户端..错误..但在tcpserver端,因为我的客户端工作就像它shoud ..当我使用此链接的tcpserver代码 - > http://ndm.wikidot。 COM/cnsoltcpmultithreading,我使用相同的代码为cliend有没有错误..但是当我从我的串行端口导入代码形式,我捆绑它togheter然后我得到我的错误.. – belmen

相关问题