|
Download[^]
Use winrar to get to the .jar and two .dll files
Copy the .jar to jre/lib/ext and the two .dll two jre/bin
Then I want you two run this code:
Code 1:
import gnu.io.*;
public class demo
{
public static void main(String[] args)
{
listPorts();
}
static void listPorts()
{
java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
while ( portEnum.hasMoreElements() )
{
CommPortIdentifier portIdentifier = portEnum.nextElement();
System.out.println(portIdentifier.getName() + " - " + getPortTypeName(portIdentifier.getPortType()) );
}
}
static String getPortTypeName ( int portType )
{
switch ( portType )
{
case CommPortIdentifier.PORT_I2C:
return "I2C";
case CommPortIdentifier.PORT_PARALLEL:
return "Parallel";
case CommPortIdentifier.PORT_RAW:
return "Raw";
case CommPortIdentifier.PORT_RS485:
return "RS485";
case CommPortIdentifier.PORT_SERIAL:
return "Serial";
default:
return "unknown type";
}
}
}
Code 2:
public static HashSet<CommPortIdentifier> getAvailableSerialPorts() {
HashSet<CommPortIdentifier> h = new HashSet<CommPortIdentifier>();
Enumeration thePorts = CommPortIdentifier.getPortIdentifiers();
while (thePorts.hasMoreElements()) {
CommPortIdentifier com = (CommPortIdentifier) thePorts.nextElement();
switch (com.getPortType()) {
case CommPortIdentifier.PORT_SERIAL:
try {
CommPort thePort = com.open("CommUtil", 50);
thePort.close();
h.add(com);
} catch (PortInUseException e) {
System.out.println("Port, " + com.getName() + ", is in use.");
} catch (Exception e) {
System.err.println("Failed to open port " + com.getName());
e.printStackTrace();
}
}
}
return h;
}
Post your results from both codes.
Good luck
|
|
|
|
|
Ok , fellas. Let me rephrase my question.
My problem is that I can detect my ports and the connection works over the two PCs. However, when I tried to send data over to the other PC, the message being sent over looks something like "@.pp...." in my output, which is incorrect. I wanted the output to be in hex, something like
"AA BC 25 EE AC...".
Any suggestions?
DA: http://www.pohcbsonic.deviantart.com/
Blog: http://www.pohcbsonicx.blogspot.com/
Homepage: http://www.pohcbsonic.tripod.com/
|
|
|
|
|
Ok , fellas. Let me rephrase my question.
My problem is that I can detect my ports and the connection works over the two PCs. However, when I tried to send data over to the other PC, the message being sent over looks something like "@.pp...." in my output, which is incorrect. I wanted the output to be in hex, something like
"AA BC 25 EE AC...". I'm quite new to java.
Any suggestions?
DA: http://www.pohcbsonic.deviantart.com/
Blog: http://www.pohcbsonicx.blogspot.com/
Homepage: http://www.pohcbsonic.tripod.com/
|
|
|
|
|
pohcb_sonic wrote: the message being sent over looks something like "@.pp...." in my output
Unless you convert it to Hex before you send it then this is how it will look. I suspect you just want to view it in Hex, which menas you need to capture it first and then use software to display it as hex characters.
|
|
|
|
|
import java.io.*;
import java.lang.*;
public class ByteToHexa{
public static void main(String[] args) throws IOException{
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the byte number:");
byte b =(byte)6;
String str = buff.readLine();
int i =Integer.parseInt(str);
String hexString = Integer.toHexString(i);
System.out.println("Hexa is:=" + hexString);
}
}
Does this do it?
|
|
|
|
|
Member 4277480 wrote: Does this do it?
Well apart from the obvious error(s), I don't think so; but then I'm not the OP. If you read OP's previous, he/she wants to see the captured network text displayed in Hex rather than ascii characters.
|
|
|
|
|
If you read the code by poster he/she is transmitting a Byte Array, for each element in that array there is a byte which is an Integer so to get the Hex of it use the code I posted above and what obvious error are you talking about and ASCII charecters??
|
|
|
|
|
pohcb_sonic wrote: However, when I tried to send data over to the other PC, the message being sent over looks something like "@.pp...." in my output, which is incorrect. I wanted the output to be in hex, something like
"AA BC 25 EE AC...".
Member 4277480 posted this code:
public static void main(String[] args) throws IOException{
BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the byte number:");
byte b =(byte)6;
String str = buff.readLine();
int i =Integer.parseInt(str);
String hexString = Integer.toHexString(i);
System.out.println("Hexa is:=" + hexString);
}
}
I don't see what the following statement is for:
byte b =(byte)6;
The remaining code reads some input from the console, parses it to an integer and then displays it in Hex. This is not converting the characters of the message passing between the two PCs.
Or did I miss something obvious?
|
|
|
|
|
1. My bad for extra statement that does not make it an error though
2. Let me clarify:
In the previous code I am only saying that if I have a message suppose it is "hello" I want to transmit that message, the first thing I will do is convert it to a byte array and send it, then use the Integer.toHexString(i); to get the Hex of that message from every byte in that array. This in short is a simulation of what is to be done not the exact code to do it.
|
|
|
|
|
Member 4277480 wrote: then use the Integer.toHexString(i); to get the Hex of that message from every byte in that array. This in short is a simulation of what is to be done not the exact code to do it.
But, as I said earlier there is an error. Your Integer.toHexString(i); statement is displaying the hex value of the index rather than the byte within the array. Something of the form Integer.toHexString(byteArray[i]); would be more useful.
|
|
|
|
|
Ok fellas,
I have edited and updated my main Question in this thread. Pls refer to it. can kindly assist? thanks.
DA: http://www.pohcbsonic.deviantart.com/
Blog: http://www.pohcbsonicx.blogspot.com/
Homepage: http://www.pohcbsonic.tripod.com/
modified on Sunday, October 4, 2009 11:51 PM
|
|
|
|
|
int indx = 9;
EndianUtils.writeSwappedDouble(BytetxtMsg, 2, reverse(number));
ByteBuffer bbuf = ByteBuffer.allocate(8);
Byte buffer is 8 and is constant i.e does not change lets call it b, so we have two variables the indx and the 2 in the endian lets call it x. The relationship would be indx = b + x; as the minimum acceptable value.
|
|
|
|
|
the problem is that:
EndianUtils.writeSwappedDouble(BytetxtMsg, 2, reverse(number));
you just fix it:
EndianUtils.writeSwappedDouble(BytetxtMsg, 1, reverse(number));
Double has 8 bytes.bbuf has 9 bytes capacity and begins with index zero.But your offset is 2,so its capaticy will not be enough. it will throw out of bound exception.
|
|
|
|
|
Ok, I've already solved the EndianUtils problem. But the following error still exists.
<small><br />
<br />
ERROR: SerialComm.Comms<br />
ERROR: null<br />
ERROR: SerialComm.Comms<br />
ERROR: null<br />
<br />
</small>
Although I did use the try and catch method to catch the above errors in my code, I still couldn't figure out why the problems exist.
<small><br />
<br />
try{<br />
<br />
<br />
connect("COM2");<br />
}catch (Exception e) {<br />
System.err.println("ERROR: " + e.getMessage());
<br />
}<br />
<br />
}<br />
<br />
<br />
try{ <br />
C1.sendBytes(BytetxtMsg); <br />
} catch (Exception e) {<br />
System.err.println("ERROR: " + e.getMessage());
<br />
}<br />
<br />
</small>
DA: http://www.pohcbsonic.deviantart.com/
Blog: http://www.pohcbsonicx.blogspot.com/
Homepage: http://www.pohcbsonic.tripod.com/
|
|
|
|
|
hi guys,
i have learnt java beginners fundamentals and concepts,i want to learn j2ee can i learn it using books or it should be learnt only in training centers.please help me out.
|
|
|
|
|
Sun has a lot of information; here[^].
Install netbeans and start creating.
Buy a book. Even if you decide to take a training course, buy a book it will always be useful.
Start learning the big concepts first.
97.35% of EE work is the same as SE; so don't sweat it.
Enjoy!
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
thanks for the information provided
|
|
|
|
|
i have a web application in struts. i want to use the tiles for that application.is there any possibility?
|
|
|
|
|
Yes it is and a nice guide for it Link[^]
Good luck
|
|
|
|
|
Hi,
I tried the code given to send mails using lotus notes. I want to try the code for multiple reciepients. I tried the solution given i.e using ";" to separate the email ids. But its not working. Its not working even for cc and bcc.
|
|
|
|
|
Ask the guy who implemented it Link[^]
Good luck
|
|
|
|
|
I want to create a PDF file having the inputs specified at console through Java. I do not want to use ready made libraries like iText etc.
I dont have to create sophisticated PDF's, i just want to write strings in the PDF.
I started off :
import java.io.*;
public class FileHandling
{
public static void main(String args[]) throws IOException
{
FileOutputStream fos=new FileOutputStream("file.pdf");
DataOutputStream dos=new DataOutputStream(fos);
DataInputStream dis=new DataInputStream(System.in);
String str;
System.out.println("enter string value");
str=dis.readLine();
dos.writeChars(str);
dos.close();
fos.close();
}}
But this is not working, i guess i am missing the specifications of the PDF. Please provide me solution for it, and a code would be highly obliged
|
|
|
|
|
I don't know why you don't like Itext[^]? Anyway there is no simple way to do it unless you want to implement it your self. A nice article to read here in codeproject utilizes PDF Generation using XSLFO and FOP[^] but if I where you I would stick to Itext a simple example:
import java.awt.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.io.*;
import java.util.zip.*;
public class TextToPDF{
public static void main(String arg[]){
try{
InputStreamReader in= new InputStreamReader(System.in);
BufferedReader bin= new BufferedReader(in);
System.out.println("Enter text:");
String text=bin.readLine();
Document document = new Document(PageSize.A4, 36, 72, 108, 180);
PdfWriter.getInstance(document,new FileOutputStream("pdfFile.pdf"));
document.open();
document.add(new Paragraph(text));
System.out.println("Text is inserted into pdf file");
document.close();
}catch(Exception e){}
}
}
18 lines of code what do you think? Better than 16+ don't you think?
Regards
|
|
|
|
|
akashj87 wrote: FileOutputStream fos=new FileOutputStream("file.pdf");
You need to understand that this does not create a PDF file. The extension of "pdf" means nothing to Java or to Windows/UNIX, it is merely a convention for humans to recognise the type of file, but the application still has to do the work to create the file.
The definition of DataOutputStream.writeChars() states that it Writes a string to the underlying output stream as a sequence of characters . The structure of a PDF file is much more complex than this (see the specifications on the Adobe web site). The easiest way to achieve your objective is use the tools that are available.
|
|
|
|
|
you can use Apache FOP without having to learn XSL-FO or complex API. Just put the data you want convert in a XML file and use this XSL FO generator to create the PDF file.
|
|
|
|