Auf Thema antworten

[SPOILER="KOMPLETTER CODE:"]

[code=Java]package com.daniel;


import com.fazecast.jSerialComm.SerialPort;


import javax.swing.*;

import java.awt.*;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.PrintWriter;

import java.util.Scanner;


public class Main{

    static SerialPort chosenPort;

    static JTextArea txtarea;

    static JFrame j;

    public static void main(String[] args) {


            j = new JFrame(":D - By: DanielsLPecke");

        j.setSize(700,500);

        j.setLayout(new BorderLayout());

        j.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

      

        JComboBox<String> portList = new JComboBox<String>();

        JButton sendB = new JButton("COM-Port Setzen");

        JButton getB = new JButton("GetFromArduino");

        JButton disconect = new JButton("Disconect");

        disconect.setEnabled(false);

        JPanel panel = new JPanel();

        JTextField txt = new JTextField();

        txt.setSize(100, 25);

        txt.setText("0");

        JLabel lbl = new JLabel("PortNumber:");

        JCheckBox b = new JCheckBox();

        b.setSelected(true);

        b.setText("Enabled?");

        JPanel stts = new JPanel();

        JTextField pw = new JTextField("maunzi");

        JLabel txtlbl = new JLabel("Password:");

        txtarea = new JTextArea();

        JScrollPane jsp = new JScrollPane();

        jsp.setViewportView(txtarea);

        txtarea.setFont(new Font("Comic sans ms",15, 15));

        Log("Logging","Starting the Programm");

        txtarea.setEditable(false);


        //stts.add(getB);

        //stts.add(jsp);

        panel.add(disconect);

        panel.add(txtlbl);

        panel.add(pw);

        panel.add(b);

        panel.add(lbl);

        panel.add(txt);

        panel.add(portList);

        panel.add(sendB);

        j.add(jsp,BorderLayout.CENTER);

        //    j.add(getB,BorderLayout.NORTH);

        j.add(panel,BorderLayout.SOUTH);

        SerialPort[] portNames = SerialPort.getCommPorts();


        for(int i =0; i<portNames.length;i++){

            portList.addItem(portNames[i].getSystemPortName());

        }


      //  chosenPort = SerialPort.getCommPort(portList.getSelectedItem().toString());

    disconect.addActionListener(e->{

        chosenPort.closePort();

        disconect.setEnabled(false);

        sendB.setText("COM-Port Setzen");

    });


    getB.addActionListener(e->{

                Thread t = new Thread(){

                    private Scanner s;


                    @Override

                    public void run(){

                        for(int i = 0; i<10;i++){

                            sendToArduino("stts"+i+"pw", chosenPort);

                            System.out.println("Okay i: "+i);

                            InputStream iS = chosenPort.getInputStream();

                            s = new Scanner(iS);

                          

                          

                            txtarea.setText(txtarea.getText()+"\nArduino: "+s.nextLine());

                          

                        }

                    }


                  

                };

                t.start();


        });



        sendB.addActionListener(e->{

            if(sendB.getText().equalsIgnoreCase("com-port setzen")){

                chosenPort = SerialPort.getCommPort(portList.getSelectedItem().toString());

                chosenPort.setBaudRate(9600);

                chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0,0);

                if(chosenPort.openPort()){}else{

                    int dialogButton = JOptionPane.OK_OPTION;

                    JOptionPane.showConfirmDialog(null, "Der "+chosenPort.getSystemPortName()+" Port ist leider belegt.","Error.", dialogButton);

                    Log("Logging"," Port used.");

                }

                portList.setEnabled(false);

                disconect.setEnabled(true);

                sendB.setText("Senden");

            }

            if(sendB.getText().equalsIgnoreCase("Senden")){

                    Log("Logging","Try to Send");

                    sendB.setText("Senden");

                    sendB.setEnabled(false);

                    txt.setEditable(false);

                    portList.setEnabled(false);

                    pw.setEditable(false);

                    b.setEnabled(false);


                    if(b.isSelected())sendToArduino("sets"+txt.getText()+"1"+pw.getText(),chosenPort);

                    if(!b.isSelected())sendToArduino("sets"+txt.getText()+"0"+pw.getText(),chosenPort);

                  

                    try{Thread.sleep(50);}catch(Exception e7){}

                    sendB.setText("Senden");

                    sendB.setEnabled(true);

                    txt.setEditable(true);

                    portList.setEnabled(true);

                    pw.setEditable(true);

                    b.setEnabled(true);

                }


            //}

        });

        j.setVisible(true);

        Log("Logging","Finished.");

    }


    public static void Log(String title,String text){

        txtarea.setText(txtarea.getText()+" "+title+": "+text+" \n");

        j.repaint();

    }

    private static void sendToArduino(String string,SerialPort s) {

        Log("Sending",string);

        OutputStream os = s.getOutputStream();

        PrintWriter p = new PrintWriter(os);

      

        p.print(string);

        p.flush();

      

        p.close();

        try {

            os.close();

        } catch (IOException e) {System.out.println("ERROR!");}

        Log("Loggin","Successfully send!");

    }

}[/code]

[/SPOILER]

[SPOILER="ARDUINO-CODE:"]

[code=C]int p0 = 13,p1 = 12,p2 = 11 ,p3 = 10,p4 = 9,p5 = 8,p6 = 7,p7 = 6,p8 = 5,p9 = 4;

void setup() {

Serial.begin(9600);

pinMode(p0,OUTPUT);

pinMode(p1,OUTPUT);

pinMode(p2,OUTPUT);

pinMode(p3,OUTPUT);

pinMode(p4,OUTPUT);

pinMode(p5,OUTPUT);

pinMode(p6,OUTPUT);

pinMode(p7,OUTPUT);

pinMode(p8,OUTPUT);

pinMode(p9,OUTPUT);

}


void loop() {

  String text;

  if(Serial.available()>0)

  text = Serial.readStringUntil('\n');

  else text = "";

 

  if(text == "sets01maunzi"){

    digitalWrite(p0,HIGH);

  }

  if(text == "sets00maunzi"){

    digitalWrite(p0,LOW);

  }

 

 

  if(text == "sets11maunzi"){

    digitalWrite(p1,HIGH);

  }

  if(text == "sets10maunzi"){

    digitalWrite(p1,LOW);

  }

 

  if(text == "sets21maunzi"){

    digitalWrite(p2,HIGH);

  }

  if(text == "sets20maunzi"){

    digitalWrite(p2,LOW);

  }

 

  if(text == "sets31maunzi"){

    digitalWrite(p3,HIGH);

  }

  if(text == "sets30maunzi"){

    digitalWrite(p3,LOW);

  }

 

  if(text == "sets41maunzi"){

    digitalWrite(p4,HIGH);

  }

  if(text == "sets40maunzi"){

    digitalWrite(p4,LOW);

  }

 

  if(text == "sets51maunzi"){

    digitalWrite(p5,HIGH);

  }

  if(text == "sets50maunzi"){

    digitalWrite(p5,LOW);

  }

 

  if(text == "sets61maunzi"){

    digitalWrite(p6,HIGH);

  }

  if(text == "sets60maunzi"){

    digitalWrite(p6,LOW);

  }

 

  if(text == "sets71maunzi"){

    digitalWrite(p7,HIGH);

  }

  if(text == "sets70maunzi"){

    digitalWrite(p7,LOW);

  }

 

  if(text == "sets81maunzi"){

    digitalWrite(p8,HIGH);

  }

  if(text == "sets80maunzi"){

    digitalWrite(p8,LOW);

  }

 

  if(text == "sets91maunzi"){

    digitalWrite(p9,HIGH);

  }

  if(text == "sets90maunzi"){

    digitalWrite(p9,LOW);

  }








 

  if(text == "stts0maunzi"){

    if(digitalRead(p0) == HIGH){ Serial.println("true"); } if(digitalRead(p0) == LOW){ Serial.println("false"); }

  }

  if(text == "stts1maunzi"){

    if(digitalRead(p1) == HIGH){ Serial.println("true"); } if(digitalRead(p1) == LOW){ Serial.println("false"); }

  }

  if(text == "stts2maunzi"){

    if(digitalRead(p2) == HIGH){ Serial.println("true"); } if(digitalRead(p2) == LOW){ Serial.println("false"); }

  }

  if(text == "stts3maunzi"){

    if(digitalRead(p3) == HIGH){ Serial.println("true"); } if(digitalRead(p3) == LOW){ Serial.println("false"); }

  }

  if(text == "stts4maunzi"){

    if(digitalRead(p4) == HIGH){ Serial.println("true"); } if(digitalRead(p4) == LOW){ Serial.println("false"); }

  }

  if(text == "stts5maunzi"){

    if(digitalRead(p5) == HIGH){ Serial.println("true"); } if(digitalRead(p5) == LOW){ Serial.println("false"); }

  }

  if(text == "stts6maunzi"){

    if(digitalRead(p6) == HIGH){ Serial.println("true"); } if(digitalRead(p6) == LOW){ Serial.println("false"); }

  }

  if(text == "stts7maunzi"){

    if(digitalRead(p7) == HIGH){ Serial.println("true"); } if(digitalRead(p7) == LOW){ Serial.println("false"); }

  }

  if(text == "stts8maunzi"){

    if(digitalRead(p8) == HIGH){ Serial.println("true"); } if(digitalRead(p8) == LOW){ Serial.println("false"); }

  }

  if(text == "stts9maunzi"){

    if(digitalRead(p9) == HIGH){ Serial.println("true"); } if(digitalRead(p9) == LOW){ Serial.println("false"); }

  }

 

 

}[/code]

[/SPOILER]



Oben