Community Tip - You can change your system assigned username to something more personal in your community settings. X
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
Solved! Go to Solution.
@Ziyad, again few options are available. For example:
var weight = Resources["MTViper"].GetImmediateReading({ host: "10.18.200.51", port: 9761 });
var weight = Things["MTViper-001"].GetImmediateReading();
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
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:
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:
var weight = Resources["MTViper"].GetImmediateReading({ host: "10.18.200.51", port: 9761 });
var weight = Things["MTViper-001"].GetImmediateReading();
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
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