PORTS Forum
Ports and Interfaces => Serial Ports => Topic started by: TOMOYO on March 14, 2011, 05:53:32 am
-
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
-
Get the serial and Ethernet interfaces working separately (do a loopback application to test), then put them together.
These might help:
http://www.maxim-ic.com/products/tini/devguide.cfm
http://www.janaxelson.com/eeccode.htm
Jan
-
Hello,
thanks for the links, unfortunately I already found them.
I tried something like that : TINI card is plugged to a PC by RS232 communication via hyperterminal, and also linked by ethernet to a telnet session. Thing is, if i turn on hyperterminal telnet session stops. And I don't understand this.
edit : I found what I wanted most likely with the CommTester example provided by tini firmware.
But I meet some errors at the compilation, am I supposed to change some parts of the code? It was supposed to be working as soon as I pasted it on my computer and got so much problems.
What's the best thing to use? I'm only working with the dos console of windows.
Sofiène HAMMAD
-
This forum archive might have something useful:
http://osdir.com/ml/hardware.microcontrollers.tini/
Jan
-
Hi, I understood a few things thanks to the link you gave me, after analyzing the SerialToEthernet code, I decided to use it since it fits perfectly to my project. However I meet some short issues at the execution such as :
- When I do it by netbeans : the java application doesn't find the TINI card, and it's stuck to " waiting for connection"
- When I try to do it by DOS by typing : java serialtoethernet.Serial COM1 , it gives me a classpath problem i guess but i don't have a clue to solve it :
java Serial
Exception in thread "main" java.lang.NoClassDefFoundError: Serial (wrong
name:
serialtoethernet/Serial)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Thanks in advance
regards sofiène HAMMAD
-
A java forum like:
www.coderanch.com/forums
might be able to help.
Jan