serialport write and read on windows not working - java

I first tried to use javax.comm to connect to the serial port (COM4). It did not even open the serial port.
Then I tried to use rxtx libraries (rxtx-2.2pre2) to connect. It connects and writes the data but does not read any data from the serial port.
Is there any JDK/platform dependency to use javax.comm or rxtx libraries?
I am using:
Windows XP SP3,
JDK 1.6.0_22,
rxtx-2.2pre2,
USB to Serial adapter,
Portmon (by Microsoft) - to monitor activity on serial ports
Hyperterminal - to check if the COM port really works.
http://rxtx.qbang.org/wiki/index.php/Two_way_communcation_with_the_serial_port - sample code used to check read and write

After some digging, I was able to solve this myself, by explicitly setting the flow control mode. Even if you don't require flow control, setting it explicitly to FLOWCONTROL_NONE helps.
The same call was not required on Linux.
If you are setting the flow control to Hardware, I think it is also required to setRTS explicitly in order to get notifications in this mode. (Not verified).

I tried this code and was successful. You need to have three files with you:
comm.jar
javax.comm.properties
win32com.dll
and you need to put these files in specific directories:
comm.jar
in /java/jre6/lib/ext , /java/jdk_1.6.0.20/jre6/lib/ext , java/jdk_1.6.0.20/lib
win32com.dll
in /java/jre6/bin, /java/jdk_1.6.0.20/bin, /windwos/System32
javax.comm.properties
in /java/jdk_1.6.0.20/lib, java/jre/lib , /java/jdk_1.6.0.20/jre/lib

