java로 TCP 프로젝트를 만들고 있습니다. 마이크로 컨트롤러에서 직렬 통신으로 java에서 TCP 연결을 만들고 싶습니다. 멀티 스레딩을 사용하고 싶습니다."java.lang.UnsupportedOperationException : 아직 지원되지 않습니다."
하지만 내 서버와 클라이언트를 실행할 때 내 클라이언트에서 내 서버로 메시지를 보냅니다. 그럼 난 tcpserver는에서 다음과 같은 오류가 있습니다
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();
}
}
}
내 원래 시리얼 통신 코드 :
다음"Exception in thread "Thread-0" java.lang.UnsupportedOperationException: Not supported yet.
at serialPort.openPort(serialPort.java:30)
at Client.run(TCPserver.java:63)"
내 TCPservercode입니다
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);
}
}
}
}
수 있습니다 anybo 도와 줘? 그들이 사용하는 예제를 검토 할 때 https://code.google.com/archive/p/java-simple-serial-connector/
:에 JSSC에 대한 구글 코드 페이지를 통해 찾고
'jssc.SerialPort' 가져 오기에 대한 정보를 제공해주십시오. 어디에서 왔습니까? 어떤 라이브러리와 버전입니까? –
나는 최신 버전 인 jssc libary를 사용합니다 – belmen
"serialPort.java"파일의 코드를 보여줄 수 있습니까? 나는 이것이 당신이 쓴 수업이라고 생각하나요? –