风筝
发表于: 2020-2-4 12:27:53 | 显示全部楼层

在本篇文章中,我们将主要演示如何在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文件查看连接的详细信息:

fritzing_sketch.png

将LCM1602 IIC V1模块连接到Arduino Uno。


Arduino源代码

接下来,我们必须为Arduino Uno写一些代码。代码等待准备好串口,等待被读取的字节。如果读取到了一个字节,则将其打印到LCM1602 IIC V1模块。

  1. #include <Wire.h>
  2. #include <LiquidCrystal_I2C.h> // LiquidCrystal_I2C library

  3. 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)
  4. void setup() {
  5.   lcd.begin(16, 2); // begins connection to the LCD module
  6.   lcd.backlight(); // turns on the backlight
  7.   lcd.clear();
  8.   Serial.begin(9600);
  9.   while (!Serial) {
  10.     ; // wait for serial port to connect.
  11.   }
  12.   lcd.setCursor(0, 0); // set cursor to first row
  13.   lcd.print("Init done"); // print to lcd

  14. }
  15. void loop() {
  16.   if (Serial.available() > 0) {   
  17.     byte incomingByte = 0;
  18.     incomingByte = Serial.read(); // read the incoming byte:
  19.     if (incomingByte != -1) { // -1 means no data is available
  20.       lcd.setCursor(0, 0); // set cursor to first row
  21.       lcd.print("I received: "); // print out to LCD
  22.       lcd.setCursor(0, 1); // set cursor to secon row
  23.       lcd.print(incomingByte); // print out the retrieved value to the second row
  24.     }
  25.   }
  26. }
复制代码

Java源代码

Java应用程序使用jSerialComm库通过标准USB连接将文本发送到Arduino Uno。 我利用Maven设置了Java项目和jSerialComm库之间的依赖关系。 如果您也是将Maven用于您的项目,那么我的POM文件可能对您有用:

  1. <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">
  2.   <modelVersion>4.0.0</modelVersion>
  3.   <groupId>de.mschoeffler.arduino.serialcomm</groupId>
  4.   <artifactId>de.mschoeffler.arduino.serialcomm.example01</artifactId>
  5.   <version>0.0.1-SNAPSHOT</version>
  6.   <name>ArduinoBasicExample</name>
  7.   <build>
  8.     <sourceDirectory>src</sourceDirectory>
  9.     <plugins>
  10.       <plugin>
  11.         <artifactId>maven-compiler-plugin</artifactId>
  12.         <version>3.5.1</version>
  13.         <configuration>
  14.           <source>1.8</source>
  15.           <target>1.8</target>
  16.         </configuration>
  17.       </plugin>
  18.     </plugins>
  19.   </build>
  20.   <dependencies>
  21.     <dependency>
  22.      <groupId>com.fazecast</groupId>
  23.      <artifactId>jSerialComm</artifactId>
  24.      <version>1.3.11</version>
  25.   </dependency>
  26.   </dependencies>
  27. </project>
复制代码

实际的Java源代码只是带有main方法的单个类。 建立串口连接。 然后,五位数字(0-4)被写入串行端口。 最后,关闭串口连接:

  1. package de.mschoeffler.arduino.serialcomm.example01;
  2. import java.io.IOException;
  3. import com.fazecast.jSerialComm.SerialPort;
  4. /**
  5. * Simple application that is part of an tutorial.
  6. * The tutorial shows how to establish a serial connection between a Java and Arduino program.
  7. * @author Michael Schoeffler (www.mschoeffler.de)
  8. *
  9. */
  10. public class Startup {
  11.   public static void main(String[] args) throws IOException, InterruptedException {
  12.     SerialPort sp = SerialPort.getCommPort("/dev/ttyACM1"); // device name TODO: must be changed
  13.     sp.setComPortParameters(9600, 8, 1, 0); // default connection settings for Arduino
  14.     sp.setComPortTimeouts(SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0); // block until bytes can be written
  15.    
  16.     if (sp.openPort()) {
  17.       System.out.println("Port is open :)");
  18.     } else {
  19.       System.out.println("Failed to open port :(");
  20.       return;
  21.     }   
  22.    
  23.     for (Integer i = 0; i < 5; ++i) {      
  24.       sp.getOutputStream().write(i.byteValue());
  25.       sp.getOutputStream().flush();
  26.       System.out.println("Sent number: " + i);
  27.       Thread.sleep(1000);
  28.     }   
  29.    
  30.     if (sp.closePort()) {
  31.       System.out.println("Port is closed :)");
  32.     } else {
  33.       System.out.println("Failed to close port :(");
  34.       return;
  35.     }
  36.    
  37.   }
  38. }
复制代码

主要的重要方法调用是SerialPort.getCommPort(...)。该函数只有一个参数,必须是Arduino Uno的设备名称。因此,很有可能需要更改此参数值。您可以通过查看Arduino IDE找出Arduino Uno的设备名称。在工具->端口中,找到所有已连接的设备。为了将代码上传到Arduino,您必须选择相应Arduino的正确设备名称。幸运的是,jSerialComm库需要相同的设备名称。因此,只需将设备名称从Arduino IDE复制到Java源代码即可。


执行

将源代码上传到Arduino并启动Java应用程序,您可以看到LCM1602 IIC V1模块上出现数字0-4。

arduino_example1.jpg


示例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(也可以使用其他数字引脚)。

fritzing_sketch_bb.png

将USB转TTL串行适配器连接到Arduino Uno。


以下是串口适配器的实物连接:

arduino_example2.jpg

USB-to-TTL串口适配器连接到Arduino Uno。


Arduino源代码:

Arduino程序利用了所谓的软件串口。初始化软件串口对象时,它需要接收和发送引脚的引脚号。由于我们不打算从Arduino Uno传输文本,因此可以将传输引脚设置为任何数字。在这里,我只是将引脚设置为6。

  1. #include <SoftwareSerial.h>

  2. SoftwareSerial sserial(5,6); // receive pin (used), transmit pin (unused)
  3. void setup() {
  4.   Serial.begin(9600); // used for printing to serial monitor of the Arduino IDE
  5.   sserial.begin(9600); // used to receive digits from the Java application
  6.   while (!Serial) {
  7.     ; // wait for serial port to connect.
  8.   }
  9. }
  10. void loop() {
  11.   if (sserial.available() > 0) {
  12.     byte incomingByte = 0;
  13.     incomingByte = sserial.read();
  14.     if (incomingByte != -1) {
  15.       Serial.print("I received: "); // print out to serial monitor
  16.       Serial.println(incomingByte); // print out to serial monitor
  17.     }
  18.   }
  19. }
复制代码

Java源代码:

Java应用程序与本教程第一部分中使用的应用程序基本相同。 唯一的例外是USB转TTL设备的设备名称。 您可以再次使用Arduino IDE,因为它也会显示串口适配器的名称。


执行:

如果一切都成功执行,则Arduino IDE的串口监视器应显示五个接收到的数字。

serial_monitor_eclipse.png

显示Java应用程序(Eclipse IDE)和Arduino Uno之间的串口连接结果。

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-24 15:41 , Processed in 0.035773 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表