Hi guys,
I'm currently working on a project with a TINIm400 board, in order to get datas from sensors by RS232, and transmit them to a server by Ethernet.
The main issue is that regarding the age of this board, I can't find any interesting source code which could help me to code that in java.
Here is a sample of what i coded, I'm testing this with a GPS, so basically receiving NMEA frames by RS232.
import java.io.*;
import java.util.*;
import javax.comm.*; //Utilisation de l'API javax.comm
public class LectureSerie {
//declaration des variables
static CommPortIdentifier portId;
static Enumeration portList;//Liste des ports série de la carte
static InputStream in;
static SerialPort serialPort;
//boolean inputBufferEmptyFlag;
// buffer de lecture
static byte[] data = new byte[10];
public static void main ( String [] args){
int i=0;
boolean flag=false;
boolean portFound = false ;
String defaultport = "serial0"; // port serial0 est le port par défaut
//aucun argument passé dans le tableau args
if (args.length >0){
defaultport= args[0];
}
portList = CommPortIdentifier.getPortIdentifiers();//renvoi les ports de la machine
while (portList.hasMoreElements()){
portId = (CommPortIdentifier) portList.nextElement();//nom du port à utiliser
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL){
if (portId.getName().equals(defaultport)){
System.out.println("Port serie trouve :" + defaultport);//port trouvé
portFound=true;
try{
serialPort=(SerialPort) portId.open("AppLectureSerie",5000); //ouverture du port
System.out.println("Port serie ouvert");
}catch(PortInUseException e) {
System.out.println("Port deja utilise.");//si port actuellement utilisé, gestion d'exception
continue;
}
try{
//récupération du flux d'entrée
in = serialPort.getInputStream();
}catch(IOException e) {}
try {
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);//configuration du port
System.out.println("Port serie configure");
}catch(UnsupportedCommOperationException e) {}
System.out.println("Ecoute du port serie");
try{
//Tant qu'il y a des octets dispo
while (i<10) {
System.out.println("ligne : " + (i+1) );
int flux = in.read(data);
//afficher le nombre d'octets transmis
System.out.println("| nb octet lu : " + flux +" | ");
System.out.println("| contenu tab : " + data[i]+" | " );
System.out.println("-----------------------");
i++;
}
}catch(IOException e){}
flag=true;
//fermeture du port série
if (flag){
System.out.println("Fermeture du programme");
//serialPort.close();
System.exit(1);
}
}
}
if (!portFound){
System.out.println("port " + defaultport + " non trouve");
}
}
}
}
Thanks to this code, I get some datas [ weird characters ] but they're emitted randomly, the gps sends it every second but the board doesn't screen it regularly.
In fact, my final objective is to get this : SENSORS -> RS232->TINI->ETHERNET->SERVER
Any idea ? I'm a bit lost.
I also have a little question about the fact serial server stops and starts, what's happening in fact?
Thanks for your help.
Sofiene HAMMAD