java - how to pass multiple parameters over serial port to Arduino mega

别等时光非礼了梦想. 提交于 2019-12-13 08:26:32

问题


Through my java program. i want to pass a byte value to the Arduino mega to blink an Led and also at the same time i want to pass a string value to the Arduino to be displayed in the lcd.

How can I separately get above 2 inputs from the java program to Arduino and use them in different processes inside Arduino..

Below is the arduino code

LiquidCrystal lcd (12, 11, 10, 9, 8, 7);
int operation;

void setup() {

lcd.begin(16, 2);


Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);

pinMode(3, OUTPUT);
pinMode(2, OUTPUT);

}

int count = 0; 
void loop() {

//LCD start
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(50);
// clear the screen
lcd.clear();
delay(10);
// read all the available characters
while (Serial.available() > 0) {
  // display each character to the LCD
  lcd.write(Serial.read());
 }
 } 
 //LCD end

 //LED Blink start
 if (Serial.available() > 0)
 delay(10);
 {
 operation = Serial.read();
  }

 if(operation == '2')
 {

  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  delay(50);
  digitalWrite(3, LOW);
  delay(50);
  }

  if(operation == '1')
  {

   digitalWrite(3, LOW);
   digitalWrite(2, HIGH);
   delay(50);
   digitalWrite(2, LOW);
   delay(50);
 }
  //LED Blink end

  // Recieve rfid tag numbers 
if(Serial1.available()) { 
   int x = Serial1.read();     
   Serial1.print(x);
  }

if(Serial2.available()) { 
   int x = Serial2.read();     
   Serial1.print(x);
  }

}

Below is the Java code to send data

Code to send number 1 to Arduino

String buf = "1";
char buf2[] = buf.toCharArray();
output.write((byte)buf2[0]);

Code to send string to display in lcd

output.write("Hellow world. this is a String from java".getBytes());

When I run these codes separately it works well without any interference.. but when I do them both together... sometimes value 1 or 2 is displayed in the lcd.. and led doesnot blink properly . how to get two inputs from java to arduino and process them separately inside the Arduino?


回答1:


I think there might be several issues working in concert.

First of all, Strings are represented internally in Java as UTF-16 encoded characters. I don't remember what Arduino "C" operates on per default, but I am pretty sure it is not UTF-16.

From the JavaDoc

"A String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character class for more information). Index values refer to char code units, so a supplementary character uses two positions in a String."

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

Second, the method getBytes() return a platform specific encoded array, so depending on what platform you run the program on, the returned bytes can vary.

Try looking into using public byte[] getBytes(String charsetName), which will give you predictable values back.

Try something like bytes[] asciiBytes = new String("Hello World").getBytes("US-ASCII");.

See http://docs.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html for more info on Character sets.




回答2:


You could concatenate the data. So, if you are passing "Hello" and "200" as 2 items then combine them before the send to Arduino and send "Hello%200" and split on the % inside the Arduino.



来源:https://stackoverflow.com/questions/22644644/java-how-to-pass-multiple-parameters-over-serial-port-to-arduino-mega

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!