Vous êtes sur la page 1sur 1

‫إﻟﻜﺘﺮوﺑﺮوﺟﻴﻪ‬ ‫ﺗﺤﻤﻴﻞ | ﺗﺴﺠﻴﻞ دﺧﻮل‬ ‫ﻣﻘﺎﻻت‬ ‫ﻣﺸﺎرﻳﻊ‬ ‫دروس‬

Application JAVA NetBeans pour Contrôler un ARDUINO

Création d'une application JAVA NetBeans on utilisons la


 bibliothèque JSSC pour contrôler un ARDUINO via le port
série. 

Figure 1 - Montage

La librairie JSSC (Java-Simple-Serial-Connector)


Pour la communication Série RS-232 nous allons utilisé cette librairie se nommant Java-Simple-Serial-Connector, simple
d'utilisation et légère, possédant exactement ce que nous voulons (envoyer et recevoir) et en plus avec des exemples
facilement compréhensibles. Nous avons fait différents tests avec cette librairie, les résultats étant concluants, notre choix
s'est porté sur celle-ci.
Après divers tests et renseignements, nous avons choisi d'utiliser la librairie jssc, notamment car son utilisation est très simple.
De plus, elle est multi-plateforme et nativement compatible avec Windows 64 bits.

Le programme ARDUINO
. #include <LiquidCrystal.h>
.
. LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
.
. #define led1 10
. #define led2 11
. #define led3 12
.
. #define sensorPin A0
. int sensorValue = 0;
. int outputValue =0;
.
. float led10state = false;
. float led11state = false;
. float led12state = false;
.
. void setup() {
. Serial.begin(115200);
. while (!Serial) {
.
. }
. Serial.println("########################################");
. Serial.println("###### ARDUINO JAVA LEDs CONTROLE ######");
. Serial.println("###### PAR ELECTROPROJETS.MA ###########");
. Serial.println("########################################");
. Serial.println("");
. delay(500);
. Serial.println("###### ARDUINO : OK!");
.
. pinMode(led1, OUTPUT);
. pinMode(led2, OUTPUT);
. pinMode(led3, OUTPUT);
.
. digitalWrite(led1,LOW);
. digitalWrite(led2,LOW);
. digitalWrite(led3,LOW);
.
. delay(500);
. Serial.println("###### LED1, LED2, LED3 : ALL OK!");
.
. lcd.begin(16, 2);
. lcd.print("LED CONTROLE :");
.
. lcd.setCursor(0, 1);
. lcd.print("Pret ...");
.
. delay(500);
. Serial.println("###### LCD 2*16 : OK!");
. Serial.println("");
. }
.
. void loop() {
. sensorValue = analogRead(sensorPin);
. outputValue = map(sensorValue, 0, 1023, 0, 100);
. Serial.print("Valeure : ");
. Serial.print(outputValue);
. Serial.println(" %");
. delay(200);
.
. if (Serial.available()) {
. int strin = Serial.read();
. if(strin == 97) {
. if(!led10state) {
. digitalWrite(led1,HIGH);
. led10state = true;
. lcd.setCursor(0, 1);
. lcd.print(" LED 1 ON ");
. delay(200);
. Serial.println("LED 10 ON");
. }else {
. digitalWrite(led1,LOW);
. led10state = false;
. lcd.setCursor(0, 1);
. lcd.print(" LED 1 OFF");
. delay(200);
. Serial.println("LED 10 OFF");
. }
. }
. if(strin == 98) {
. if(!led11state) {
. digitalWrite(led2,HIGH);
. led11state = true;
. lcd.setCursor(0, 1);
. lcd.print(" LED 2 ON ");
. delay(200);
. Serial.println("LED 11 ON");
. }else {
. digitalWrite(led2,LOW);
. led11state = false;
. lcd.setCursor(0, 1);
. lcd.print(" LED 2 OFF");
. delay(200);
. Serial.println("LED 11 OFF");
. }
. }
. if(strin == 99) {
. if(!led12state) {
. digitalWrite(led3,HIGH);
. led12state = true;
. lcd.setCursor(0, 1);
. lcd.print(" LED 3 ON ");
. delay(200);
. Serial.println("LED 12 ON");
. }else {
. digitalWrite(led3,LOW);
. led12state = false;
. lcd.setCursor(0, 1);
. lcd.print(" LED 3 OFF");
. delay(200);
. Serial.println("LED 12 OFF");
. }
. }
. delay(200);
. }
. }

Le programme JAVA

Figure 2 - ARDUINO JAVA APP

. package arduinointerfaceapp;
.
. import java.io.IOException;
.
. public class ArduinoInterfaceApp {
.
. public static void main(String[] args) throws IOException {
. //
. frmmain window = new frmmain();
. window.setVisible(true);
. }
.
. }

