Author Topic: TINIm400 timed reading/writing  (Read 11870 times)

TOMOYO

  • Member
  • ***
  • Posts: 5
TINIm400 timed reading/writing
« on: May 11, 2011, 08:01:39 am »
Hi,
I already posted on this forum. I'm currently dealing with a TINIm400 module in order to set a sensor->RS232->TINI->Ethernet->PC/Server Communication.
So now that the communication works, I have to improve some things in the code such as :

-Reading the data and sending it must be done each seconds.
-Previously, I have to send something to the sensor in order to set it in "sending mode".
-If the data collected is the same as before, it shouldn't be sent.

Here is my code, any idea about how could I improve it?

Code: [Select]
import javax.comm.*;
import java.io.*;
import java.net.*;
import java.lang.*;
import com.dalsemi.tininet.*;
import com.dalsemi.system.*;

class SerialToEthernet extends Thread {
// Utilisation d'un buffer de 1024 pour le transfert de données
private static final int INPUT_BUF_LEN = 1024;
// Serial port and associated streams
private SerialPort sp;
private InputStream spin;
private OutputStream spout;
// Socket and associated streams
private Socket s;
private InputStream sin;
private OutputStream sout;

private SerialToEthernet(String server, int port, int speed)
throws Exception {
    // Creation et initialisation du port serie
    sp = (SerialPort)
    //ouverture du port serie
    CommPortIdentifier.getPortIdentifier("serial0").open(
    "SerialToEthernet", 5000);
    // RTS
    TINIOS.setRTSCTSFlowControlEnable(0, true);
    // 8 data bits, 1 stop bit, pas de parité
    sp.setSerialPortParams(speed, SerialPort.DATABITS_8,
    SerialPort.STOPBITS_1,
    SerialPort.PARITY_NONE);
    // Controle de flux
    sp.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN |
    SerialPort.FLOWCONTROL_RTSCTS_OUT);
    // Initialisation des flux d'entrées et de sorties'
    spin = sp.getInputStream();
    spout = sp.getOutputStream();
    // Timeout de lecture de 100ms
    sp.enableReceiveTimeout(100);
    // Parametrage du seuil de reception et du buffer
    sp.enableReceiveThreshold(INPUT_BUF_LEN);
    // Connection au serveur TCP
    s = new Socket(server, port);
    sin = s.getInputStream();
    sout = s.getOutputStream();
    // Création et lancement d'un thread Serial To Ethernet'
    (new Thread(new SerialReader(this, INPUT_BUF_LEN))).start();
    // Lancement du thread de maintenance
    super.start();
    }
private volatile boolean running = true;
private int serialTotal = 0;
private int networkTotal = 0;

public void run() {
    while (running) {
    try {
    Thread.sleep(60000);
    } catch (InterruptedException ie) {}
    //System.out.println("Bytes received from serial:" + serialTotal);
    //System.out.println("Bytes received from network:" + networkTotal);
}
}

private class SerialReader implements Runnable {
private byte[] serBuf;
private Thread maint;
private SerialReader(Thread maint, int size) {
serBuf = new byte[size];
this.maint = maint;
}
public void run() {
while (running) {
try {
System.out.println("Lecture des datas sur le port série");
int count = spin.read(serBuf, 0, serBuf.length);
//S'il existe encore des données à lire sur le port alors on continue la lecture'
if (count > 0) {
System.out.println("Ecriture des datas sur Ethernet");
sout.write(serBuf, 0, count);
serialTotal += count;
}
} catch (IOException ioe) {
// Problème de connection avec le serveur
System.out.println(ioe.getMessage());
ioe.printStackTrace();
running = false;
maint.interrupt();
break;
}
}
}
}
}









Thanks

Regards

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: TINIm400 timed reading/writing
« Reply #1 on: May 14, 2011, 12:10:37 pm »
A Java forum might have suggestions. Try:

http://www.javaranch.com/

Jan