I would recommend trying out purejavacomm: http://www.sparetimelabs.com/purejavacomm/index.html
It is an implementation of javax.comm written in pure java + JNA, which completely solved the problem of portability between Windows and Linux for me. It should also work on OSX or FreeBSD (haven't tried), and should be easy to port to other OS-es that JNA supports, such as Solaris.
Another benefit is that you don't need to install a DLL like you do with rxtx, and that it doesn't seem to have a certain 100% CPU eating bug that rxtx sometimes has on windows.

Related

Windows change system volume programmatically with Java or CMD

Is there any way to change the system volume on Windows with Java or CMD programmatically?
I've found some command lines for Linux and Mac, but so far nothing for stupid Windows.
Take a look at a form post on oracles website https://forums.oracle.com/thread/2390172 you can see it is not possible from inside java using native libraries.
Quote from the oracle post: "Because Java is cross-platform, it cannot do platform-specific stuff like changing the volume or whatever you want to do to control the OS. You need to use the operating system's unique API layer to do it."
For command line I found this utility that seems to have what you are looking for http://www.nirsoft.net/utils/nircmd.html
If you don't want to rely on 3rd party executables you could either make your own exe or make a dll and look into using JNI.

Java ME SDK 3.2 (and 3.3, 3.0 as well) just won't start

When i try to launch it any way, Java ME just freezes. To be more specific, javaw.exe called by device-manager.exe seems to go into infinite loop, since not a single exception or error message is passed. Icon in tray appears, but its menu, instead of devices list, shows only one entry: Exit, which incidentally doesn't work. I can only shutdown it through task manager. And since device-manager is required for emulator, i can't work with it at all.
I tried versions 3.0,3.2,3.3 of Java ME SDK and this problem persists in each one. OS: Windows 7. JDK: 7u25.
I've tried each and every advice i found on the Internet and still can't get it working. Device-manager log shows that the problem starts upon calling "rmiRegistryPortFile".
[2013-07-02 19:20:53.070] DEBUG - strap.BasicObjectConfiguration - Calling create on rmiRegistryPortFile
That's always the last entry in the log.
There was only one way i've managed to get it working - by installing and running it under Windows Virtual PC. Curious thing is, under VM it's working fine in the same very OS (freshly installed Windows 7). But unfortunately that didn't really give much on the cause of the bug, and that's not a solution :( I have to somehow get it working without VM.
I tried reinstalling Windows, that didn't help. Looked through javaw I/O in Process Monitor and compared it with working one. It looks like one of the application threads suddenly shuts down after reading file "rt.jar" (when loading "rmiRegistryPortFile" i guess?), whereas working javaw writes to log-file immediately after that. Windows logs got nothing on the subject: no permission issues, no errors or warnings at the time.
Tried modifying PATH variable to the dir with rmiregistry.exe, did not help. Network sockets are available. Changed DEP settings, same.
Could anyone please help? I've spent days on this bug already.
It's definitely a permission problem. Try to look if any folder related to Java is "READ ONLY".
If you get this error message when trying to run midlets through the built-in emulator of the JavaME SDK 3.0, try disabling DEP for runMidlet.exe.
Data Execution Prevention (DEP) configuration can be found at the following place in Windows: Control Panel > System Security > System > Advanced system Settings > Advanced tab > Performance > Data Execution Prevention.
Add this file to the DEP exclusion list:
<javame-install-dir>\runtimes\cldc-hi-javafx\bin\runMidlet.exe
If things work for you now, complain loudly to Sun (now Oracle) that they need to make software without buffer overflows.
Personally I filed a bug-report against the JavaME SDK 3.0. You should do that too, or make your voice heard on the same bug-report that you're having this problem as well.
Freshly installed windows doesn't have msvcrtXX.dlls
Go to folder runtimes\\cdc-hi\\bin and copy Microsoft.VC80.CRT into runtimes\\cldc-hi-javafx\\bin. This problem will be fixed over autoupdate soon.
Problem with localhost
Please edit <javamesdk_installdir>\\toolkit-lib\\modules\\bootstrap\\conf\\system.properties and change
device-manager.object-registry.host=localhost to: device-manager.object-registry.host=127.0.0.1
Port 1299 might be taken
Please edit <javamesdk_installdir>\\toolkit-lib\\modules\\bootstrap\\conf\\system.properties and change
device-manager.object-registry.port=1299 to: device-manager.object-registry.port=1999
XP 64-bit
Please use 32-bit version of JDK.
Firewall
Make sure that firewall is not blocking communication on ports given in 3. Default port numbers are 1299 for windows and 1999 for Mac.
I have tried all those steps above to no avail, until I replaced my JDK jdk-8u117 with jdk-8u112 (Must be 32 bit) after reading this thread https://community.oracle.com/thread/4009110. I had to restart my machine after changing the Java version because it was not detecting right away after installation. I'm using Netbeans 7.4.

Why is getDeviceList() method in jpcap.JpcapCaptor class native in JEthereal?

I recently downloaded the JEthereal.jar file to capture packet traffic on my internet connection through JEthereal and also to study it. It reads the packets from a .pcap file just fine but on trying to capture packets it gave me a message that:
"No device found. This probably means that you installed JPcap but not
libpcap / WinPcap.Please follow the instructions on the website to
install it. It takes about 15 seconds and is well worth it."
So I downloaded its source code and opened it in Eclipse since I had already installed WinPcap.
On going through the code I came to the part where network devices are supposed to be discovered. The static method getDeviceList() from the JpcapCaptor class in the jpcap package which should return an array of network devices of type NetworkInterface. The following is its declaration given on the internet:
public static NetworkInterface[] getDeviceList()
However, in the source code that I have downloaded the method is left without a body and its been declared native i.e.
public static native NetworkInterface[] getDeviceList();
What should I do now to identify my devices? On searching on the net and on this portal I came to know that a jpcap.dll file should be present in my system32 folder (I am using Windows7) and I should install WinPcap for that. I had already installed WinPcap and there is no jpcap.dll file in the system32 folder. Also, i suppose i should add system32 to my PATH variable, i haven't done that yet since there is no jpcap.dll there.
Thanx in advance!!
If "On searching on the net and on this portal I came to know that a jpcap.dll file should be present in my system32 folder (I am using Windows7) and I should install WinPcap for that.", then, as the saying goes, someone is wrong on the Internet.
jpcap.dll is not part of WinPcap, and you do not install WinPcap to get it. You need to install Keita Fujii's Jpcap (he no longer seems to be working on it, and the site the JEthereal site links for it no longer exists, but the JEthereal site has some local download links, and some people have created their own forks of it).
Have you installed any version of Jpcap? If so, where did you get it from? It may be a version that doesn't work with JEthereal.
Why is getDeviceList() method in jpcap.JpcapCaptor class native in JEthereal?
Because getDeviceList() is implemented as native (C/C++) code.
on trying to capture packets it gave me a message that says: "No device found..."
I am using Windows7
Touchè. Starting with Windows Vista (which is the first Windows that implement root-like pretensions), you need to run jpcap as an administrator. This has to be done at least the first time that you run jpcap after the system has been restarted (so that the winpcap library is authorized to access network resources).
From jpcap FAQ
Q: I cannot obtain the list of network interfaces or cannot capture any packets. Why?
It may be because you don't have administrative privileges, which are
required to run Jpcap-based applications.
On Windows 2000/XP, your account should be in the Administrator group.
On Windows Vista, even if your account is in the administrator group,
you still need to use "run as administrator" option. To use the
option, right-click the application icon, and choose "run as
administrator". If your application is command-line based, you need to
open Command Prompt by using "run as administrator" option, and run
your application within that Command Prompt.

Arduino(RXTX) under Linux? - works in Windows

I'm currently developing a programm with Java to interact with an Arduino module. Now in Windows, it's running pretty well so far (I receive what I expect; I can work with the data), but in Linux (Ubuntu in my case); it's like the opposite. I installed Arduino and rxtx-java packages.
When I tried to use the Arduino program to connect to the Arduino board, I had to set a softlink on the ACM module to some serial or USB module. For example,
ln -s /dev/ttyACM0 /dev/ttyS99
Otherwise it won't even show up in the list in the Arduino program. So what I did then was starting the serial monitor tool (Ctrl + Shift + M) and sent it my start sequence waiting for an answer. It worked without a problem there (it was binary data so I couldn't verify if it was correct, but I got an answer at least).
The next thing I did was trying to do the same with my program, so the Arduino was connected to the PC, the softlink was set correctly, the device listed along with two normal COM-ports I have in my PC in my program, and I tried to connect, but it didn't work.
No error, no nothing, simply no answer of the board. After I waited a couple of minutes, it still didn't do anything, so I disconnected it from the PC, and then I got an array of zero-bytes as the answer.
What could cause this kind of problems and how would I fix it?
Thanks for your help
Volker
PS: the program is a jar file with all libraries/dependencies included
EDIT:
Hardware is an Arduino UNO Board Model R3,and on windows i'm using Arduino 1.0 to program it
Your symlink to /dev/ttyACM0 is double dubious:
Disconnecting+reconnecting or powering off+on might change the number of the dev.
ttyACM usually refers to modems, not to plain USB-serial converters like that used by Arduino (at least those with the FT232 chip). I would expect something like ttyUSB0. For those you also would not need a symlink.
If you installed from the Ubuntu packages you're bound to have problems. I tried this first myself only to find that there are serious issues. I would recommend downloading the most recent version of the IDE arduino-1.0-src.tar.gz.
You'll notice it's the source, I think you'll like it better that way. Or you can get the repository from Git, you'll find that information plus how to build the IDE in Building Arduino, Steps for First Time Setup. Besides the fact that the Ubuntu distribution version is buggy, it's also like old, big time.

Is it possible to get the default gateway IP and MAC addresses in java?

I'm trying to find a way to access the IP and MAC addresses for the default gateway from a java applet to be run on a website, is this possible?
You can't do that kind of thing in Java, like getting the SSID on a Wifi network. This low-level hardware information can not be obtained in Java in a cross-platform way and Java only allow you to work in the Transport (TCP) level, aka use sockets.
If you need this kind of information, try invoking a OS command tool using Runtime.getRuntime().exec(...) and parsing the output. But you have to deal with different OS that means different commands tools and outputs.
But in the Applet world, you can't invoke OS command tools, nor opening files, you are in a sandbox that prevents Applets for doing some low-levels tasks that can put in risk the user computer.
If you sign your applet, you have privileges to perform same operations like regular desktop applications, but this is out of scope.
Try to focus you problem in another direction.
You can get the Mac Address of an interface in Java using the method getHardwareAddress in the class java.net.NetworkInterface (http://download.oracle.com/javase/6/docs/api/java/net/NetworkInterface.html#getHardwareAddress()). I am not sure if that will work inside an Applet, and I have never tested that method myself, but if something does it, this method is that thing.
The IP Address? sure thing. Just have the applet connect back to your server. An applet can open an http connection to the server that hosts it.
The MAC and Default GW are system specific things you could get via external programs, but I'm not 100% sure those are supported in the applet sandbox (in fact, they're probably not supported).

Categories

Resources