爪哇 - 如何通过通过串行端口多个参数的Arduino万 [英] java - how to pass multiple parameters over serial port to Arduino mega

查看:411
本文介绍了爪哇 - 如何通过通过串行端口多个参数的Arduino万的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过我的Java程序。我想一个字节的值传递到Arduino巨型点亮LED
而且在同一时间,我想为一个字符串值传递到Arduino要显示在LCD上。

我怎样才能获得单独从以上的java程序2输入Arduino的,在不同的过程中使用它们的Arduino里面..

下面是Arduino的code

  LiquidCrystal液晶(12,11,10,9,8,7);
INT操作;无效设置(){lcd.begin(16,2);
Serial.begin(9600);
Serial1.begin(9600);
Serial2.begin(9600);pinMode(3,输出);
pinMode(2,输出);}诠释计数= 0;
空隙环(){// LCD启动
如果(Serial.available()){
//稍等一下,整个消息的到达
延迟(50);
//清屏
lcd.clear();
延迟(10);
//读取所有可用字符
而(Serial.available()大于0){
  //显示每个字符液晶
  lcd.write(Serial.read());
 }
 }
 // LCD结束 // LED指示灯闪烁启动
 如果(Serial.available()大于0)
 延迟(10);
 {
 操作= Serial.read();
  } 如果(操作=='2')
 {  digitalWrite(2,低);
  digitalWrite(3,高);
  延迟(50);
  digitalWrite(3,低);
  延迟(50);
  }  如果(操作=='1')
  {   digitalWrite(3,低);
   digitalWrite(2,高);
   延迟(50);
   digitalWrite(2,低);
   延迟(50);
 }
  // LED指示灯闪烁结束  //收到RFID标签号
如果(Serial1.available()){
   INT X = Serial1.read();
   Serial1.print(X);
  }如果(Serial2.available()){
   INT X = Serial2.read();
   Serial1.print(X);
  }}

下面是Java code发送数据

code发送数字1到Arduino的

 字符串BUF =1;
CHAR BUF2 [] = buf.toCharArray();
output.write((字节)BUF2 [0]);

code发送字符串液晶显示

  output.write(Hellow世界,这是从Java字符串.getBytes());

当我运行这些codeS单独运作良好,不受任何干扰。但是当我做他们两个在一起......有时值1或2显示在液晶..和LED闪烁不列入正常。如何从Java获得两个输入的Arduino并分别处理它们Arduino的?

里面
解决方案

您可以连接的数据。
所以,如果你是路过Hello和200为2项,然后在发送前将它们合并到Arduino的和发送你好200%,并在Arduino的内部分裂%

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?

解决方案

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.

这篇关于爪哇 - 如何通过通过串行端口多个参数的Arduino万的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