How do I listen in (preferably using Java) on a small set of well-defined CANopen messages without a commercial CANopen stack?
There is really nothing special about CANopen in this regard. If you are able to read CAN data from the CAN bus you should be OK. The CAN ID of the messages will identify them, and the interpretation of the data will be given in the data emitting device's documentation.
It's hard to give a generic answer on how to read the data, because everything depends on your hardware situation. However, before you can listen to any CAN messages, you need a CAN interface for your computer. The CAN interface's documentation will probably tell you how to use it.
Related
sorry if my english isn't perfect.
I'm trying to make an app and I need to exchange information between more devices.
I thought that could be a solution connect the devices on a server but I really don't have the idea where start.
What language I need to study to make this? There is a better solution?
This highly depends on what you are trying to achieve in the first place. It would be helpful if you could tell what you are trying to do, but I will still outline some general aspects:
You need to decide, what information is going to be exchanged and how this should happen
What information: Figure out, what exactly needs to be sent and received. Generic text messages? Images? Byte Streams?
How should this be done: Generally spoken, there are two approaches of getting information as a client: Polling and subscribing.
Polling: This approach means to periodically check an endpoint for new data. For example, HTTP uses this way: A web browser or any other client (REST-Client for example) periodically requests information from a HTTP-Server, using a connection just for this single request.
Subscribing / Sync / Notification: In some way or another, the client tells the server that it is interested in the information and wants to get notified when there is something new. The connection is initiated at the beginning and held open for further usage. The benefit of this approach is that changes are received immediately, but on the other hand a permanent connection needs to be maintained.
Things to study
At the beginning, get a good understanding of the TCP/IP Protocol, how Sockets work, how common Protocols do their job (e.g. HTTP, WebSockets)
Take a look at specific Protocols working on top of the basic ones
Tip: REST: Most common WebServices Protocol, providing a common way to exchange stateless data. Uses Polling.
WebSockets: Socket connection using Web Browsers. Commonly used to update information without needing to poll.
There is no specific language to learn for connections. It's more about understanding what the difficulties are and what ways have been invented to address this. Once you get to this point and know what you want to do, it's possible in every language.
Recommendation: As you seem to use Java/Android, I would try to use REST. A really great client-side library for REST on Android is Retrofit. For the server side use what fits for you .. common Java way would be to use Jersey, but you are free to choose from a lot of choices. If using Jersey is too hard for the beginning, maybe take a look at the JS/NodeJS world, those guys invented Express, which allows you to create a REST service out of just a database, wihtout having to code a lot.
First you need to decide if you want to go for an Android or an iOS application. There are other various mobile operating systems as well, but these are widely used . If you want to go for android which is most widely used in my opinion, then you need to learn Java. If you want to go for iOS application, then you need to learn swift or objectiveC. These languages provide the API to connect with various types of services such as Facebook, Firebase and Amazon etc. If you want to connect to some other local server who’s IP is known to you, then you can use socket programming to send messages.
There could be many ways you can implement this. One way will be using Web services. Of course REST might be a better option, if you follow this approach. You can implement Your service(server side code) with any language. I will recommend you use java since you are already using android.
Aside from this You might need to go through the basics of REST, its specifications and
some reference implementations for language of your preference.
In my java apps, I always comment my classes and methods to indicate what they are all about. The problem I often have however is documenting a complete process whereby the process is spread over several classes. I need to document how all the pieces work together to give an easy overview of how something works.
What I find problematic is WHERE I write this documentation. I could just as well write it in a separate file such as a Word document but the documentation tends to become alienated from the actual code and it is possible to get out of sync. On the other hand, if I document it in a single java code file, it makes it difficult for someone reading one of the dependent classes to know how that class fits into the overall process unless they are aware of the documentation in the class where I wrote it. A possible solution is just to include a reference in each class that makes up the entire process and just designate one of the classes as the "primary" class where the documentation originates from.
Or is there a better alternative approach on how I should be doing this?
EDIT:
An example is where you have a mobile app that uploads data to the server and then you download data from the server to another device. You have a process that covers three components (a sending mobile device, the server and the receiving mobile device). None of these can be really considered the "starting point in the overall data transfer process, so when documenting this process, where would this documentation go?
I'm developing a distributed application, and I need to connect a client Java based to a server C++ based. Both of them will need to send information to each other, but I need them to be able to do things while waiting for the information, and they don't know when they are gonna get new information, or send information.
How can I achieve this? Now I'm trying to implement a basic communication with Sockets, but I don't really get to communicate them. I have read that using sockets + threads is usually a good approach for client-server apps.
Could you please recommend me some web or book to read about this, or send me some example code to learn?
Do you think that i should use other approach, better than sockets? maybe a higher level library (i would need it for c++ and java) or a totally different way?
EDIT:
I will add some extra information.
What I would love to achieve is the following:
My C++ program has a main loop, where I would like to have a call like GetUpdatedDataFromRemoteDevice() where I read the new values of some numerical variables that previously got updated from the net (the socket, for example).
Eventually, the C++ program will need to send a message to the remote device, to tell him to send other kind of data, and after that, keep getting the updated values.
From the Java program (remote device) the application running is an interactive touchable screen, that cant get blocked by the network transmissions, because it must keep working for the user, so all the networking should be done in a separated thread.
That thread, should connect to the server, and when a button is pushed, start to send the data (4 changing numerical values) in a loop until another event happens.
It would be nice also to be easily re-connectable to the server.
ICE is a modern and good library for distributed applications:
many languages as C++ and Java
many platforms
GNU GPL
good performance
easy to use
First, you define the messages you want to exchange between server and client.
Then, you implement the C++ and Java source code to handle these messages.
More info at http://zeroc.com/ice.html
Have fun ;-)
EDIT: I have to use ACE in some projects. I can tell ACE is very old, maybe mature, but uses outdated C++ coding rules :-(
Therefore ACE is not as easy to use as STL or BOOST. Moreover, ACE is not really efficient... I prefer ICE ;-)
I don't know what your application is but robust client server socket programming is pretty hairy task to do properly. Hardware byte order, String encoding, Network errors, retries, duplicate messages, acks etc.. require lots of good design and careful programming. You need to get it work well as single-threaded before even thinking using multiple threads.
Unless you need instant notifications from server to client I suggest that you use HTTP as protocol between client and server. Client can poll server occasionally for new messages.
Anyway the problem has been solved multiple times already.
http://activemq.apache.org/
http://www.rabbitmq.com/devtools.html
http://www.cs.wustl.edu/~schmidt/ACE-overview.html
I did something of this sort once. In my case it was easier to connect my C++ app to a local Java app using JNI and then have the two Java apps talk to each other.
I'm in the process of developing a highly object-oriented solution, (i.e., I want as little coupling as possible, lots of reusability and modular code, good use of design patterns, clean code, etc). I am currently implementing the client-server aspect of the application, and I am new to it. I know how to use Sockets, and how to send streams and receive them. However, I am unsure of actually how to design my solution.
What patterns (if any) are there for TCP Java solutions? I will be sending lots of serialized objects over the network, how do I handle the different requests/objects? In fact, how do I handle a request itself? Do I wrap each object I'm sending inside another object, and then when the object arrives I parse it for a 'command/request', then handle the object contained within accordingly? It is this general design that I am struggling with.
All the tutorials online just seem to be bog-standard, echo servers, that send back the text the client sent. These are only useful when learning about actual sockets, but aren't useful when applying to a real situation. Lots of case statements and if statements just seems poor development. Any ideas? I'd much rather not use a framework at this stage.
Cheers,
Tim.
Consider using a higher level protocol then TCP/IP, don't reinvent the wheel. rmi is a good option and you should be able to find good tutorials on it.
I suggest you either use RMI, or look at it in details so you can determine how you would do things differently. At a minimum I suggest you play with RMI to see how it works before attempting to do it yourself.
If high performance and low latency aren't main requirements then just use existing solutions.
And if you decide to use rmi than consider using J2EE with EJB - it'll provide you a transaction management on top of rmi.
Otherwise if you need extremely low latency take a look on sources of existing solutions that use custom protocols on top of tcp.
For example OpenChord sends serialized Request and Response objects and Project Voldemort uses custom messages for its few operations.
Background
I am required to create a Java program on a laptop to receive/send CANopen messages.
RJ45 is chosen to be the network's physical medium. I am new to CANopen and Java communications programming.
Pardon me if I appear to be uninitiated. The truth is, I have been reading up a lot, but I still do not know how to get started.
Questions
Other than connecting a PC to the CANbus network, what else does the CAN-PC adapter do?
Is it possible to connect the laptop to the CANbus network without the CAN-PC adapter?
If a CAN-PC adapter is required, what sort of adapter should I use? PCMCIA, parallel, serial, usb, etc.?
How do I get started in writing the java program to listen/write CANopen messages?
What libraries should I use?
Do I need to create my own drivers?
Should my program handle heart-beat monitoring, error detection, etc.? Or are these taken care of by the CAN-PC adapter?
How do I retrieve specific information from a CANbus node?
How is the EDS file and object dictionary created? Does every node require them?
How do I simulate a CAN network to test my Java program without buying CAN hardware?
That's a lot of questions. I have never done any CAN related programming in Java, but let's see which questions I might answer anyway:
1) Other than connecting a PC to the
CANbus network, what else does the
CAN-PC adapter do?
It depends mainly on which CAN controller that is embedded in your adapter. But basically it just handles the low-level stuff like bus-arbitration, error-handling, retransmissions and message buffering.
2) Is it possible to connect the
laptop to the CANbus network without
the CAN-PC adapter?
No.
3) If a CAN-PC adapter is required,
what sort of adapter should I use?
PCMCIA, parallel, serial, usb, etc?
On a laptop? I would choose a USB interface. I have good experience with Kvaser's interfaces.
4) How do I get started in writing the
java program to listen/write CANopen
messages?
Depends on the API for your adapter. The API will most probably be C-based, so I would at least start out with some simple C test programs. The CAN-interface supplier probably have some good examples.
5) What libraries should I use?
Probably the one supplied with your CAN-interface, at least for the basic CAN part. For the CANopen part, there are some commercial CANopen stacks available and perhaps even some free. I doubt there are any written in Java.
You could also implement the necessary parts of the CANopen stack yourself if you are only doing simple communication.
6) Do I need to create my own drivers?
Generally no. Depends on your operating system and CAN-interface model. Select a CAN interface with drivers for your operating system.
7) Should my program handle heart-beat
monitoring / error detection / etc? Or
are these taken care of by the CAN-PC
adapter?
The CANopen stack should handle that on the CANopen level. The low level CAN bus error handling is taken care of by your interface.
8) How do I retrieve specific
information from a CANbus node?
I don't know what you mean here. PDOs or SDOs depending on what kind of information you want.
9) How is the EDS file and object
dictionary created? Does every node
require them?
You don't genereally need to create an EDS file, but might be useful for documentation purposes. The object-dictionary is implemented in software. Some parts of the OD are mandatory if making something standards-compliant.
10) How do I simulate a CAN network to
test my java program without buying
CAN hardware?
I wouldn't... A meaningful bus simulator would probably be more expensive to develop than to just bu a CAN adapter. Note that many CAN adapters contain dual interfaces, so you may do communication tests on a real CAN bus with little more hardware than just the adapter and a couple of termination resistors.