The Artima Developer Community
Sponsored Link

Java Answers Forum
Serial Port

2 replies on 1 page. Most recent reply: Apr 28, 2003 6:26 AM by Rpaha

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Rpaha

Posts: 3
Nickname: rpaha
Registered: Apr, 2003

Serial Port Posted: Apr 25, 2003 7:34 AM
Reply to this message Reply
Advertisement
Hi

I've installed javax.comm and seen the examples sampleread and samplewrite.
Now I want to communicate with a serial port. On the port, I've a reader, who reads chips. The chips have an ID and some other informations.
I can open the port and get the input- and outputstream, but I can't communicate with the port.
Normally if I would write "V?\r" on the outputstream, the inputstream should give me the version of the reader. But that doesn't come. Instead, I get this: .
I don't know why.
If I write again something on the outputstream, the inputstream is empty.

Here is the function I use to write and read on the port:

String messageString = "V?\r";
byte[] readBuffer = new byte[256];

//Write
try {
out.write(messageString.getBytes());
} catch (IOException e) {}
//Read
try {
in = serialPort.getInputStream();
while (in.available() > 0) {
numBytes = in.read(readBuffer);
SB.append((char) numBytes);
}
} catch (Exception e) { System.out.println(e);}

Thanks for helping


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Serial Port Posted: Apr 25, 2003 8:46 AM
Reply to this message Reply
What does this line do?

SB.append((char) numBytes);


If this is a StringBuffer that you're printing out, then you will have problems. numBytes is the number of bytes read in from the input stream (not the content of the input stream which you have placed in readBuffer.

Adam

Rpaha

Posts: 3
Nickname: rpaha
Registered: Apr, 2003

Re: Serial Port Posted: Apr 28, 2003 6:26 AM
Reply to this message Reply
Thanks

It wasn't the problem. Now, if I do that with a serialEvent, I only get the string I sent to the port. Means: I can send data to the port, but I get the same data back. A little bit bizzarre!

public void serialEvent(SerialPortEvent event) {

switch (event.getEventType()) {

case SerialPortEvent.DATA_AVAILABLE:
StringBuffer SB;
SB = new StringBuffer();
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
SB.append(numBytes);
System.out.println("\nSB: " + SB);
}

System.out.print(new String(readBuffer));
} catch (IOException e) {}

break;
}
}

Flat View: This topic has 2 replies on 1 page
Topic: How can i break the reference Previous Topic   Next Topic Topic: small help of replacing event handler with the Swing

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use