Problem sending string through serial port

Hey all

I am currently trying to develop a java program which is able to send commands through the serial port to a Nudam 6021 module.

Everything works fine, except that the Nudam module does not understand the commands from the java program. I can see that the Nudam module recieves the command, but it just doesn't understand it. I have tried to investigate the problem and to me there seems to be a problem with my java code.

I have tried sending the command to the nudam module by using Hyperterminal, Borland c builder and a program called DCON. And every time i use something other than the java program, the Nudam module will understand the command. Or to put it another way, my java class is the only destination from where the nudam module is unable to understand the command.

I should also note that i have tried sending the command with hyperterminal to another computer which recieved the command with hyperterminal, and the command showed up on the other computer just fine.

I use BlueJ as my java editor, and i have the javax.comm package installed. i also have jdk.1.5.0_06 installed.

Here's the java code:

import java.io.*;

import java.util.*;

import javax.comm.*;

//Class SerialTransmit is able to transmit a string

public class SerialTransmit

{

private Connect connection;//An object of class Connect

private String messageString = "#0000.000";//"Serial transmit test.\r\n"; //String to send

private OutputStream outputStream;//Output string

public SerialTransmit()

{

}//End of constructor

/*Send the messageto the serial connection */

public void Transmit()

{

//Get the stream

try{outputStream=connection.serialPort.getOutputStream();

} catch (IOException e) {}

//Send the stream

try {

outputStream.write(messageString.getBytes());

} catch (IOException e) {

System.out.println("Error by writing to the serial port");}

}//End of Transmit

}

I am preatty new to java programming, but for some reason i suspect some sort of charset problem to be the reason for my program not working. I don't think that the command is sent out in the correct format.

Does anyone know a solution to this problem? I have tried for 2 days now to solve the problem, but i'm getting nowhere. Any help is much appreciated.

[2431 byte] By [Runiloa] at [2007-11-27 6:01:25]
# 1
If we only knew what kind of bytes a Nudam module expects, then we could tell you whether you have an encoding problem...
CeciNEstPasUnProgrammeura at 2007-7-12 16:41:18 > top of Java-index,Java Essentials,Java Programming...
# 2

Yeah, i know. I've been trying to find out, but i can't find any information on this, or i have misunderstood the question.

If anyone is interested, the datasheet for the module can be found here: http://www.nudaq.com/download/NUDAM/ND602X.pdf

Maybe it needs to be completely free of encoding? If it is even possible. I say this because it also works to send commands from microcontrollers to the nudam module.

Runiloa at 2007-7-12 16:41:18 > top of Java-index,Java Essentials,Java Programming...
# 3
Can you instead of writing the bytes to the serial port, write them to a file? Both in C Builder and your Java program? then compare.
CeciNEstPasUnProgrammeura at 2007-7-12 16:41:18 > top of Java-index,Java Essentials,Java Programming...
# 4

Great idea.

I just tried it out, and now used the following code:

import java.io.*;

import java.util.*;

import javax.comm.*;

//Class SerialTransmit is able to transmit a string

public class SerialTransmit

{

private Connect connection;//An object of class Connect

private String messageString = "#0000.000";//"Serial transmit test.\r\n"; //String to send

private OutputStream outputStream;//Output string

public SerialTransmit()

{

}//End of constructor

/*Send the messageto the serial connection */

public void Transmit()

{

//Get the stream

try{outputStream=connection.serialPort.getOutputStream();

} catch (IOException e) {}

//Send the stream

try {

outputStream.write(messageString.getBytes());

} catch (IOException e) {

System.out.println("Error by writing to the serial port");}

try{

// Create file

FileWriter fstream = new FileWriter("out.txt");

BufferedWriter out = new BufferedWriter(fstream);

out.write(""#0000.000");

//Close the output stream

out.close();

}catch (Exception e){//Catch exception if any

System.err.println("Error: " + e.getMessage());

}

}//End of Transmit

}

But as you can see, i have hardcoded the value, rather than writing out the same variable as the one sent to the comport. I don't know how to write the same variable to the file. However, the hardcoded value looks like it should in the file

Message was edited by:

Runilo

Message was edited by:

Runilo

Message was edited by:

Runilo

Runiloa at 2007-7-12 16:41:18 > top of Java-index,Java Essentials,Java Programming...
# 5

> as you can see,

All I can see is a pile of unformatted junk. Please read this:

http://forum.java.sun.com/help.jspa?sec=formatting

> i have hardcoded the value, rather than

> writing out the same variable as the one sent to the

> comport. I don't know how to write the same variable

> to the file.

Instead of

outputStream=connection.serialPort.getOutputStream();

use

outputStream=new FileOutputStream(someFile);

By the way, you're not closing that stream anywhere, do you?

CeciNEstPasUnProgrammeura at 2007-7-12 16:41:18 > top of Java-index,Java Essentials,Java Programming...
# 6

thanks for the help.

I got it to work, and wrote to a file from java and c++. Both look exactly the same on the file, and both look like they should.

Could it be this line in my program that distorts the data in some way?: outputStream.write(messageString.getBytes());

And no, i'm not closing the stream anywhere

null

Runiloa at 2007-7-12 16:41:18 > top of Java-index,Java Essentials,Java Programming...