资源预览内容
第1页 / 共8页
第2页 / 共8页
第3页 / 共8页
第4页 / 共8页
第5页 / 共8页
第6页 / 共8页
第7页 / 共8页
第8页 / 共8页
亲,该文档总共8页全部预览完了,如果喜欢就下载吧!
资源描述
Processing的串口通讯的串口通讯Processing的串口通讯的串口通讯l通过串口,可以读写,双向通讯。系统范例系统范例l学习系统自带的范例:lFile-examples关键语句分析关键语句分析l引用库:limport processing.serial.*;l定义变量lSerial myPort;l连接指定串口l String portName = Serial.list()0;l myPort = new Serial(this, portName, 9600);关键语句分析关键语句分析l判断串口是否有数据lmyPort.available() 0l获取串口数据lval = myPort.read(); l输出串口数据lmyPort.write(H); 范例范例1:import processing.serial.*;Serial myPort; / Create object from Serial classint val; / Data received from the serial portvoid setup() size(200, 200);String portName = Serial.list()0; myPort = new Serial(this, portName, 9600);void draw() if ( myPort.available() 0) / If data is available, val = myPort.read(); / read it and store it in val background(255); / Set background to white if (val = 0) / If the serial value is 0, fill(0); / set fill to black else / If the serial value is not 0, fill(204); / set fill to light gray rect(50, 50, 100, 100);范例范例1中中arduino的代码的代码int switchPin = 4; / Switch connected to pin 4void setup() pinMode(switchPin, INPUT); / Set pin 0 as an input Serial.begin(9600);void loop() if (digitalRead(switchPin) = HIGH) / If switch is ON, Serial.print(1, BYTE); / send 1 to Processing else / If the switch is not ON, Serial.print(0, BYTE); / send 0 to Processing delay(100); / Wait 100 milliseconds练习:练习:l基础:做一个文字型的电子钟;l进阶:做一个文字加现状的电子钟。
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号