I need help interfacing jSerialComm with arduino

I need help interfacing a java program with an arduino using jSerialComm.SerialPort or via the COM port. I know the arduino code works fine, because of the serial monitor. It takes the number provided from the COM port and subtracts 10 to set the angle of a servo. However, my java code doesn't work. The Arduino blinks as if the port opens, but the servo remains stationary. I've tweeked the java code a bit, using the serialPort.writeBytes but got the same result. If anyone knows how to fix my code, or a working alternative to jSerialComm, that would be greatly appreciated.

Java code:

package com.company;

import com.fazecast.jSerialComm.SerialPort;

import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;

public class Main {
    public static SerialPort serialPort;

    public static void main(String[] args) throws Exception {
        serialPort = SerialPort.getCommPort("COM5");
        serialPort.setComPortParameters(9600,8,1,0);
        serialPort.setComPortTimeouts(SerialPort.TIMEOUT_NONBLOCKING,0,0);
        serialPort.openPort(20);

        PrintWriter out = new PrintWriter(serialPort.getOutputStream(),true);
        out.println("50");
        out.flush();
    }
}

Arduino Code:

#include <Servo.h>
// Declare the Servo pin 
        int servoPin = 3;
        long incomingByte; // for incoming serial data
// Create a servo object 
        Servo Servo1;
        void setup() {
            Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
            Servo1.attach(servoPin);
            Servo1.write(0);
        }
        void loop(){
            if (Serial.available())
            {
                int state = Serial.parseInt();
        
                if (state < 10)
                {
                    Serial.print(">");
                    Serial.println(state);
                    Serial.println("cannost execute command, too low number");
                }
        
                if (state >= 10 && state <= 190)
                {
                    state -= 10;
                    Serial.println(state);
                    Servo1.write(state);
                }
            }
        }

the following works on Windows 10 using Java version 1.8

// jSerialComm read keyboard and transmit to COM port - read response

// compile and run commands - note that jSerialComm-1.3.11.jar should be in same directory as .java file
// javac -cp F:\Programming\JAVA\JSerialComm\jSerialComm-1.3.11.jar;.  SimpleTerminal.java
// java -cp F:\Programming\JAVA\JSerialComm\jSerialComm-1.3.11.jar;.  SimpleTerminal

import com.fazecast.jSerialComm.*;
import java.util.*;

public class SimpleTerminal {
  public static void main(String[] args) { 
  Scanner console = new Scanner(System.in);
  System.out.println("List COM ports");
  SerialPort comPorts[] = SerialPort.getCommPorts();
  for (int i = 0; i < comPorts.length; i++)   
          System.out.println("comPorts[" + i + "] = " + comPorts[i].getDescriptivePortName());
  int port = 1;     // array index to select COM port
  comPorts[port].openPort();
  System.out.println("open port comPorts[" + port + "]  " + comPorts[port].getDescriptivePortName());
  comPorts[port].setBaudRate(115200);
  try {
    while (true)
    {
      // if keyboard token entered read it
      if(System.in.available() > 0)
           {
           //System.out.println("enter chars ");
           String s = console.nextLine() + "\n";                // read token
           byte[] writeBuffer=s.getBytes() ;
           comPorts[port].writeBytes(writeBuffer, writeBuffer.length);
           //System.out.println("write " + writeBuffer.length);
          }
     // read serial port  and display data
      while (comPorts[port].bytesAvailable() > 0)
          {
          byte[] readBuffer = new byte[comPorts[port].bytesAvailable()];
          int numRead = comPorts[port].readBytes(readBuffer, readBuffer.length);
          //System.out.print("Read " + numRead + " bytes from COM port: ");
          for (int i = 0; i < readBuffer.length; i++)   
             System.out.print((char)readBuffer[i]);
          //System.out.println();
          }
     }
  } catch (Exception e) { e.printStackTrace(); }
  comPorts[port].closePort();  
}
}

the program loops waiting for

  1. a token to be entered on the keyboard (text terminated by newline) when is transmitted over the COM port
  2. serial data received from COM port which is then displayed on console

if you wish to receive keyboard input character by character created a GUI and wait on KeyEvents

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.