| 在本篇文章中,我们将主要演示如何在Java应用程序和Arduino Uno之间建立串口连接。本文分为两个部分:在第一部分中,说明了如何从Java应用程序向Arduino发送文本(数字)。此外,Arduino将数字输出到LCD模块(LCM1602 IIC V1)。在第二部分中,使用基本上相同的Java应用程序将数字发送到Arduino,但这一次使用的是在USB-to-TTL模块。这用,Arduino IDE可以使用Arduino的标准串行端口将接收到的数字打印到串口监视器上。 
 所需的材料清单: –  Arduino Uno开发板 –  LCM1602 IIC V1 / LCD模块 –  USB转TTL串行适配器 
 示例第1部分:连接设置 在这一部分中,我们将LCM1602 IIC V1连接到Arduino。 LCM1602有四个引脚:VCC、GND、SDA和SCL。接线很简单:VCC接至Arduino的5V。其他三个引脚在Arduino上具有完全相同的名称:GND接到GND,SDA接到SDA,SCL接到SCL。看一下fritzing文件查看连接的详细信息: 
  将LCM1602 IIC V1模块连接到Arduino Uno。
 
 Arduino源代码 接下来,我们必须为Arduino Uno写一些代码。代码等待准备好串口,等待被读取的字节。如果读取到了一个字节,则将其打印到LCM1602 IIC V1模块。 复制代码#include <Wire.h>
#include <LiquidCrystal_I2C.h> // LiquidCrystal_I2C library
 
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // 0x27 is the i2c address of the LCM1602 IIC v1 module (might differ)
void setup() {
  lcd.begin(16, 2); // begins connection to the LCD module
  lcd.backlight(); // turns on the backlight
  lcd.clear(); 
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect.
  }
  lcd.setCursor(0, 0); // set cursor to first row
  lcd.print("Init done"); // print to lcd
 
}
void loop() {
  if (Serial.available() > 0) {    
    byte incomingByte = 0;
    incomingByte = Serial.read(); // read the incoming byte:
    if (incomingByte != -1) { // -1 means no data is available
      lcd.setCursor(0, 0); // set cursor to first row
      lcd.print("I received: "); // print out to LCD
      lcd.setCursor(0, 1); // set cursor to secon row
      lcd.print(incomingByte); // print out the retrieved value to the second row
    }
  }
}
 Java源代码 Java应用程序使用jSerialComm库通过标准USB连接将文本发送到Arduino Uno。 我利用Maven设置了Java项目和jSerialComm库之间的依赖关系。 如果您也是将Maven用于您的项目,那么我的POM文件可能对您有用: 复制代码<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>de.mschoeffler.arduino.serialcomm</groupId>
  <artifactId>de.mschoeffler.arduino.serialcomm.example01</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>ArduinoBasicExample</name>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.5.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
     <groupId>com.fazecast</groupId>
     <artifactId>jSerialComm</artifactId>
     <version>1.3.11</version>
  </dependency>
  </dependencies>
</project>
 实际的Java源代码只是带有main方法的单个类。 建立串口连接。 然后,五位数字(0-4)被写入串行端口。 最后,关闭串口连接: 复制代码package de.mschoeffler.arduino.serialcomm.example01;
import java.io.IOException;
import com.fazecast.jSerialComm.SerialPort;
/**
 * Simple application that is part of an tutorial. 
 * The tutorial shows how to establish a serial connection between a Java and Arduino program.
 * @author Michael Schoeffler (www.mschoeffler.de)
 *
 */
public class Startup {
  public static void main(String[] args) throws IOException, InterruptedException {
    SerialPort sp = SerialPort.getCommPort("/dev/ttyACM1"); // device name TODO: must be changed
    sp.setComPortParameters(9600, 8, 1, 0); // default connection settings for Arduino
    sp.setComPortTimeouts(SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0); // block until bytes can be written
    
    if (sp.openPort()) {
      System.out.println("Port is open :)");
    } else {
      System.out.println("Failed to open port :(");
      return;
    }    
    
    for (Integer i = 0; i < 5; ++i) {      
      sp.getOutputStream().write(i.byteValue());
      sp.getOutputStream().flush();
      System.out.println("Sent number: " + i);
      Thread.sleep(1000);
    }    
    
    if (sp.closePort()) {
      System.out.println("Port is closed :)");
    } else {
      System.out.println("Failed to close port :(");
      return;
    }
    
  }
}
 主要的重要方法调用是SerialPort.getCommPort(...)。该函数只有一个参数,必须是Arduino Uno的设备名称。因此,很有可能需要更改此参数值。您可以通过查看Arduino IDE找出Arduino Uno的设备名称。在工具->端口中,找到所有已连接的设备。为了将代码上传到Arduino,您必须选择相应Arduino的正确设备名称。幸运的是,jSerialComm库需要相同的设备名称。因此,只需将设备名称从Arduino IDE复制到Java源代码即可。 
 执行 将源代码上传到Arduino并启动Java应用程序,您可以看到LCM1602 IIC V1模块上出现数字0-4。 
  
 示例2 在本文的第二部分中,数字(来自Java应用程序)被打印输出到Arduino的默认串口连接。这样,可以从Arduino IDE的串口监视器(工具->串口监视器)查看接收到的数字。Java应用程序无法使用这个标准的USB连接,因为一旦打开串口监视器,串口监视器便已经捕获了该串口。因此,我们使用了USB转TTL串口适配器。 
 连接设置 通常,USB转TTL适配器至少具有四个引脚:VCC、GND、RX(接收数据)和TX(发送数据)。由于我们将仅使用此适配器从Java应用程序发送数据,因此我们可以忽略RX引脚。 VCC连接到Arduino的5V引脚,GND连接到Arduino的GND引脚,TX连接到Arduino数字引脚#5(也可以使用其他数字引脚)。 
  将USB转TTL串行适配器连接到Arduino Uno。 
 以下是串口适配器的实物连接: 
  USB-to-TTL串口适配器连接到Arduino Uno。 
 Arduino源代码: Arduino程序利用了所谓的软件串口。初始化软件串口对象时,它需要接收和发送引脚的引脚号。由于我们不打算从Arduino Uno传输文本,因此可以将传输引脚设置为任何数字。在这里,我只是将引脚设置为6。 复制代码#include <SoftwareSerial.h>
 
SoftwareSerial sserial(5,6); // receive pin (used), transmit pin (unused)
void setup() {
  Serial.begin(9600); // used for printing to serial monitor of the Arduino IDE
  sserial.begin(9600); // used to receive digits from the Java application
  while (!Serial) {
    ; // wait for serial port to connect. 
  }
}
void loop() {
  if (sserial.available() > 0) {
    byte incomingByte = 0;
    incomingByte = sserial.read();
    if (incomingByte != -1) {
      Serial.print("I received: "); // print out to serial monitor
      Serial.println(incomingByte); // print out to serial monitor
    }
  }
}
 Java源代码: Java应用程序与本教程第一部分中使用的应用程序基本相同。 唯一的例外是USB转TTL设备的设备名称。 您可以再次使用Arduino IDE,因为它也会显示串口适配器的名称。 
 执行: 如果一切都成功执行,则Arduino IDE的串口监视器应显示五个接收到的数字。 
  显示Java应用程序(Eclipse IDE)和Arduino Uno之间的串口连接结果。 |