JSerialComm is a Java library designed to provide a platform-independent way to access standard serial ports without requiring external libraries, native code, or any other tools. It is meant as an alternative to RxTx and the (deprecated) Java Communications API, with increased ease-of-use, an enhanced support for timeouts, and the ability to.
![]()
Greenhorn
posted 16 years ago
I have a device which is connected to serial port and I am trying to read data from the device through serial port(COM1).
I have a Applet by which I am able to open Serial Port Connection and able to read data from the input stream. But I am not able to get full content whcih I am expecting. Program piece of code looks something like this.. If somebody can throw somelight where I am going wrong [Added code tags - Jim] [ January 22, 2004: Message edited by: Jim Yingst ]
Author and all-around good cowpoke
posted 16 years ago
If I'm reading this right, your method quits as soon as there are no more byte in the input buffer - but the other end of the connection still has bytes to send, they just have not gotten there yet.
I think you need a better way for the applet to know it has reached the end of the data. Bill
Greenhorn
posted 16 years ago
You are right I have to have better way of signifying end of data in Input Stream and in this way I way I went a head.
Now end of data in Stream is signified by Ctrl/Z Character and I am able to get full data but I am getting some special characters like '?' also which I am not expecting
Wanderer
posted 16 years ago
[WB]: If I'm reading this right, your method quits as soon as there are no more byte in the input buffer - but the other end of the connection still has bytes to send, they just have not gotten there yet.
I'm not sure that's a problem. The method quits when it's read all the bytes currently in the buffer, but when more bytes arrive, won't they set off a new SerialPortEvent? For a given SerialPortEvent, once we reach the end of the buffer there really isn't anything to do but exit the method and wait for the next event. However the program needs to make sure that data from separate DATA_AVAILABLE events eventually gets reassembled into a single String or something. It's hard to tell what happens in the code shown - a new StringBuffer is created for each event, and data is appended to is, but then nothing is done with the StringBuffer. I'm guessing there's some code not shown here - is there a write(inputBuffer.toString()) somewhere? I think the key here is, wherever we put the data, make sure that the next time more data is available, it will get appended to the same place. [RV]: Now end of data in Stream is signified by Ctrl/Z Character and I am able to get full data OK, good. I'm not sure what other standard protocols are out there for this, but yours seems reasonable. but I am getting some special characters like '?' also which I am not expecting Sounds like an encoding problem. When you write you are telling the system to treat each byte as if it's a Unicode character. That will work fine if the encoding is US-ASCII or ISO-8859-1, since those are sybsets of the first 128 and 256 Unicode values, respectively, and they fit nicely into a byte. However for several other common encodings (and many, many uncommon ones) a byte cannot simply be cast to a char to decode it to Unicode. I would say you need to find out what encoding is being used here, and use something like an InputStreamReader or a new String(byte[], String) to specify the encoding to be used to interpret each byte. If you know for sure that it's a single byte encoding this is easy. But if a single character may take more than one byte (as is the case for UTF-8, UTF-16 and other encodings) then there's a possibility that a single character may be split across two different DATA_AVAILABLE events, which makes things a little more complex. You could just append everything to a single ByteArrayOutputStream, to be converted to a string only after you know all the data has been sent. (Except then, how will you know if ^Z has been sent? You'd need another protocol.) Or you can write all bytes read into a PipedOutputStream, and in another thread use a PipedInputStream wrapped in an InputSreamReader to read bytes as they become available. ![]()
Greenhorn
posted 16 years ago
Thankyou everyone now problem is resolved I am able to get everthing.
I was to suppose to write something to output stream os.write(6) after new character is read. So now I can run the application when applet program is standalone. But when I try to run under browser it gives me following exception Preparing to get serial connection java.lang.NoClassDefFoundError: javax/comm/SerialPortEventListener at java.lang.ClassLoader.defineClass0(Native Method) at java.lang.ClassLoader.defineClass(Unknown Source) at java.security.SecureClassLoader.defineClass(Unknown Source) at sun.applet.AppletClassLoader.findClass(Unknown Source) at sun.plugin.security.PluginClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.applet.AppletClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source) at SerialDemoApplet.<init>(SerialDemoApplet.java:38) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at java.lang.Class.newInstance0(Unknown Source) at java.lang.Class.newInstance(Unknown Source) at sun.applet.AppletPanel.createApplet(Unknown Source) at sun.plugin.AppletViewer.createApplet(Unknown Source) at sun.applet.AppletPanel.runLoader(Unknown Source) at sun.applet.AppletPanel.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Can anyone solve this problem. Is i have to do some setting in web browser.
Ranch Hand
posted 16 years ago
Dear Rawat,
Do you think your problem a little bit similar to your first problem? or it is something else? or anybody can help? just click SNMP Application and you will know my problem thanks
Greenhorn
posted 16 years ago
Dear Qusay, I am able to resolve my first problem and able to read the bytes from COM port. This I was doing through Java Applet as a stand alone program. When I run this applet in web browser it doesnot work. ![]() Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2022
Categories |