. /*
. * To change this license header, choose License Headers in Project Properties.
. * To change this template file, choose Tools | Templates
. * and open the template in the editor.
. */
. package arduinointerfaceapp;
.
. import java.awt.BorderLayout;
. import java.awt.Image;
. import java.io.IOException;
. import java.util.logging.Level;
. import java.util.logging.Logger;
. import javax.imageio.ImageIO;
. import javax.swing.JFrame;
. import jssc.SerialPort;
. import jssc.SerialPortEvent;
. import jssc.SerialPortEventListener;
. import jssc.SerialPortException;
. import jssc.SerialPortList;
.
. /**
. *
. * @author RABII
. */
. public class frmmain extends javax.swing.JFrame {
.
. /**
. * Creates new form frmmain
. */
. public frmmain() {
. initComponents();
. }
.
. /**
. * This method is called from within the constructor to initialize the form.
. * WARNING: Do NOT modify this code. The content of this method is always
. * regenerated by the Form Editor.
. */
.
. SerialPort ArduinoPort;
.
. @SuppressWarnings("unchecked")
. private void initComponents() {
.
. jPanel1 = new javax.swing.JPanel();
. cmbb_portsliste = new javax.swing.JComboBox<>();
. btn_connect = new javax.swing.JButton();
. btnrefrech = new javax.swing.JButton();
. jSeparator1 = new javax.swing.JSeparator();
. jScrollPane1 = new javax.swing.JScrollPane();
. arearx = new javax.swing.JTextArea();
. jSeparator2 = new javax.swing.JSeparator();
. prgsvaleure = new javax.swing.JProgressBar();
. jPanel2 = new javax.swing.JPanel();
. cmdled12 = new javax.swing.JButton();
. cmdled11 = new javax.swing.JButton();
. cmdled10 = new javax.swing.JButton();
. jLabel1 = new javax.swing.JLabel();
. jLabel2 = new javax.swing.JLabel();
. jSeparator3 = new javax.swing.JSeparator();
. txtsend = new javax.swing.JTextField();
. btnsend = new javax.swing.JButton();
. jLabel3 = new javax.swing.JLabel();
. jLabel4 = new javax.swing.JLabel();
. lblvaleur = new javax.swing.JLabel();
. }
.
. private void formWindowOpened(java.awt.event.WindowEvent evt) {
. // TODO add your handling code here:
. this.setLocationRelativeTo(null);
. this.setTitle("Interface Arduino - Electroprojets.ma");
. this.setLayout(new BorderLayout());
. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
.
. try {
. Image iconimage = ImageIO.read(getClass().getResource("icon.png"));
. this.setIconImage(iconimage);
. } catch (IOException ex) {
. Logger.getLogger(frmmain.class.getName()).log(Level.SEVERE, null, ex);
. }
.
. cmbb_portsliste.removeAllItems();
. String[] serialPortNames = SerialPortList.getPortNames();
. for (String serialPortName : serialPortNames) {
. this.cmbb_portsliste.addItem(serialPortName);
. }
.
. cmdled10.setEnabled(false);
. cmdled11.setEnabled(false);
. cmdled12.setEnabled(false);
. }
.
. private void cmbb_portslisteActionPerformed(java.awt.event.ActionEvent evt) {
. // TODO add your handling code here:
. }
.
. private void btnrefrechActionPerformed(java.awt.event.ActionEvent evt) {
. // TODO add your handling code here:
. if (btn_connect.getText().equals("Connecter")) {
. cmbb_portsliste.removeAllItems();
. String[] serialPortNames1 = SerialPortList.getPortNames();
. for (String serialPortName : serialPortNames1) {
. cmbb_portsliste.addItem(serialPortName);
. }
. }
. }
.
. private void btn_connectActionPerformed(java.awt.event.ActionEvent evt) {
.
. if (btn_connect.getText().equals("Connecter")) {
. String selectedPort = (String) cmbb_portsliste.getSelectedItem();
. ArduinoPort = new SerialPort(selectedPort);
. try {
. ArduinoPort.openPort();
. ArduinoPort.setParams(SerialPort.BAUDRATE_115200,
. SerialPort.DATABITS_8,
. SerialPort.STOPBITS_1,
. SerialPort.PARITY_NONE,
. false,
. true);
. ArduinoPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
.
. ArduinoPort.addEventListener(new PortReader(), SerialPort.MASK_RXCHAR);
.
. btnrefrech.setEnabled(false);
. cmbb_portsliste.setEnabled(false);
.
. cmdled10.setEnabled(true);
. cmdled11.setEnabled(true);
. cmdled12.setEnabled(true);
.
. btn_connect.setText("Deconnecter");
. }
. catch (SerialPortException ex) {
. System.out.println("There are an error on writing string to port т: " + ex);
. }
. }else {
. try {
. ArduinoPort.removeEventListener();
.
. if(ArduinoPort.isOpened()){
. ArduinoPort.closePort();
. }
. btnrefrech.setEnabled(true);
. cmbb_portsliste.setEnabled(true);
.
. cmdled10.setEnabled(false);
. cmdled11.setEnabled(false);
. cmdled12.setEnabled(false);
.
. btn_connect.setText("Connecter");
. } catch (SerialPortException ex) {
. Logger.getLogger(frmmain.class.getName()).log(Level.SEVERE, null, ex);
. }
. }
. }
.
. private void cmdled10ActionPerformed(java.awt.event.ActionEvent evt) {
. try {
. ArduinoPort.writeBytes("a".getBytes());
. } catch (SerialPortException ex) {
. System.out.println("There are an error on writing string to port т: " + ex);
. }
. }
.
. private void cmdled11ActionPerformed(java.awt.event.ActionEvent evt) {
. try {
. ArduinoPort.writeBytes("b".getBytes());
. } catch (SerialPortException ex) {
. System.out.println("There are an error on writing string to port т: " + ex);
. }
. }
.
. private void cmdled12ActionPerformed(java.awt.event.ActionEvent evt) {
. try {
. ArduinoPort.writeBytes("c".getBytes());
. } catch (SerialPortException ex) {
. System.out.println("There are an error on writing string to port т: " + ex);
. }
. }
.
. private void txtsendActionPerformed(java.awt.event.ActionEvent evt) {
. // TODO add your handling code here:
. }
.
. private void btnsendActionPerformed(java.awt.event.ActionEvent evt) {
. // TODO add your handling code here:
. String strsend = txtsend.getText();
. try {
. ArduinoPort.writeBytes((strsend + "\r\n").getBytes());
. } catch (SerialPortException ex) {
. System.out.println("There are an error on writing string to port т: " + ex);
. }
. }
.
. /**
. * @param args the command line arguments
. */
. public static void main(String args[]) {
. /* Set the Nimbus look and feel */
. /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
. */
. try {
. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
. if ("Nimbus".equals(info.getName())) {
. javax.swing.UIManager.setLookAndFeel(info.getClassName());
. break;
. }
. }
. } catch (ClassNotFoundException ex) {
. java.util.logging.Logger.getLogger(frmmain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
. } catch (InstantiationException ex) {
. java.util.logging.Logger.getLogger(frmmain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
. } catch (IllegalAccessException ex) {
. java.util.logging.Logger.getLogger(frmmain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
. java.util.logging.Logger.getLogger(frmmain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
. }
.
. /* Create and display the form */
. java.awt.EventQueue.invokeLater(() -> {
. new frmmain().setVisible(true);
. });
. }
.
. public class PortReader implements SerialPortEventListener {
.
. @Override
. public void serialEvent(SerialPortEvent event) {
. if(event.isRXCHAR() AND event.getEventValue() > 0) {
. try {
. String receivedData = ArduinoPort.readString(event.getEventValue());
. String[] splited = receivedData.split(" ");
.
. if("Valeure".equals(splited[0])) {
. lblvaleur.setText(splited[2] + " %");
. int nbrvaleur = Integer.parseInt(splited[2].trim());
. prgsvaleure.setValue(nbrvaleur);
. }
.
. if("LED 10 ON\r\n".equals(receivedData)) {
. cmdled10.setText("LED 1 OFF");
. }
. if("LED 10 OFF\r\n".equals(receivedData)) {
. cmdled10.setText("LED 1 ON");
. }
. if("LED 11 ON\r\n".equals(receivedData)) {
. cmdled11.setText("LED 2 OFF");
. }
. if("LED 11 OFF\r\n".equals(receivedData)) {
. cmdled11.setText("LED 2 ON");
. }
. if("LED 12 ON\r\n".equals(receivedData)) {
. cmdled12.setText("LED 3 OFF");
. }
. if("LED 12 OFF\r\n".equals(receivedData)) {
. cmdled12.setText("LED 3 ON");
. }
. String text = arearx.getText();
. arearx.setText(text + receivedData);
. arearx.setCaretPosition(arearx.getDocument().getLength());
. }
. catch (SerialPortException ex) {
. System.out.println("Error in receiving string from COM-port: " + ex);
. }
. }
. }
.
. }}

Voire la Vidéo

Sources
Recherches sur Internet
Essais personnels
Téléchargement
Télécharger tous les articles

Electroprojets.ma le 07 décembre 2017


Publicitée

‫ﺗﻌﻠﻴﻘﺎت‬

.‫ ﻳﺠﺐ ﻋﻠﻴﻚ ﺗﺴﺠﻴﻞ اﻟﺪﺧﻮل أو ﺗﺴﺠﻴﻞ إﻧﺨﺮاط ﻟﻮ ﻟﻢ ﺗﻜﻦ ﻋﻀﻮ ﺑﻌﺪ‬,‫ﻟﻜﻲ ﺗﺴﺘﻄﻴﻊ ﺗﺮك ﺗﻌﻠﻴﻖ‬

.‫ﻻ ﺗﻮﺟﺪ ﺗﻌﻠﻴﻘﺎت ﻟﻬﺬا اﻟﻤﻠﻒ‬

   
Copyright © 2017 Electroprojets.ma

Vous aimerez peut-être aussi