Java Serial Communication PortInUseException - java

I have an SMS-based Java application using CommV3 drivers for Serial communication to a GSM Modem. I have two separate codes, one for sending messages and one for receiving messages. Each code works perfectly when executed individually.
Now, when I want to integrate both these codes, I get a PortInUseException, which might be obvious. But, I am not sure where to start from.
Could I get pointers/ links/ tutorials where I could start resolving my issue. I do not have a clue where I should start from.
Thanks in advance!

Make sure that your code uses one SerialPort and not two. Then there is no problem reading or writing to this port.
SerialPort serialPort;
InputStream inStream;
OutputStream outStream;
serialPort = (SerialPort) portId.open(...);
serialPort.setFlowControlMode(...);
serialPort.setSerialPortParams(...);
inStream = serialPort.getInputStream ();
outStream = serialPort.getOutputStream ();
PS: SMSLib is an excellent Java library (ported to .NET Framework as well) which allows you to send/receive SMS messages via a compatible GSM modem or GSM phone. SMSLib also supports some bulk SMS operators. It is free and very stable.

Probably you can separate out the listener code (which binds to a port) from 2 codes .
and dedicate to the appropriate code segment based on send / recieve.

Related

How to get data from RS-485 connected by USB

I need to read data from some Data Aggregator Device over RS-485 interface by USB port (USB-to-RS-485 converter used) for the Desktop App on JavaFX, so I have stacked on checking connection and getting proper values.
Data Aggregator Device collects information about produced current, power etc. from PV-module (Solar Energy). This device has the only RS-485 port. As I read from a documentation, it uses Modbus RTU protocol (with settings: 9600-8N1).
To be honest, It's my first experience with working on COM-ports and Devices. That is why I don't know even am I correctly connect pins. I read a lot of things related with RS-485, Modbus. But, still, I am on the first steps.
So, I use simple UTP cable to connect. And what I have done:
This is how I connect USB-to-RS485 converter
And this is
about Data Aggregator Device
Windows found virtual "COM3" port, after connecting USB to PC. Then I try to check connection with Terminal 1.9 by Bray. I try to send something. And as receiving messages it sent me some data also. However, it doesn't mean that everything connected well. Also terminal shows me some FRAME ERROR.
I know, that Modbus protocol based on "master-slave" scheme. Therefore, PC as a client is "Master" and device as a server is "Slave". I have to send some request to the slave to get some expected response. But, how?!
Please, check my connections! I don't know what to do next.
RS-485 is differential and requires just two wires, you can safely remove the ground wire (GND). Then, connect the wires to R+ and R- on your USB-to-RS485 converter
Done that, may I ask you how you tried to send something? Modbus requires a final byte, the CRC which is hard to determine by hand. Did you create a valid modbus packet?
A valid modbus packet requires a receiver address, the payload length, the data itself and the final CRC
I don't know about java, but if you do have specification of devices protocol, you can surely use terminal program to send message to modbus and read messages back. That way you will have protocol tested and it shouldn't be to hard to make custom software later. But until you can send message and get back meaningful message using terminal software I recommend you to stick to it.
This youtube video will give basic knowledge needed about modbus RTU packet format: https://www.youtube.com/watch?v=OvRD2UvrHjE
The most difficult part is calculating CRC every time. But luckily you can download Docklight terminal. It actually has an option to calculate CRC automatically for modbus and add it at last positions of the packet.
You can download free version at their homepage.
docklight.de
It is very simple to use. Same as Bray terminal you used.. And here is example of how to set up modbus CRC calculation for every package.
https://docklight.de/manual/sendingmodbuscommandswithc.htm
I know they also have great support.
Once you successfully send message to device and get answer, things will get much easier because you will understand how protocol actually works. Modbus RTU is very mature protocol, but it is still used because many existing systems use it and it is very simple to add it to some device. All device needs is Serial port (UART) to connect to modbus. So it is not that hard to understand it and use it. Just check out explanation video and terminal specifically adopted for modbus that I gave links in this message.

Java BlueCove - Sending information from server to client

