今回、Processing 3での、シリアル通信を容易に設定するために、SerialSelectorクラスを作成した。
Processing 3での作動や実行型ファイルへの変換(Application Export)でハングしてしまうControlP5ライブラリの使用は避けて、Java SwingでGUIは構築した。setup()関数内で以下のように、SerialSelectorクラスのインスタンスを生成して、通信条件の初期値をクラス内のインスタンス変数に設定していく。
JSerialSelector.pde
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void setup(){ .................. selector = new JSerialSelector(); selector.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); selector.combo_port.setSelectedIndex(1); selector.combo_baud_rate.setSelectedItem("19200"); selector.combo_parity.setSelectedItem('N'); selector.combo_data_bits.setSelectedItem("8"); selector.combo_stop_bits.setSelectedItem("1"); selector.setBounds(10, 10, 440, 550); selector.setTitle("Serial Port & Mode"); .................. } |
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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
import javax.swing.*; import java.awt.Dimension; import java.awt.BorderLayout; import java.awt.event.*; import processing.serial.*; Serial myPort; int stop_flag = 0; int start_flag = 0; String selected_port; public class JSerialSelector extends JFrame implements ActionListener { JComboBox combo_port; JComboBox combo_baud_rate; JComboBox combo_parity; JComboBox combo_data_bits; JComboBox combo_stop_bits; JButton button_connect; JButton button_stop; JLabel label; JTextArea textarea; ButtonGroup group; JRadioButton radio1; JRadioButton radio2; JRadioButton radio3; JRadioButton radio4; JRadioButton radio5; JRadioButton radio6; String[] portName; int port_index; int baud_rate_index; int parity_index; int data_bits_index; int stop_bits_index; String[] ports; int[] baud_rate = {9600, 14400, 19200, 38400, 57600}; char[] parity = {'N', 'E', 'O', 'M', 'S'}; int[] data_bits = {8, 7, 6, 5}; int[] stop_bits = {1, 2}; public JSerialSelector() { String default_baud_rate = "19200"; String default_parity = "N"; String default_data_bits = "8"; String default_stop_bits = "1"; portName = new String[Serial.list().length]; for (int i = 0; i< Serial.list().length; i++) { portName[i] = Serial.list()[i]; println("port["+i+"]=:", portName[i]); } String[] portNames = {}; ; portNames = (String[])append(portNames, ""); portNames = concat(portNames, Serial.list()); ports = portNames; String[] dl_ports = ports; String[] dl_baud_rate = {"9600", "14400", "19200", "38400", "57600"}; String[] dl_parity = {"N", "E", "O", "M", "S"}; String[] dl_data_bits = {"8", "7", "6", "5"}; String[] dl_stop_bits = {"1", "2"}; for (int i=0; i<baud_rate.length; i++) { if (dl_baud_rate[i]==default_baud_rate) { baud_rate_index = i; break; } } for (int i=0; i<parity.length; i++) { if (dl_parity[i]==default_parity) { parity_index = i; break; } } for (int i=0; i<data_bits.length; i++) { if (dl_data_bits[i]==default_data_bits) { data_bits_index = i; break; } } for (int i=0; i<stop_bits.length; i++) { if (dl_stop_bits[i]==default_stop_bits) { stop_bits_index = i; break; } } button_stop = new JButton("Exit"); button_connect = new JButton("Connect"); combo_port = new JComboBox(dl_ports); combo_port.setPreferredSize(new Dimension(200, 30)); combo_baud_rate = new JComboBox(dl_baud_rate); combo_baud_rate.setPreferredSize(new Dimension(120, 30)); combo_parity = new JComboBox(dl_parity); combo_parity.setPreferredSize(new Dimension(60, 30)); combo_data_bits = new JComboBox(dl_data_bits); combo_data_bits.setPreferredSize(new Dimension(60, 30)); combo_stop_bits = new JComboBox(dl_stop_bits); combo_stop_bits.setPreferredSize(new Dimension(60, 30)); textarea = new JTextArea(); textarea.setLineWrap(true); JScrollPane scrollpane = new JScrollPane(textarea); scrollpane.setPreferredSize(new Dimension(480, 300)); radio1 = new JRadioButton("black"); radio2 = new JRadioButton("green"); radio3 = new JRadioButton("purple"); radio4 = new JRadioButton("blue"); radio5 = new JRadioButton("orange"); radio6 = new JRadioButton("red"); group = new ButtonGroup(); group.add(radio1); group.add(radio2); group.add(radio3); group.add(radio4); group.add(radio5); group.add(radio6); combo_port.addActionListener(this); combo_baud_rate.addActionListener(this); combo_parity.addActionListener(this); combo_data_bits.addActionListener(this); combo_stop_bits.addActionListener(this); button_connect.addActionListener(this); button_stop.addActionListener(this); radio1.addActionListener(this); radio2.addActionListener(this); radio3.addActionListener(this); radio4.addActionListener(this); radio5.addActionListener(this); radio6.addActionListener(this); JPanel p = new JPanel(); p.add(radio1); p.add(radio2); p.add(radio3); p.add(radio4); p.add(radio5); p.add(radio6); p.add(new JLabel("Com Port:")); p.add(combo_port); p.add(new JLabel("baud rate:")); p.add(combo_baud_rate); p.add(new JLabel("parity:")); p.add(combo_parity); p.add(new JLabel("data bits:")); p.add(combo_data_bits); p.add(new JLabel("stop bits:")); p.add(combo_stop_bits); p.add(button_connect); p.add(scrollpane); p.add(button_stop); label = new JLabel(); JPanel labelPanel = new JPanel(); labelPanel.add(label); getContentPane().add(p, BorderLayout.CENTER); getContentPane().add(labelPanel, BorderLayout.PAGE_END); } public String getPort(int i) { return ports[i]; } public int getBaudRate(int i) { return baud_rate[i]; } public char getParity(int i) { return parity[i]; } public int getDataBits(int i) { return data_bits[i]; } public int getStopBits(int i) { return stop_bits[i]; } public Serial getSerial() { return myPort; } public int getStart_Flag() { return start_flag; } public int getStop_Flag() { return stop_flag; } public int getChoice() { return choice; } public int getColor_code() { return color_code; } public void actionPerformed(ActionEvent e) { if (e.getSource()==button_stop) { println("Stop Button Pushed!"); stop_flag = 1; exit(); } else { if (e.getSource()== button_connect) { SelectedPort = selector.getPort(port_index); SelectedBaudRate = selector.getBaudRate(baud_rate_index); SelectedParity = selector.getParity(parity_index); SelectedDataBits = selector.getDataBits(data_bits_index); SelectedStopBits = selector.getStopBits(stop_bits_index); start_flag = 1; choice = 0; serial_connect(); } if (e.getSource() == radio1) { if (radio1.isSelected()) { color_code = 0; } } if (e.getSource() == radio2) { if (radio2.isSelected()) { color_code = 1; } } if (e.getSource() == radio3) { if (radio3.isSelected()) { color_code = 2; } } if (e.getSource() == radio4) { if (radio4.isSelected()) { color_code = 3; } } if (e.getSource() == radio5) { if (radio5.isSelected()) { color_code = 4; } } if (e.getSource() == radio6) { if (radio6.isSelected()) { color_code = 5; } } if (e.getSource()== combo_port) { selected_port = (String)combo_port.getSelectedItem(); port_index = (int)combo_port.getSelectedIndex(); SelectedPort = selector.getPort(port_index); } if (e.getSource()== combo_baud_rate) { SelectedBaudRate = selector.getBaudRate(baud_rate_index); } if (e.getSource()== combo_parity) { SelectedParity = selector.getParity(parity_index); } if (e.getSource()== combo_data_bits) { SelectedDataBits = selector.getDataBits(data_bits_index); } if (e.getSource()== combo_stop_bits) { SelectedStopBits = selector.getStopBits(stop_bits_index); } } } } void serial_connect() { myPort = new Serial(this, SelectedPort, SelectedBaudRate); println("Selected"); println("SelectedPort=", SelectedPort); println("SelectedBaudRate=", SelectedBaudRate); //port.clear(); } |