极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 21162|回复: 3

Processing連接串口的一些基本概念

[复制链接]
发表于 2019-1-15 11:33:06 | 显示全部楼层 |阅读模式
基礎: 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) 再藉當時的串口列表情況指定到要使用的串口號。如:當時串口列表[0]對應的是串口COM4、[1]對應串口COM8、.....,則以下句來選用串口COM4:
myPort = new Serial(this, Serial.list()[0], 9600);  
註:句中的[0]指的就是串口列表中第一個串口,[1]是指第二個,如此類推。串口列表的表示數字和順序,祇是按當時實際存在能用的串口作順序排列。舉列說:[0]指的祇能說明是串口列表中的第一個,但具體是那一個中口,則祇能從當時的列表中看到並任用,這點要特別注意!

4) //使用
myPort.write(65);
回复

使用道具 举报

 楼主| 发表于 2019-1-15 12:08:33 | 显示全部楼层
本帖最后由 eddiewwm 于 2019-1-15 12:25 编辑

以上每次均要在運行前按當時情況而改寫程序,對非技術的使用者很不便,以下是在程序中讓使用者選擇的範例。

Processing 串口選用範例一
參考:https://create.arduino.cc/projec ... cilloscope&offset=5

  1. import processing.serial.*;
  2. Serial serial;
  3. int serialBaudRate=115200;
  4. int windowWidth, windowHeight;
  5. boolean connected=false;

  6. void setup() {
  7.   surface.setResizable(true);
  8. }

  9. void draw() {
  10.   if (!connected) {
  11.     background(0, 0, 0);
  12.     text("Select serial port:", 25, 25);
  13.     for (int i=0; i<Serial.list().length; i++)
  14.       text("F"+(i+1)+" - "+Serial.list()[i], 25, 80+i*20);
  15.   } else {
  16.     background(0, 0, 0);
  17.     text("Test Finished", 25, 25);
  18.   }
  19. }

  20. void keyPressed() {  
  21.   if (key == CODED) {
  22.     if (!connected) {
  23.       serial = new Serial(this, Serial.list()[keyCode-112], serialBaudRate);
  24.       connected=true;
  25.     }
  26.   }
  27. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-15 12:27:17 | 显示全部楼层
本帖最后由 eddiewwm 于 2019-1-15 12:28 编辑

再進一步看一個在程序中運行,界面經過修飾的串口選用範例:

Processing串口選用範例二
http://startingelectronics.org/s ... serial-port-select/

  1. import processing.serial.*;

  2. Serial serial_port = null;        // the serial port

  3. // serial port buttons
  4. Button btn_serial_up;              // move up through the serial port list
  5. Button btn_serial_dn;              // move down through the serial port list
  6. Button btn_serial_connect;         // connect to the selected serial port
  7. Button btn_serial_disconnect;      // disconnect from the serial port
  8. Button btn_serial_list_refresh;    // refresh the serial port list
  9. String serial_list;                // list of serial ports
  10. int serial_list_index = 0;         // currently selected serial port
  11. int num_serial_ports = 0;          // number of serial ports in the list

  12. void setup() {
  13.   // set the window size
  14.   size (640, 480);
  15.   
  16.   // create the buttons
  17.   btn_serial_up = new Button("^", 140, 10, 40, 20);
  18.   btn_serial_dn = new Button("v", 140, 50, 40, 20);
  19.   btn_serial_connect = new Button("Connect", 190, 10, 100, 25);
  20.   btn_serial_disconnect = new Button("Disconnect", 190, 45, 100, 25);
  21.   btn_serial_list_refresh = new Button("Refresh", 190, 80, 100, 25);
  22.   
  23.   // get the list of serial ports on the computer
  24.   serial_list = Serial.list()[serial_list_index];
  25.   
  26.   //println(Serial.list());
  27.   //println(Serial.list().length);
  28.   
  29.   // get the number of serial ports in the list
  30.   num_serial_ports = Serial.list().length;
  31. }

  32. void mousePressed() {
  33.   // up button clicked
  34.   if (btn_serial_up.MouseIsOver()) {
  35.     if (serial_list_index > 0) {
  36.       // move one position up in the list of serial ports
  37.       serial_list_index--;
  38.       serial_list = Serial.list()[serial_list_index];
  39.     }
  40.   }
  41.   // down button clicked
  42.   if (btn_serial_dn.MouseIsOver()) {
  43.     if (serial_list_index < (num_serial_ports - 1)) {
  44.       // move one position down in the list of serial ports
  45.       serial_list_index++;
  46.       serial_list = Serial.list()[serial_list_index];
  47.     }
  48.   }
  49.   // Connect button clicked
  50.   if (btn_serial_connect.MouseIsOver()) {
  51.     if (serial_port == null) {
  52.       // connect to the selected serial port
  53.       serial_port = new Serial(this, Serial.list()[serial_list_index], 9600);
  54.     }
  55.   }
  56.   // Disconnect button clicked
  57.   if (btn_serial_disconnect.MouseIsOver()) {
  58.     if (serial_port != null) {
  59.       // disconnect from the serial port
  60.       serial_port.stop();
  61.       serial_port = null;
  62.     }
  63.   }
  64.   // Refresh button clicked
  65.   if (btn_serial_list_refresh.MouseIsOver()) {
  66.     // get the serial port list and length of the list
  67.     serial_list = Serial.list()[serial_list_index];
  68.     num_serial_ports = Serial.list().length;
  69.   }
  70. }

  71. void draw() {
  72.   // draw the buttons in the application window
  73.   btn_serial_up.Draw();
  74.   btn_serial_dn.Draw();
  75.   btn_serial_connect.Draw();
  76.   btn_serial_disconnect.Draw();
  77.   btn_serial_list_refresh.Draw();
  78.   // draw the text box containing the selected serial port
  79.   DrawTextBox("Select Port", serial_list, 10, 10, 120, 60);
  80. }

  81. // function for drawing a text box with title and contents
  82. void DrawTextBox(String title, String str, int x, int y, int w, int h)
  83. {
  84.   fill(255);
  85.   rect(x, y, w, h);
  86.   fill(0);
  87.   textAlign(LEFT);
  88.   textSize(14);
  89.   text(title, x + 10, y + 10, w - 20, 20);
  90.   textSize(12);  
  91.   text(str, x + 10, y + 40, w - 20, h - 10);
  92. }

  93. // button class used for all buttons
  94. class Button {
  95.   String label;
  96.   float x;    // top left corner x position
  97.   float y;    // top left corner y position
  98.   float w;    // width of button
  99.   float h;    // height of button
  100.   
  101.   // constructor
  102.   Button(String labelB, float xpos, float ypos, float widthB, float heightB) {
  103.     label = labelB;
  104.     x = xpos;
  105.     y = ypos;
  106.     w = widthB;
  107.     h = heightB;
  108.   }
  109.   
  110.   // draw the button in the window
  111.   void Draw() {
  112.     fill(218);
  113.     stroke(141);
  114.     rect(x, y, w, h, 10);
  115.     textAlign(CENTER, CENTER);
  116.     fill(0);
  117.     text(label, x + (w / 2), y + (h / 2));
  118.   }
  119.   
  120.   // returns true if the mouse cursor is over the button
  121.   boolean MouseIsOver() {
  122.     if (mouseX > x && mouseX < (x + w) && mouseY > y && mouseY < (y + h)) {
  123.       return true;
  124.     }
  125.     return false;
  126.   }
  127. }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-1-15 12:48:02 | 显示全部楼层
另一個使用cp5界面和帶防止出錯的串口選用範例:

Processing串口選用範例三
https://stackoverflow.com/questi ... nlist-in-processing

  1. import processing.serial.*;
  2. import controlP5.*;

  3. ControlP5 cp5;
  4. DropdownList serialPortsList;

  5. Serial serialPort;
  6. final int BAUD_RATE = 9600;

  7. String[] portNames;


  8. void setup() {
  9.   size(700, 400,P3D);

  10.   //String[] portNames = Serial.list(); //changed to
  11.   portNames = Serial.list();

  12.   cp5 = new ControlP5(this);
  13.   // create a DropdownList
  14.   serialPortsList = cp5.addDropdownList("serial ports").setPosition(10, 10).setWidth(200);
  15.   for(int i = 0 ; i < portNames.length; i++) serialPortsList.addItem(portNames[i], i);  
  16. }


  17. void controlEvent(ControlEvent theEvent) {
  18.   // DropdownList is of type ControlGroup.
  19.   // A controlEvent will be triggered from inside the ControlGroup class.
  20.   // therefore you need to check the originator of the Event with
  21.   // if (theEvent.isGroup())
  22.   // to avoid an error message thrown by controlP5.
  23.   if (theEvent.isGroup()) {
  24.     // check if the Event was triggered from a ControlGroup
  25.     println("event from group : "+theEvent.getGroup().getValue()+" from "+theEvent.getGroup());
  26.     //check if there's a serial port open already, if so, close it
  27.     if(serialPort != null){
  28.       serialPort.stop();
  29.       serialPort = null;
  30.     }
  31.     //open the selected core
  32.     //String portName = serialPortsList.getItem((int)theEvent.getValue()).toString(); //changed to
  33.     String portName =portNames.toString();
  34.     try{
  35.       serialPort = new Serial(this,portName,BAUD_RATE);
  36.     }catch(Exception e){
  37.       System.err.println("Error opening serial port " + portName);
  38.       e.printStackTrace();
  39.     }
  40.   }
  41.   else if (theEvent.isController()) {
  42.     println("event from controller : "+theEvent.getController().getValue()+" from "+theEvent.getController());
  43.   }
  44. }

  45. void draw() {
  46.   background(128);
  47. }
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 19:22 , Processed in 0.039494 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表