I'm writing a bluetooth related application, and I'm using a API called BlueCove if you're familiar with it.
I managed to send some text from the client to a server, however I'm not familiar with the API for sending information from a server to a client so I couldn't send any information back to the client. I want to know how to do that.
Could anyone point me to it? I'm really unfamiliar with the API. Thanks
Turns out to be a really simple thing. Bluetooth provides different ways to communicate between devices, and one of them is using DataStream. Set up the following on both server and client side, and they'll be able to talk to each other:
StreamConnection conn = (StreamConnection)Connector.open(url);
DataOutputStream output= new DataOutputStream(conn.openOutputStream());
DataInputStream input = new DataInputStream(conn.openInputStream());
Whatever is put in DataOutputStream on one end, it'll come out in DataInputStream on the other side of the connection, regardless whether it's a server/client. DataInputStream and DataOutputSream's API are found in the link.

send file to android device

I need to send a few strings from a java project (Windows or Ubuntu) to an Android device via WiFi means. I found a few sources on how to send files from Android device to Android device but I didn't find anything cross platform. Can someone suggest me a way to do it, or a lead about where to look ?
I am open to alternate methods, but as always the simplest the better.
Thanks in advance.
I would write a simple REST web service for the android client to check. That way if you wanted to for whatever reason you could move it to the cloud and do it remotely. That is probably the simplest way I can think of as well.
I would suggest send it through TCP connection.
Because Both Java and Android have ServerSocket class and Socket class.
You can set your computer to be host by using ServerSocket and your Android device to be client by using Socket.
Then after connection, you can use getInputStream() and getOutputStream() methods of Socket to transfer data, including Strings, Objects and even Files with suitable combination of FileInputStream,ObjectOutputStream etc.

Exchanging Files Client-Server architecture JAVA

I am trying to send an image from the android phone, process it on the server side, and then get it back to the phone. I have been able to send the file from the phone to the server but then it seems that the server cannot send the image using the same socket. I am using bufferedinputstream and bufferedoutputsteram. is it possible or would i need 2 different ports? code is in java.
It is possible to have a 2-way-communication by using one socket connection.
In easy words: the server's outputstream is the client's inputstream and vice versa.

Client-Server Networking Between PHP Client and Java Server

I have a university project which is already 99% completed. It consists of two parts-website (PHP) and desktop (Java).
People have their accounts on the website and they wish to query different information regarding their accounts. They send an SMS which is received by desktop application which queries database of website (MySQL) and sends the reply accordingly. This part is working superbly. The problem is that some times website wishes to instruct the desktop application to send a specific SMS to a particular number. Apparently there seems no way other than putting all the load to the DB server... This is how I made it work. Website puts SMS jobs in a specific table. Java application polls this table again and again and if it finds a job, it executes it. Even this part is working correctly but unfortunately it is not acceptable by my university to poll the DB like this. :(
The other approach I could think of is to use client-server one. I tried making Java server and its PHP client. So that whenever an SMS is to be sent, the website opens a socket connection to desktop application and sends two strings (cell # and SMS message). Unfortunately I am unable to do this. I was successfully to make a Java server which works fine when connected by a Java client, similarly my PHP client connects correctly to a PHP server, but when I try to cross them, they start hating each other... PHP shows no error but Java gives StreamCorruptedException when it tries to read header of input stream.
Could someone please tell what I can try to make PHP client and Java server work together? Or if the said purpose can be achieved by another means, how?
Regards,
Yasir
Wait... are you using object streams? According to the java documentation StreamCorruptedException is "Thrown when control information that was read from an object stream violates internal consistency checks." I doubt your PHP app is sending what Java considers a serialized object. Why don't you go low-tech and read a string? The following had worked for me back in the day:
ServerSocket serverSocket = new ServerSocket(port);
Socket clientSocket = serverSocket.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while((inputLine = in.readLine())!=null)
{
//Do whatever
}
You might try looking into Quercus. It's a server that runs PHP inside java. You can call java called directly from PHP as if it was native PHP functions. You won't have to worry about streams then.

Categories

Resources