-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathCreateUsbLink.java
73 lines (62 loc) · 3.2 KB
/
CreateUsbLink.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
Calimero 2 - A library for KNX network access
Copyright (c) 2016, 2024 B. Malinowsky
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import io.calimero.KNXException;
import io.calimero.link.KNXNetworkLink;
import io.calimero.link.KNXNetworkLinkUsb;
import io.calimero.link.LinkEvent;
import io.calimero.link.NetworkLinkListener;
import io.calimero.link.medium.TPSettings;
import io.calimero.serial.ConnectionStatus;
/**
* This example shows how to establish a client network link ({@link KNXNetworkLink}) to a KNX TP1 network using a KNX
* USB device. Minimum requirements are Calimero version 3.0-SNAPSHOT and Java SE 17 (java.base).
* <p>
* You can safely run this example; the established connection is closed 10 seconds after creation.
* No KNX messages are sent to the KNX network.
*
* @author B. Malinowsky
*/
public class CreateUsbLink
{
/**
* Specify your KNX USB device; you can either use the product or manufacturer name, or the USB vendor:product ID.
*/
private static final String device = "weinzierl";
public static void main(final String[] args)
{
System.out.println("This example establishes a KNX connection using the KNX USB device '" + device + "'");
// Create the USB-based link. The network link uses the KNX USB communication protocol. The second argument
// indicates that the KNX installation uses twisted-pair (TP) medium, with TP1 being most common.
try (KNXNetworkLink knxLink = new KNXNetworkLinkUsb(device, new TPSettings())) {
System.out.println("Connection established using KNX USB device " + knxLink.getName());
// Add a listener with a link event which notifies us in case the USB interface to KNX connection got disrupted.
// (Note, this is not the connection-state of the USB network link attached to this host itself.)
knxLink.addLinkListener(new NetworkLinkListener() {
@LinkEvent
void status(ConnectionStatus status) { System.out.println("KNX connection status: " + status); }
});
Thread.sleep(10_000);
}
catch (KNXException | InterruptedException e) {
// KNXException: all Calimero-specific checked exceptions are subtypes of KNXException
// InterruptedException: longer tasks that might block are interruptible, e.g., connection procedures. In
// such case, an instance of InterruptedException is thrown.
// If a task got interrupted, Calimero will clean up its internal state and resources accordingly.
// Any deviation of such behavior, e.g., where not feasible, is documented in the Calimero API.
System.out.println("Error creating USB network link: " + e);
}
}
}