Search code examples
javaarmembedded-linuxangstrom-linux

Read byte from linux device char file in java


I'm writing a java software that works on a control unit with Angstrom Linux, ARM platform. This software must read the status of the gsm modem from the / dev / gsm_status file and know if the modem is on or off.

This file is a character file, so far I have read the value from this file with the command with bash

dd if=/dev/gsm_status count=1 bs=1 2>/dev/null | hexdump -e '1/1 "%X\n"'

in this way the values ​​that are displayed are 0 or 1.

But I can not read the file via java, I tried RandomAccessFile and FileInputStream but I always receive IO exception.

I think I was completely wrong approach, could you give me some indication?

Thank you so much to everyone

update

This is java code

RandomAccessFile f = new RandomAccessFile(fileDevGsmPowerStatus.getAbsoluteFile(), "r");
        f.seek(0);
        System.out.println("--" + f.readChar()+"--");

This is error with RandomAccessFile and with FileInpuntStream is the same

java.io.IOException: Input/output error
        at java.io.RandomAccessFile.read(Native Method)
        at java.io.RandomAccessFile.readChar(RandomAccessFile.java:743)
        at winger.fuelfeed.client.abt21.gsm.CinterionBGS2.getModemPowerState(CinterionBGS2.java:207)
        at winger.fuelfeed.client.FuelfeefClient.main(FuelfeefClient.java:31)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:616)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)

Solution

  • Perhaps try reading a single byte of data explicitly (as opposed to say, treating the input as a string as might be the case with some readers):

    DataInputStream input = new DataInputStream(new BufferedInputStream(
        new FileInputStream("/dev/gsm_status")
    ));
    
    byte b = input.readByte();