2

I am making a TCP project in java. I want to build a TCP connection in java with serial communication from a microcontroller. 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 :

"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 :

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 :

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?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
belmen
  • 35
  • 1
  • 7
  • 2
    Please provide information about the `jssc.SerialPort` import. Where does it come from: which library and version? – Stefan Freitag Oct 27 '16 at 10:48
  • i use the jssc libary , the newest version – belmen Oct 27 '16 at 19:38
  • Can you show us the code of the file "serialPort.java"? I assume that this is a class you have written, or? – Stefan Freitag Oct 27 '16 at 19:59
  • you can see my code file from my serialport above you in mij question. I have attached with it. And i used the code from my tcpserver from this site --> ndm.wikidot.com/cnsoltcpmultithreading – belmen Oct 27 '16 at 20:17
  • I noticed that you added the Main class. But I am really confused now: The stack trace notes that the problem is located in line 30 of the file serialPort.java. The jssc file [SerialPort.java](https://github.com/scream3r/java-simple-serial-connector/blob/2.8.0/src/java/jssc/SerialPort.java) in the latest release has no code at line 30. Can you please check e.g. by debugging if you are using the right SerialPort class? – Stefan Freitag Oct 28 '16 at 05:29

1 Answers1

0

Looking through the Google Code page for jssc on: 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

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.

I found no reference to the openPort method throwing a UnsupportedOperationException.

Adam J
  • 71
  • 7
  • i think that it is not on the client .. the error .. but on the tcpserver side because my client works like it shoud .. when i use the tcpserver code from this link --> http://ndm.wikidot.com/cnsoltcpmultithreading , and i use the same code for the cliend there are no errors .. but when i import the code form my serial port and i bundle it togheter then i get i error .. – belmen Oct 27 '16 at 19:41