You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|`lib-serial`|`pi4j-ktx-serial`| Serial communication DSL wrapping [jSerialComm](https://fazecast.github.io/jSerialComm/)|
13
20
14
21
## Example
15
22
@@ -26,32 +33,58 @@ console {
26
33
title("<-- The Pi4J Project -->", "Minimal Example project")
27
34
pi4j {
28
35
digitalInput(PIN_BUTTON) {
29
-
id("button")
30
-
name("Press button")
31
-
pull(PullResistance.PULL_DOWN)
32
-
debounce(3000L)
33
-
piGpioProvider()
34
-
}.onLow {
35
-
pressCount++
36
-
+"Button was pressed for the ${pressCount}th time"
37
-
}
36
+
id("button")
37
+
name("Press button")
38
+
pull(PullResistance.PULL_DOWN)
39
+
debounce(3000L)
40
+
}.onLow {
41
+
pressCount++
42
+
+"Button was pressed for the ${pressCount}th time"
38
43
}
39
-
44
+
40
45
digitalOutput(PIN_LED) {
41
-
id("led")
42
-
name("LED Flasher")
43
-
shutdown(DigitalState.LOW)
44
-
initial(DigitalState.LOW)
45
-
piGpioProvider()
46
-
}.run {
47
-
while (pressCount <5) {
48
-
+"LED ${state()}"
49
-
toggle()
50
-
sleep(500L/ (pressCount +1))
51
-
}
46
+
id("led")
47
+
name("LED Flasher")
48
+
shutdown(DigitalState.LOW)
49
+
initial(DigitalState.LOW)
50
+
}.run {
51
+
while (pressCount <5) {
52
+
+"LED ${state()}"
53
+
toggle()
54
+
sleep(500L/ (pressCount +1))
52
55
}
53
56
}
54
57
}
55
58
}
56
59
```
57
60
61
+
### Serial
62
+
63
+
Serial communication is provided by the `pi4j-ktx-serial` module, which wraps [jSerialComm](https://fazecast.github.io/jSerialComm/) (independent of Pi4J core since serial was removed in Pi4J 4.0.0).
64
+
65
+
**Gradle setup:**
66
+
67
+
```kotlin
68
+
dependencies {
69
+
implementation("com.pi4j:pi4j-ktx-serial:4.0.0")
70
+
implementation("com.fazecast:jSerialComm:2.11.0")
71
+
}
72
+
```
73
+
74
+
**Usage:**
75
+
76
+
```kotlin
77
+
importcom.pi4j.ktx.io.serial.serial
78
+
importcom.pi4j.ktx.io.serial.open
79
+
80
+
serial("/dev/ttyS0") {
81
+
baudRate =115200
82
+
dataBits =8
83
+
// stopBits, parity, flowControl also available
84
+
}.open {
85
+
// `this` is a jSerialComm SerialPort
86
+
// port is automatically closed when the block exits
87
+
outputStream.write("Hello\n".toByteArray())
88
+
val response = inputStream.bufferedReader().readLine()
0 commit comments