简体   繁体   English

“ java.lang.UnsupportedOperationException:尚不支持。”

[英]“java.lang.UnsupportedOperationException: Not supported yet.”

I am making a TCP project in java. 我正在用Java做一个TCP项目。 I want to build a TCP connection in java with serial communication from a microcontroller. 我想用来自微控制器的串行通讯在Java中建立TCP连接。 I want to do that with Multithreading. 我想用多线程来做到这一点。

But when I run my server and client , and I send a message from my client to my server. 但是,当我运行服务器和客户端时,我从客户端向服务器发送了一条消息。 Then I have the following error in the TCPserver : 然后我在TCPserver中出现以下错误:

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

Here is my TCPservercode : 这是我的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);
        }

    }

}

and my TCPclient 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();
        }
    }
}

My original serial communication code : 我原来的串行通讯代码:

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 anybody help me? 有谁能够帮助我?

Looking through the Google Code page for jssc on: https://code.google.com/archive/p/java-simple-serial-connector/ 在以下网址浏览jssc的Google代码页: https//code.google.com/archive/p/java-simple-serial-connector/

When reviewing the examples they use: 在查看示例时,他们使用:

SerialPort serialPort = new SerialPort("COM1");

Which I see set in the TCPServer class but not in the Client class where 我看到在TCPServer类中设置了,但在Client类中没有设置

serialPort.openPort();

is called. 叫做。

So you may need to initialize serialPort inside your Client class instead of your TCPServer class, before calling openPort on it. 因此,在调用openPort之前,可能需要在Client类而不是TCPServer类中初始化serialPort。

I found no reference to the openPort method throwing a UnsupportedOperationException. 我找不到对引发UnsupportedOperationException的openPort方法的引用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “java.lang.UnsupportedOperationException:尚不支持。” - “java.lang.UnsupportedOperationException: Not supported yet.” Java Dropbox HttpServletRequest(java.lang.UnsupportedOperationException:尚不支持。) - Java Dropbox HttpServletRequest ( java.lang.UnsupportedOperationException: Not supported yet.) java.lang.UnsupportedOperationException:尚不支持 - java.lang.UnsupportedOperationException: Not supported yet 由于“线程“AWT-EventQueue-0”中的异常java.lang.UnsupportedOperationException:尚不支持,我的程序无法读取密钥。 - my program cant read the keys because of "Exception in thread "AWT-EventQueue-0" java.lang.UnsupportedOperationException: Not supported yet." Duke Fast Deduplication:java.lang.UnsupportedOperationException:尚不支持操作? - Duke Fast Deduplication: java.lang.UnsupportedOperationException: Operation not yet supported? UnsupportedOperationException:不支持。 在jframe中 - UnsupportedOperationException: Not supported yet. in jframe 由java.lang.UnsupportedOperationException引起:AdapterView不支持addView(View,layoutParams) - Caused by java.lang.UnsupportedOperationException: addView(View,layoutParams) is not supported in the AdapterView DBFit java.lang.UnsupportedOperationException: 不支持类型 İNT - DBFit java.lang.UnsupportedOperationException: Type İNT is not supported java.lang.UnsupportedOperationException:option不是受支持的字段类型 - java.lang.UnsupportedOperationException: option is not a supported field type Tomcat连接池-java.lang.UnsupportedOperationException:BasicDataSource不支持 - Tomcat Connection Pooling - java.lang.UnsupportedOperationException: Not supported by BasicDataSource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM