Jump to content


Photo

JAVA ADC PcDuino


  • Please log in to reply
1 reply to this topic

#1 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 13 February 2015 - 03:16 AM

hello all, I have a question, I've been doing a java program, in order to use the ADC of my PcDuino, I used the GPIO code, but I get this error.

 

 

mini_131004112054691423.png

 

as I can solve this errror, I put the code.

import java.io.File;
import java.io.FileInputStream;

public class ADC {
   static String analogPinDir = "/proc/";

   static File f;
   static FileInputStream fis;

   public static void main(String[] args) throws Exception {
      System.out.println(AnalogRead(0));
   }

   public static double AnalogRead(int whichpin) throws Exception {
      String[] avalableAnalogs = { "adc0", "adc1", "adc2", "adc3", "adc4",
            "adc5" };
      String contents = "";
      double toreturn = -1.0;
      int contint = 0;

      try {
         File f = new File(analogPinDir + avalableAnalogs[whichpin]);
         FileInputStream fis = new FileInputStream(f);
         while ((contint = fis.read()) != -1) {
            contents += (char) contint;
         }
         if (contents.contains(":")) {
            int i = contents.indexOf(":");
            contents = contents.substring(i + 1);
         }
         toreturn = Double.valueOf(contents);
         fis.close();
      } catch (Exception e) {
         e.printStackTrace();
         throw new Exception(
               "An exception occurred in trying to read from analog pin " + avalableAnalogs[whichpin] + ":\n" + e.getMessage());
      }

      return toreturn;
   }
}


thanks



#2 jim

jim

    Advanced Member

  • Administrators
  • 3,385 posts

Posted 13 February 2015 - 03:16 AM

I was also having the same problem. It is due to the reading loop.

This one should fix it.

 

import java.io.File;
import java.io.FileInputStream;

public class ADC {
    final static String ANALOG_PIN_DIR = "/proc/";
    final static String[] AVALABLE_ANALOGS = {"adc0", "adc1", "adc2", "adc3", "adc4", "adc5"};

    public static int analogRead(int pin) throws Exception {
        int bytesRead = 0;
        int bytesToRead = 12;
        byte[] buffer = new byte[bytesToRead];

        String result;
        try {
            File f = new File(ANALOG_PIN_DIR + AVALABLE_ANALOGS[pin]);
            FileInputStream fis = new FileInputStream(f);

            while (bytesRead < bytesToRead) {
                int read = fis.read(buffer, bytesRead, bytesToRead - bytesRead);
                if (read == -1) break;
                bytesRead += read;
            }

            result = new String(buffer);
            if (result.contains(":")) {
                result = result.split(":")[1].trim();
            }

            fis.close();

            return Integer.parseInt(result);
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("An exception occurred in trying to read from analog pin " + AVALABLE_ANALOGS[pin] + ":\n" + e.getMessage());
        }

    }

    public static void main(String args[]) throws Exception {
        if (args.length != 2) {
            System.out.println("Wrong number of args: Eg. java ADC <read|continuous-read>  <pin number>");
            return;
        }

        ADC adc = new ADC();
        int pin = Integer.parseInt(args[1]);

        switch (args[0]) {
            case "read":
                System.out.println(adc.analogRead(pin));
                break;
            case "continuous-read":
                while (true) {
                    System.out.println(adc.analogRead(pin));
                    Thread.sleep(1000);
                }
            default:
                System.out.println("invalid cmd: " + args[0]);
                break;

        }
    }

}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users