cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Did you know you can set a signature that will be added to all your posts? Set it here! X

Communication TCP-IP Scale-Thingworx for weighing values

Ziyad
8-Gravel

Communication TCP-IP Scale-Thingworx for weighing values

Hello Everybody 

 

I'm trying to get weight data from a scale that I connected to a network few weeks ago and I want to know if it is possible to communicate with the scale using a TCP-IP socket connection by using only thingworx.

I recently knew recently that kepware could help for this operation and it could be more efficient but unfortunately we have a licence probleme so I need to use only thingworx.

 

More details about the scale : 

The scale is a Mettler Toledo Viper, IP adress : 10.18.200.51    and     Port: 9761

to get the weight from the scale we should send a request called "SI" and then we get an answer like ***SS**258**g

The communication should enable me to extract the weight and display it on a mashup by clicking a button "Peser"

 

Thank you in advance for you Help

1 ACCEPTED SOLUTION

Accepted Solutions

@Ziyad, again few options are available. For example:

 

  1. (for simple use cases) You can expose your code via a Resource with a "static" service, so that you use it like that:
    var weight = Resources["MTViper"].GetImmediateReading({ host: "10.18.200.51", port: 9761 });​

     

  2. (slightly more advanced) You can create a thing template instead of Resource (hostname and port are configured in thing's Configuration):
    var weight = Things["MTViper-001"].GetImmediateReading();​
  3. (probably the most advanced) The same as (2), but using RemoteThing as your base template. This will help you to avoid reconnecting for each reading, handle isConnected(), etc. So that your thing behaves like a proper remote thing:
    var weight = Things["MTViper-001"].weight;​

 

As you can imagine, (3) is the most complex and (1) is the easiest to implement. Let's consider the latter. Firstly, you need to get a copy of ThingWorx Extension SDK (make sure its version corresponds to the one of your platform). Then you'll need to create a Java class corresponding to your Resource:

package com.example;
// Imports

public class MTViperResource extends Resource {

    @ThingworxServiceDefinition(name = "GetImmediateReading")
    @ThingworxServiceResult(name = "result", baseType = "NUMBER")
    public Double GetImmediateReading(
        @ThingworxServiceParameter(name = "host", baseType = "STRING") String host, 
        @ThingworxServiceParameter(name = "port", baseType = "INTEGER") Integer port
    ) {
        // ...do your TCP/IP exchange...
        return 247.0;
    }

}

 

 ...and a metadata.xml, which describes the extension:

<Entities>
	<ExtensionPackages>
		<ExtensionPackage name="MTViperExtension" vendor="My Company" packageVersion="0.1.0" minimumThingWorxVersion="6.6.0" />
	</ExtensionPackages>
	<Resources>
		<Resource name="MTViper" className="com.example.MTViperResource">
			<JarResources>
				<FileResource type="JAR" file="mtviper.jar" />
			</JarResources>
		</Resource>
	</Resources>
</Entities>


Compile your Java classes into mtviper.jar and zip it altogether like this:

 

mtviper-extension.zip

 - metadata.xml

 - lib/

 - - common/

 - - - mtviper.jar

 

Import this extension in ThingWorx and use it like I mentioned at the beginning of this post. If you need to update your code -- iterate the version in metadata.xml, re-import and restart ThingWorx. You'll find more details in ThingWorx Help and SDK package.

 

Regards,
Constantine

View solution in original post

5 REPLIES 5

Hello @Ziyad,

 

This is definitely possible, but requires some work to be done. Basically, you'll need to implement the communication protocol in Java (the complex part) and wrap it into a ThingWorx extension to access it from your mashups (the easy part).

 

The main question is how to implement it in Java, and for this you have a multitude of options, for example:

  1. Java sockets + naive implementation of the protocol (if all you need is just a couple of those commands). Here's a good starting point: https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html
  2. If you need to support a rich set of commands, then you can do (1), but implementing MT-SICS declaratively, e.g. using protobuf or Apache Thrift. I found one protobuf definition on GitHub, don't know how fresh or relevant it is: https://github.com/mp49/mettlerToledo/blob/master/protocol/mt.proto
  3. Netty -- if you need to support hundreds and thousands of those scales at the same time;

Regards,

Constantine

Thank you Constantine for you answers and recommandations

 

Actually I did the difficult part in java by using sockets and I've got the weighted value from the scale, here is the code that I tested 

 

import java.io.*;
import java.net.Socket;

public class Connexion {

public static void main(String[] args) {

//Connection parameters // Numéro de port + adress du host
String hostname = "10.18.200.51"; //todo: Modify to match your device's address!
int port = 9761;

System.out.println("Connecting to " + hostname + ":" + port);
try (Socket socket = new Socket(hostname,port)) {
try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true))
{
out.println("Connected!");
try {
for (int i = 0; i < 10; i++) {
out.println("SI"); // Request weight IP = Immediate Print
out.println("Envoie de la deuxième trame");
out.println("Envoie de la troisième trame");
String message = in.readLine(); // Read response from scale
System.out.println(message);
Thread.sleep(500); // Wait
}
} catch (InterruptedException e) { //Required due to Thread.sleep
e.printStackTrace();
}
}
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}

I would be grateful if you can describe to me the steps to integrate the code with thingworx, Thank you !!

@Ziyad, again few options are available. For example:

 

  1. (for simple use cases) You can expose your code via a Resource with a "static" service, so that you use it like that:
    var weight = Resources["MTViper"].GetImmediateReading({ host: "10.18.200.51", port: 9761 });​

     

  2. (slightly more advanced) You can create a thing template instead of Resource (hostname and port are configured in thing's Configuration):
    var weight = Things["MTViper-001"].GetImmediateReading();​
  3. (probably the most advanced) The same as (2), but using RemoteThing as your base template. This will help you to avoid reconnecting for each reading, handle isConnected(), etc. So that your thing behaves like a proper remote thing:
    var weight = Things["MTViper-001"].weight;​

 

As you can imagine, (3) is the most complex and (1) is the easiest to implement. Let's consider the latter. Firstly, you need to get a copy of ThingWorx Extension SDK (make sure its version corresponds to the one of your platform). Then you'll need to create a Java class corresponding to your Resource:

package com.example;
// Imports

public class MTViperResource extends Resource {

    @ThingworxServiceDefinition(name = "GetImmediateReading")
    @ThingworxServiceResult(name = "result", baseType = "NUMBER")
    public Double GetImmediateReading(
        @ThingworxServiceParameter(name = "host", baseType = "STRING") String host, 
        @ThingworxServiceParameter(name = "port", baseType = "INTEGER") Integer port
    ) {
        // ...do your TCP/IP exchange...
        return 247.0;
    }

}

 

 ...and a metadata.xml, which describes the extension:

<Entities>
	<ExtensionPackages>
		<ExtensionPackage name="MTViperExtension" vendor="My Company" packageVersion="0.1.0" minimumThingWorxVersion="6.6.0" />
	</ExtensionPackages>
	<Resources>
		<Resource name="MTViper" className="com.example.MTViperResource">
			<JarResources>
				<FileResource type="JAR" file="mtviper.jar" />
			</JarResources>
		</Resource>
	</Resources>
</Entities>


Compile your Java classes into mtviper.jar and zip it altogether like this:

 

mtviper-extension.zip

 - metadata.xml

 - lib/

 - - common/

 - - - mtviper.jar

 

Import this extension in ThingWorx and use it like I mentioned at the beginning of this post. If you need to update your code -- iterate the version in metadata.xml, re-import and restart ThingWorx. You'll find more details in ThingWorx Help and SDK package.

 

Regards,
Constantine

One thing I forgot to mention -- there's an Eclipse plugin for creating ThingWorx extensions, you can find it in the PTC Marketplace, it can help you to automate 80% of what I described in the previous post. But even if you use it, that's what happens behind the scene.

 

/ Constantine

slangley
23-Emerald II
(To:Ziyad)

Hi @Ziyad.

 

If one of the previous responses answered your questions, please mark the appropriate one for the benefit of others on the community.

 

Regards.

 

--Sharon

Top Tags