Processing連接串口的一些基本概念
基礎: https://processing.org/reference/libraries/serial/Serial.html基本步驟:
1) // 宣告
import processing.serial.*;
2)//設定會使用的串口名字
Serial myPort;
3) //指定要使用的串口
如是固定永不會變的已知串口(舉例如COM4),可直接指定:
myPort = new Serial(this, "COM4", 9600);
但通常在不同電腦和情況時,串口號都不會一樣,故程序是不可能寫死,要讓使用者按他們當時串口號來定,才是可行的辦法,即:
a) 使用串口列表來檢查當時的串口情況
printArray(Serial.list());
b) 再藉當時的串口列表情況指定到要使用的串口號。如:當時串口列表對應的是串口COM4、對應串口COM8、.....,則以下句來選用串口COM4:
myPort = new Serial(this, Serial.list(), 9600);
註:句中的指的就是串口列表中第一個串口,是指第二個,如此類推。串口列表的表示數字和順序,祇是按當時實際存在能用的串口作順序排列。舉列說:指的祇能說明是串口列表中的第一個,但具體是那一個中口,則祇能從當時的列表中看到並任用,這點要特別注意!
4) //使用
myPort.write(65); 本帖最后由 eddiewwm 于 2019-1-15 12:25 编辑
以上每次均要在運行前按當時情況而改寫程序,對非技術的使用者很不便,以下是在程序中讓使用者選擇的範例。
Processing 串口選用範例一
參考:https://create.arduino.cc/projec ... cilloscope&offset=5
import processing.serial.*;
Serial serial;
int serialBaudRate=115200;
int windowWidth, windowHeight;
boolean connected=false;
void setup() {
surface.setResizable(true);
}
void draw() {
if (!connected) {
background(0, 0, 0);
text("Select serial port:", 25, 25);
for (int i=0; i<Serial.list().length; i++)
text("F"+(i+1)+" - "+Serial.list(), 25, 80+i*20);
} else {
background(0, 0, 0);
text("Test Finished", 25, 25);
}
}
void keyPressed() {
if (key == CODED) {
if (!connected) {
serial = new Serial(this, Serial.list(), serialBaudRate);
connected=true;
}
}
} 本帖最后由 eddiewwm 于 2019-1-15 12:28 编辑
再進一步看一個在程序中運行,界面經過修飾的串口選用範例:
Processing串口選用範例二
http://startingelectronics.org/software/processing/serial-port-select/
import processing.serial.*;
Serial serial_port = null; // the serial port
// serial port buttons
Button btn_serial_up; // move up through the serial port list
Button btn_serial_dn; // move down through the serial port list
Button btn_serial_connect; // connect to the selected serial port
Button btn_serial_disconnect; // disconnect from the serial port
Button btn_serial_list_refresh; // refresh the serial port list
String serial_list; // list of serial ports
int serial_list_index = 0; // currently selected serial port
int num_serial_ports = 0; // number of serial ports in the list
void setup() {
// set the window size
size (640, 480);
// create the buttons
btn_serial_up = new Button("^", 140, 10, 40, 20);
btn_serial_dn = new Button("v", 140, 50, 40, 20);
btn_serial_connect = new Button("Connect", 190, 10, 100, 25);
btn_serial_disconnect = new Button("Disconnect", 190, 45, 100, 25);
btn_serial_list_refresh = new Button("Refresh", 190, 80, 100, 25);
// get the list of serial ports on the computer
serial_list = Serial.list();
//println(Serial.list());
//println(Serial.list().length);
// get the number of serial ports in the list
num_serial_ports = Serial.list().length;
}
void mousePressed() {
// up button clicked
if (btn_serial_up.MouseIsOver()) {
if (serial_list_index > 0) {
// move one position up in the list of serial ports
serial_list_index--;
serial_list = Serial.list();
}
}
// down button clicked
if (btn_serial_dn.MouseIsOver()) {
if (serial_list_index < (num_serial_ports - 1)) {
// move one position down in the list of serial ports
serial_list_index++;
serial_list = Serial.list();
}
}
// Connect button clicked
if (btn_serial_connect.MouseIsOver()) {
if (serial_port == null) {
// connect to the selected serial port
serial_port = new Serial(this, Serial.list(), 9600);
}
}
// Disconnect button clicked
if (btn_serial_disconnect.MouseIsOver()) {
if (serial_port != null) {
// disconnect from the serial port
serial_port.stop();
serial_port = null;
}
}
// Refresh button clicked
if (btn_serial_list_refresh.MouseIsOver()) {
// get the serial port list and length of the list
serial_list = Serial.list();
num_serial_ports = Serial.list().length;
}
}
void draw() {
// draw the buttons in the application window
btn_serial_up.Draw();
btn_serial_dn.Draw();
btn_serial_connect.Draw();
btn_serial_disconnect.Draw();
btn_serial_list_refresh.Draw();
// draw the text box containing the selected serial port
DrawTextBox("Select Port", serial_list, 10, 10, 120, 60);
}
// function for drawing a text box with title and contents
void DrawTextBox(String title, String str, int x, int y, int w, int h)
{
fill(255);
rect(x, y, w, h);
fill(0);
textAlign(LEFT);
textSize(14);
text(title, x + 10, y + 10, w - 20, 20);
textSize(12);
text(str, x + 10, y + 40, w - 20, h - 10);
}
// button class used for all buttons
class Button {
String label;
float x; // top left corner x position
float y; // top left corner y position
float w; // width of button
float h; // height of button
// constructor
Button(String labelB, float xpos, float ypos, float widthB, float heightB) {
label = labelB;
x = xpos;
y = ypos;
w = widthB;
h = heightB;
}
// draw the button in the window
void Draw() {
fill(218);
stroke(141);
rect(x, y, w, h, 10);
textAlign(CENTER, CENTER);
fill(0);
text(label, x + (w / 2), y + (h / 2));
}
// returns true if the mouse cursor is over the button
boolean MouseIsOver() {
if (mouseX > x && mouseX < (x + w) && mouseY > y && mouseY < (y + h)) {
return true;
}
return false;
}
}
另一個使用cp5界面和帶防止出錯的串口選用範例:
Processing串口選用範例三
https://stackoverflow.com/questions/29107544/serial-com-port-selection-by-dropdownlist-in-processing
import processing.serial.*;
import controlP5.*;
ControlP5 cp5;
DropdownList serialPortsList;
Serial serialPort;
final int BAUD_RATE = 9600;
String[] portNames;
void setup() {
size(700, 400,P3D);
//String[] portNames = Serial.list(); //changed to
portNames = Serial.list();
cp5 = new ControlP5(this);
// create a DropdownList
serialPortsList = cp5.addDropdownList("serial ports").setPosition(10, 10).setWidth(200);
for(int i = 0 ; i < portNames.length; i++) serialPortsList.addItem(portNames, i);
}
void controlEvent(ControlEvent theEvent) {
// DropdownList is of type ControlGroup.
// A controlEvent will be triggered from inside the ControlGroup class.
// therefore you need to check the originator of the Event with
// if (theEvent.isGroup())
// to avoid an error message thrown by controlP5.
if (theEvent.isGroup()) {
// check if the Event was triggered from a ControlGroup
println("event from group : "+theEvent.getGroup().getValue()+" from "+theEvent.getGroup());
//check if there's a serial port open already, if so, close it
if(serialPort != null){
serialPort.stop();
serialPort = null;
}
//open the selected core
//String portName = serialPortsList.getItem((int)theEvent.getValue()).toString(); //changed to
String portName =portNames.toString();
try{
serialPort = new Serial(this,portName,BAUD_RATE);
}catch(Exception e){
System.err.println("Error opening serial port " + portName);
e.printStackTrace();
}
}
else if (theEvent.isController()) {
println("event from controller : "+theEvent.getController().getValue()+" from "+theEvent.getController());
}
}
void draw() {
background(128);
}
页:
[1]