-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRs232-SerialPort.java
More file actions
165 lines (141 loc) · 5.75 KB
/
Rs232-SerialPort.java
File metadata and controls
165 lines (141 loc) · 5.75 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* #%L
* **********************************************************************
* ORGANIZATION : Pi4J
* PROJECT : Pi4J :: Java Examples
* FILENAME : SerialExample.java
*
* This file is part of the Pi4J project. More information about
* this project can be found here: http://www.pi4j.com/
* **********************************************************************
* %%
* Copyright (C) 2012 - 2018 Pi4J
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 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 Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-3.0.html>.
* #L%
*/
//https://github.com/Pi4J/pi4j/blob/master/pi4j-example/src/main/java/SerialExample.java
//http://pi4j.com/example/serial.html
import com.pi4j.io.serial.*;
import com.pi4j.util.CommandArgumentParser;
import com.pi4j.util.Console;
import java.io.IOException;
import java.util.Date;
/**
* This example code demonstrates how to perform serial communications using the
* Raspberry Pi.
*
* @author Robert Savage
*/
public class Rs232-SerialPort {
/**
* This example program supports the following optional command
* arguments/options: "--device (device-path)" [DEFAULT: /dev/ttyAMA0] "--baud
* (baud-rate)" [DEFAULT: 38400] "--data-bits (5|6|7|8)" [DEFAULT: 8] "--parity
* (none|odd|even)" [DEFAULT: none] "--stop-bits (1|2)" [DEFAULT: 1]
* "--flow-control (none|hardware|software)" [DEFAULT: none]
*
* @param args
* @throws InterruptedException
* @throws IOException
*/
public static void main(String args[]) throws InterruptedException, IOException {
// !! ATTENTION !!
// By default, the serial port is configured as a console port
// for interacting with the Linux OS shell. If you want to use
// the serial port in a software program, you must disable the
// OS from using this port.
//
// Please see this blog article for instructions on how to disable
// the OS console for this port:
// https://www.cube-controls.com/2015/11/02/disable-serial-port-terminal-output-on-raspbian/
// create Pi4J console wrapper/helper
// (This is a utility class to abstract some of the boilerplate code)
final Console console = new Console();
// print program title/header
console.title("<-- The Pi4J Project -->", "Serial Communication Example");
// allow for user to exit program using CTRL-C
console.promptForExit();
// create an instance of the serial communications class
final Serial serial = SerialFactory.createInstance();
// create and register the serial data listener
serial.addListener(new SerialDataEventListener() {
@Override
public void dataReceived(SerialDataEvent event) {
// NOTE! - It is extremely important to read the data received from the
// serial port. If it does not get read from the receive buffer, the
// buffer will continue to grow and consume memory.
// print out the data received to the console
try {
console.println("[HEX DATA] " + event.getHexByteString());
console.println("[ASCII DATA] " + event.getAsciiString());
} catch (IOException e) {
e.printStackTrace();
}
}
});
try {
// create serial config object
SerialConfig config = new SerialConfig();
// set default serial settings (device, baud rate, flow control, etc)
//
// by default, use the DEFAULT com port on the Raspberry Pi (exposed on GPIO
// header)
// NOTE: this utility method will determine the default serial port for the
// detected platform and board/model. For all Raspberry Pi models
// except the 3B, it will return "/dev/ttyAMA0". For Raspberry Pi
// model 3B may return "/dev/ttyS0" or "/dev/ttyAMA0" depending on
// environment configuration.
config.device(SerialPort.getDefaultPort()).baud(Baud._38400).dataBits(DataBits._8).parity(Parity.NONE)
.stopBits(StopBits._1).flowControl(FlowControl.NONE);
// parse optional command argument options to override the default serial
// settings.
if (args.length > 0) {
config = CommandArgumentParser.getSerialConfig(config, args);
}
// display connection details
console.box(" Connecting to: " + config.toString(),
" We are sending ASCII data on the serial port every 1 second.",
" Data received on serial port will be displayed below.");
// open the default serial device/port with the configuration settings
serial.open(config);
// continuous loop to keep the program running until the user terminates the
// program
while (console.isRunning()) {
try {
// write a formatted string to the serial transmit buffer
serial.write("CURRENT TIME: " + new Date().toString());
// write a individual bytes to the serial transmit buffer
serial.write((byte) 13);
serial.write((byte) 10);
// write a simple string to the serial transmit buffer
serial.write("Second Line");
// write a individual characters to the serial transmit buffer
serial.write('\r');
serial.write('\n');
// write a string terminating with CR+LF to the serial transmit buffer
serial.writeln("Third Line");
} catch (IllegalStateException ex) {
ex.printStackTrace();
}
// wait 1 second before continuing
Thread.sleep(1000);
}
} catch (IOException ex) {
console.println(" ==>> SERIAL SETUP FAILED : " + ex.getMessage());
return;
}
}
}