-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialPortFromFile.pde
123 lines (110 loc) · 2.5 KB
/
SerialPortFromFile.pde
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
import processing.serial.*;
// serial object
Serial mySerial = null;
// configuration file
final String configFile = "data/PortConfig.txt";
// baudrate
final int baudrate = 115200;
// serial port selected by user; one can set this to a default (e.g. COM8)
String selectedPort = "";
// the end maker
final char endMarker = '\n';
// data received on serial port
String receivedData = null;
// message to display
String displayMessage= "";
void setup()
{
size(350, 400);
// possible Processing bug
// when the application starts, it does not always react on key presses
// and one has to manucally click in the application to give it the focus
// workaround that might solve it found at https://discourse.processing.org/t/keypressed-only-works-sometimes/22340/
surface.setVisible(true);
// if read port from file succeeded
if (readPortFromFile() == true)
{
// if the specified port exists
if (chkPortExists() == true)
{
try
{
mySerial = new Serial(this, selectedPort, baudrate);
displayMessage = "Port " + selectedPort + "' is open";
}
catch(RuntimeException rtex)
{
displayMessage = rtex.getMessage();
}
}
}
}
void draw()
{
background(0xFFFFF1C4);
textSize(16);
textAlign(CENTER, CENTER);
if (mySerial == null)
{
fill(0xFF800000);
} //
else
{
fill(0xFF008000);
}
text(displayMessage, width/2, height / 2);
}
/*
Retrieve port-to-use from file
Returns:
true on successfu read, else false
*/
boolean readPortFromFile()
{
boolean success = true;
try
{
BufferedReader reader = createReader(configFile);
// if the file was not opened
if (reader == null)
{
displayMessage = "Could not open file '" + configFile + "'";
success = false;
} //
else
{
selectedPort = reader.readLine();
}
}
catch (Exception ex)
{
displayMessage = ex.getMessage();
success = false;
}
return success;
}
/*
Check if selected port exists
Returns:
true if it exists, else false
*/
boolean chkPortExists()
{
boolean portExists = false;
// loop through the list of ports
for (int pCnt = 0; pCnt < Serial.list().length; pCnt++)
{
// if specified port exists in the list
if (Serial.list()[pCnt].equals(selectedPort) == true)
{
portExists = true;
break;
}
}
// if the specified port does not exists in the list
if (portExists == false)
{
displayMessage = "Serial port '" + selectedPort + "' does not exist";
}
return portExists;
}