=> ttyS0: Read file text from JAVA


#1

Hello,

Pls, I am trying to read dev/ttyS0 file and I’ll like store it into a string, but cannot find the right JAVA code from the web… Can anyone help me?


#2

Arrghh, still can’t store file text to a string and set it as textview. Let me explain you that I want to do:

I have attached an Arduino to the M64 ttyS0 serial port. The Arduino gets temperature from sensor and sends by serial the temperature , I can see the dev/ttyS0 file is changing value correctly:

cat /dev/ttyS0

180

181

181

182 … (continue getting the data in a new line)

I am doing:

 public void start(View view) throws InterruptedException {

    Button empezar = (Button) findViewById(R.id.empezar); 

    Process process = null;

    DataOutputStream dos = null;

    try
    {

    process = Runtime.getRuntime().exec("su"); 

    dos = new DataOutputStream(process.getOutputStream()); 
    dos.writeBytes("exec 3</dev/ttyS0" + "\n"); //REDIRECT SERIAL OUTPUT TO FD 3. 
    dos.flush(); // Enter???
    dos.writeBytes("cat <&3 > /sdcard/temperatura.txt & PID=$!" + "\n"); //REDIRECT SERIAL OUTPUT TO FILE
    dos.flush();
    dos.writeBytes("kill $PID" + "\n"); //KILL CAT PROCESS
    dos.flush();
    dos.writeBytes("exec 3<&-" + "\n"); // FREE FD 3
    dos.flush();
    dos.close();
   } catch (IOException gpio) {
        gpio.printStackTrace();
    }  

And I am getting the serial data in a file named temperatura.txt in the external virtual storage main path.

Temperatura.txt contains integers, the content is:

185
186
187
188
189
190
190
191 … (continue getting data from /dev/ttyS0)

Then I tryed different methods to get the file text to String and the show it in a TextView

class LeeFichero {
public void main(String[] arg) {
    File archivo = null;
    FileReader fr = null;
    BufferedReader br = null;

    try {
        archivo = new File (Environment.getExternalStorageDirectory().getPath(), "temperatura.txt");
        fr = new FileReader (archivo);
        br = new BufferedReader(fr);

        // File read
        String linea = br.readLine();
        while((linea=br.readLine())!=null)

    // Write string to the textView    
    { 
            TextView temperatura = (TextView)findViewById(R.id.temperatura);
            temperatura.setText (linea);
        }

    }
    catch(Exception e){
        e.printStackTrace();
    }finally{
         try{
            if( null != fr ){
                fr.close();
            }
        }catch (Exception e2){
            e2.printStackTrace();
        }
    }
}
}

But when I press the start Button, the TextView don’t show any data

I also tried to copy file

sdcard/temperatura.txt

to

/data/data/myapkdirectory/files

and run the next code:

 try
    {
        BufferedReader fin =
                new BufferedReader(
                        new InputStreamReader(
                                openFileInput("temperatura.txt")));

       String texto = fin.readLine();
        fin.close();
        TextView temp = (TextView)findViewById(R.id.temperatura);
        temp.setText((CharSequence) texto);
     }
    catch (Exception ex)
    {
        Log.e("Ficheros", "Error al leer fichero desde memoria interna");
    }

   {
      TextView temp = (TextView) findViewById(R.id.temperatura);
        temp.setText("texto");
    }

Again, I can’t get the text file data in the TextView

Pls, any help Will be appreciated. How can I get ttyS0 data to a string and show it in a TextView correctly in JAVA? Or I am in the wrong way???


#3

Hello, more advances:

I can see if that I do " cat /dev/ttyS0 > data/data/myapkpackage/files/temp.txt" ,then I get a file named temp.txt in my apk files directory, but I can´t set it to a TextView with:

  try
  {
BufferedReader fin =
    new BufferedReader(
        new InputStreamReader(
            openFileInput("temp.txt")));

String text = fin.readLine();
fin.close();
TextView temperature = (TextView)findViewById(R.id.tvtemp);
temperature.setText("text");

} catch (Exception ex) { Log.e(“Files”, “Error reading internal memory”); }

IMPORTANT If I edit the temp.txt in the data/data/myapk/files folder with a text editor (manually) and then save , and run the java code again, the temp.txt content will be displayed in the TextView without any problem.

IMPORTANT2 Also, if I try to create and write the file temp.txt with this code:

try
{
OutputStreamWriter fout=
    new OutputStreamWriter(
        openFileOutput("temp.txt", Context.MODE_PRIVATE)); //  also works OK with MODE_APPEND

fout.write("Testing Reading test.");
fout.close();
}
catch (Exception ex)
{
Log.e("Ficheros", "Error al escribir fichero a memoria interna");

And then I run again the “file Reading from internal memory” JAVA code:

try
  {
BufferedReader fin =
    new BufferedReader(
        new InputStreamReader(
            openFileInput("temp.txt")));

String text = fin.readLine();
fin.close();
TextView temperature = (TextView)findViewById(R.id.tvtemp);
temperature.setText("text");
}
 catch (Exception ex)
{
Log.e("Files", "Error reading internal memory");
}

Why cat /dev/ttyS0 > data/data/myapkpackage/files/temp.txt don´t créate a valid .txt file? How solve it>?