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.
Related
THE CASE:
I have a TCP client what send a data and I want to hear the data from my server. So, here, everything okay.
I decided make an socket server on port 9876 to listen this device (using Java 8). The data which I receive is weird, when I print it is like: "慳慳慳", but in theory, the data coming should be a hex. This is an example of the package what the device sends (login):
40407F000431303031313132353239393837000000000000001001C1F06952FDF069529C91110000000000698300000
C0000000000036401014C00030001190A0D04121A1480D60488C5721800000000AF4944445F3231364730325F53205
6312E322E31004944445F3231364730325F482056312E322E31000000DF640D0A
I want to replicate in a easy way (one class if its possible) the connection by castel to the device (port 5086 for Traccar server app) in almost 6 methods:
Connect
Disconnect
Encode
Decode
Send package
Receive package
My goal is replicate this behavior with PHP. I tried with Websocket/HTTP protocols and the result is that I can't connect with it. I cant hear any package from the device.
So, other question could be: how I can code my own custom protocol (Castel) to connect server with these devices?
I reviewed the Traccar Server code, but its very extensive. I proved the device with it and it works, but I can't understand how (at the code level).
Documentation
Castel communication flow
Protocol package format for download upload
Login package example
I did a simple socket with PHP and this is what I listened
Traccar server includes a lot of different things, but you can just take the protocol parsing part if you want. Here's the decoder for the Castel protocol:
github.com/traccar/.../CastelProtocolDecoder.java
The decoder is based on Netty, so if you want to use it, you need to implement the rest of the server to listen to incoming connections. I would recommend checking Netty guide for more details on how to do it:
https://netty.io/wiki/user-guide-for-4.x.html
I want to build a sniffer in Android and one of the approaches that was proposed was a loop-back VPN.
The issue with this approach is that all the communication with the outside world will be done through a protected socket and in Java I still cannot sniff low level TCP information from that socket.
For example, I cannot find out when exactly an ACK was received for data that was sent from the phone. Am I missing something or is this approach simply not suitable for a proper sniffer?
You won't be able to get ack information and other low level info using an Android VPN as this data won't be exposed through Java sockets.
An alternative is to utilize a native sniffer (TCPdump is one example, and an Android port already exists), execute it as root from your Java app and then retrieve its output in your app
I would like to manage a connection between 2 applications, they will run on diffrent phones.
The purpose doesn't really matters let's say that it would be some kind of a chat.
How can I create this connection between the applications? I know how to do it on pc java programs using the socket and the serverSocket classes but I guess it's diffrent in here since the phone might change his ip when he moves between networks.
So how would you do that? how would you create a connection between 2 applications/phones
Given the scenario you described in your comment, using Push Notification i.e Google Cloud Messaging (GCM) might be sufficient.
So, when program B wants update from program A, it will ask the server to push a notification (via GCM) to program A. Note that program A will not connect to the server every x time to check for updates.
Another out-of-the-box solution is to use SMS if the communication is not so frequent and messages are short. Here I mean SMS that will be processed directly by your app (given it has certain attention word) and will not go to the inbox i.e. no SMS notification on the phone
Since the two phones will, as you note, have changing IP addresses, you'll probably need to use an intermediary.
The general design pattern here would be to have a server with a fixed IP or DNS address that relays messages between your two mobile devices. The simplest implementation would be a webserver, to which each phone would connect via HTTP(S) to transmit data or poll for updates.
There are a number of backend-as-a-service platforms that provide this type of functionality pre-built, and would likely suffice for something like a chat system. Check out Cloudmine and Parse.
If you need a low-latency or near-realtime connection, you could also consider implementing your own server that uses the Android C2DM (Cloud to Device Messaging) service.
See Android's Sample Bluetooth Chat App, It will give you an idea for how it can be achieved.
I started a project that needs using network level packets such as IP/ICMP/UDP/TCP packets.
There is two main approach to handle it: Raw sockets and Winpcap/libpcap.
I know pcap installs a driver on OS and allows programmer to capture and send packets. On the other hand there is raw sockets which have some limitations in Windows 7 or above.
The project needs sending some IP/ICMP/UDP/TCP packets to a router and analyzes the responses, such as IP-Identifier, TTL, ... . Also I want it works in Linux and Windows.
Can you list a comparison about these two approach?
If you want the code to be portable, then you can't use the raw socket API (which is rather different on Linux and Windows). Winpcap is generally compatible with libpcap, and the pcap API is generally reasonable, considering what it's doing.
in your situation, RAW sockets will work but you have to do something like
sock_raw_tcp = socket(AF_INET , SOCK_RAW , IPPROTO_TCP);
sock_raw_udp = socket(AF_INET , SOCK_RAW , IPPROTO_UDP);
sock_raw_icmp = socket(AF_INET , SOCK_RAW , IPPROTO_ICMP);
You dont have an option like IP_PROTO_IP. Now, with RAW sockets, you will get only IP headers + transport level headers but not ethernet headers. So, if you are only interested in application layer data and want to use IP header for Ipaddress & TTL and transport header for port numbers etc, then its OK. Keep in mind that for TCP you might have to do check sums and reassembly also. Some checksums will also be required for UDP.
However, winpcap solves many management issues for you since it uses a device driver to connect your NIC's data link layer OR layer 2. Here you will also get an ethernet frame and wont have to open different types of RAW sockets. You still will have to apply the application related logic of dealing with packets as you would do on the network layer (Layer 3).
Using Bluetooth I wish to communicate between a Windows PC and a mobile phone. The PC will probably have Bluecove while the mobile comes with JSR-82.
Do I have to implement CRC and other error checking procedures in my software related to Bluetooth transmission and reception or does the underlying Stack and hardware takes care of those details?
For instance if I send a message to another device over RFCOMM/SPP I need to be certain whether the message reached the device and whether it received exactly what I sent.
How can this be implemented or does this have to be checked in software?
RFCOMM is supposed to provide a reliable connection like tcp.
Data should arrive in the order it was sent, without corruption, or not at all.