Serial communication

Hi guys, i trying to make a USB communication with my arduino UNO, but does not work as I want.
I need to send a command by reading a txt file. I developed a code in JAVA that reads the txt file, converts it and send it to the USB. My txt file has the following lines, that the 3 digits number is the most important part and its where my problem is(this part have to be 000 until 179), and the conversion is after the equals. My code on Arduino is the attachment “proteussimu”

move 1 030 = 11030
move 1 150 = 11150
move 1 090 = 11090

what is wrong with my code ?

proteussimu.ino (5.89 KB)

mov = Serial.read();//lê os dados da porta serial
     //Serial.readBytes(mov,5);
    // buf = mov;
      //mov = Serial.readString();
      Serial.println(mov);
      //mov = mov.toInt();
      //mov = Serial.parseInt();
      dado  = mov.substring(0,1).toInt();
      junta = mov.substring(1,2).toInt();

mov is a single character. Calling substring on it is not going to work.

You should probably spend some time on Robin2's Serial Input Basics thread.

Maybe you can get some ideas from the updated Serial Input Basics thread.

And as a side note, do yourself a favour and forget that String (capital S) exists.

an example of using Java jSerialComm to transmit integer data over a serial line and an Arduino to parse it

// jSerialComm read integer and transmit to COM port

// java -cp D:\Programming\JAVA\JSerialComm\jSerialComm-1.3.11.jar;.  SerialTransmitInteger

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

public class SerialTransmitInteger {
  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;
  if(!comPorts[port].openPort())
    {  System.out.println("unable to open port " + comPorts[port].getDescriptivePortName() + "\n"); return; };
  comPorts[1].setBaudRate(115200);
  try {
    while (true)
    {
      // transmit integer to serial port
      System.out.print("enter an integer ");
      int test = console.nextInt();                // read intger
      //System.out.println("you entered " + test);   // display it
      byte[] writeBuffer=(Integer.toString(test) + "\n").getBytes();
      comPorts[port].writeBytes(writeBuffer, writeBuffer.length);
     // read serial port for response and display it
      while (comPorts[port].bytesAvailable() == 0)
         Thread.sleep(20);
      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[1].closePort();  
}
}

arduino code

// SERIAL: read and echo integer value
void setup() {
  // put your setup code here, to run once:
    Serial.begin(115200);
    Serial.setTimeout(60*60*1000ul);
}
void loop() {
  //Serial.print("enter integer ? ");
  int data=Serial.parseInt();
  Serial.print("Arduino received integer = ");
  Serial.println(data);

a run gives

List COM ports
comPorts[0] = Communications Port (COM1)
comPorts[1] = Arduino MKR FOX 1200 (COM7)
enter an integer 123
Read 32 bytes from COM port: Arduino received integer = 123

enter an integer  6789
Read 33 bytes from COM port: Arduino received integer = 6789

enter an integer  67890
Read 33 bytes from COM port: Arduino received integer = 2354

note that an int on the Arduino Uno is 16bits (Java is 32 bits) which is why the last value entered is parsed incorrectly

horace: enter an integer  67890 Read 33 bytes from COM port: Arduino received integer = 2354

note that an int on the Arduino Uno is 16bits (Java is 32 bits) which is why the last value entered is parsed incorrectly

If I understand your programs correctly this is not correct.

As the Arduino program is using parseInt() your Java program must be sending data as text so how the Java program stores values is irrelevant.

IMHO the problem is that the maximum value that an Arduino can hold in an int variable is 32767 and you are trying to put 67,890 into it.

...R

Serial.parseInt() returns a long so

  long int data=Serial.parseInt();

will assign the value 67890 to data

horace:
Serial.parseInt() returns a long so

  long int data=Serial.parseInt();

will assign the value 67890 to data

Shouldn’t the code be ?

long data=Serial.parseInt();

And, in any case, your code in Reply #3 uses int so I think my comment remains correct.

Serial.parseInt() returns a long

I never use it so I have just looked up the reference and this is quite correct.

So WTF didn’t they call it Serial.parseLong() [/rant]

…R

Robin2: So WTF didn't they call it Serial.parseLong() [/rant]

...R

I had assumed that parseInt() would attempt to parse a 16bit integer numeric and return a 16bit int hence it was the parsing of "67890" which returned value 2354 - this is not case parseInt() returns a 32bit long 67890 which when assigned to a 16bit int yielded 2354 shows how one should read documentation !