【问题标题】:Arduino - Processing bidirectional communication can't receive data via serialArduino - 处理双向通信无法通过串行接收数据
【发布时间】:2020-04-22 00:33:57
【问题描述】:

我正在尝试使用 jssc 将 arduino 与 java 进行通信。只是为了测试,我试图在 Arduino 上接收数据并将其发送回 PC,非常简单。问题是,董事会似乎没有收到它。当 arduino 的代码仅发送时,工作正常,但它无法接收任何数据。我首先认为问题是 jssc,所以我将我的应用程序更改为处理,同样的问题。我什至尝试更换板子,没有区别,板子很好,它可以与 Arduino 串行监视器正常通信。所以我的问题一定是Java和处理代码。我知道这是一个非常简单的问题,我为发布这个感到羞耻,一定是一个非常简单的问题,我已经过去了。

还有一点,没有显示任何错误

Java 代码:

public class ArduinoTest{
    public static void main(String[] args) throws SerialPortException, InterruptedException {
        SerialPort serialPort = new SerialPort("COM6");
        serialPort.openPort();
        serialPort.setParams(SerialPort.BAUDRATE_9600,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        int i;
        serialPort.writeBytes("a".getBytes());
        while(true){
            a=serialPort.readBytes(1);
            i = a[0] & 0xff;
            System.out.println(i);
        }
    }
}

处理代码:

import processing.serial.*;

Serial port;
int r;
void setup(){
  port = new Serial(this, "COM6", 9600);
  port.write(1);
}
void draw(){
  if(port.available()>0){
    r = port.read();
    println(r);
  }
}

Arduino 代码:

int r1=0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  if(Serial.available() > 0){
    r1 = Serial.read();
    Serial.write(r1);
  }
}

【问题讨论】:

    标签: java c++ arduino processing


    【解决方案1】:

    这是 Arduino 的工作通信代码:

    const int led_pin = 13;    // Initializing the LED pin internal led
    String char_output = "";   // Declaring a variable for output
    
    
    void setup ( ) {
      pinMode(led_pin, OUTPUT); // Declaring the LED pin as output pin
      Serial.begin(9600);       // Starting the serial communication at 9600 baud rate
    
    }
    
    void loop ( ) {
    
    
      if (Serial.available ( ) > 0) {   // Checking if the Processing IDE has send a value or not
    
        char state = Serial.read ( );    // Reading the data received and saving in the state variable
    
        if (state == '1')  {          // If received data is '1', then turn on LED
          digitalWrite (led_pin, HIGH);
          char_output = "Led on";
        }
    
        if (state == '0') {     // If received data is '0', then turn off led
          digitalWrite (led_pin, LOW);
          char_output = "Led off";
        }
      }
    
      delay(50);
      Serial.println (char_output);     // Sending the output to Processing IDE
    } 
    

    这里是处理部分:

    import processing.serial.*;    // Importing the serial library to communicate with the Arduino
    
    Serial myPort;      // Initializing a vairable named 'myPort' for serial communication
    float background_color ;   // Variable for changing the background color
    
    void setup ( ) {
      size (500,  500);     // Size of the serial window, you can increase or decrease as you want
      myPort  =  new Serial (this, "COM3",  9600); // Set the com port and the baud rate according to the Arduino IDE
      myPort.bufferUntil ( '\n' );   // Receiving the data from the Arduino IDE
    
    }
    void serialEvent  (Serial myPort) {
      background_color  =  float (myPort.readStringUntil ( '\n' ) ) ;  // Changing the background color according to received data
    }
    
    void draw ( ) {
      background ( 150, 50, background_color );   // Initial background color, when we will open the serial window
      if ( mousePressed  &&  ( mouseButton  ==  LEFT ) ) { // if the left mouse button is pressed
        myPort.write ( '1' ) ;       // send a '1' to the Arduino IDE
      }
      if  ( mousePressed  &&  ( mouseButton == RIGHT ) ) {  // if the right mouse button is pressed
        myPort.write ( '0' ) ;     // Send a '0' to the Arduino IDE
      }
    }
    

    读取 cmets 会发现从串行读取和打印到串行是分开的

    【讨论】:

    • 谢谢,使用此代码我能够编辑并制作我的作品,但我仍然不知道我的问题是什么
    • 问题是我试图在端口完成打开之前对其进行写入。您的代码之所以有效,是因为它需要用户响应才能写入,这样程序就有时间在写入之前打开端口。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多