串行通信将不会打印出任何结果
我对所有这些都是很新的,确实需要及时努力,以使我的评估来解决。不幸的是,我完全被困。我已经能够使P5.js读取单个按钮和操纵杆的输出,并做出相应的反应,但是我无法将其打印出来,我会摆脱设置。
设置:4个按钮,所有按钮都可以打开或关闭,以及一个值变量,可以通过将特定的数字添加在一起,告诉我是否和哪些按钮打开和关闭。 (如有必要,可以发布Arduino代码)
是否有任何理由我的P5.J中的代码现在无法正常工作? (从串行通信中没有任何输出)(检验到斯科特·菲茨杰拉德(Scott Fitzgerald)的代码基础)
let serial; // variable for the serial object
let value = "waiting for data"; // variable to hold the data
function setup() {
createCanvas(windowWidth, windowHeight);
// serial constructor
serial = new p5.SerialPort();
// get a list of all connected serial devices
serial.list();
// serial port to use - you'll need to change this
serial.open('COM7');
// callback for when the sketchs connects to the server
serial.on('connected', serverConnected);
// callback to print the list of serial devices
serial.on('list', gotList);
// what to do when we get serial data
serial.on('data', gotData);
// what to do when there's an error
serial.on('error', gotError);
// when to do when the serial port opens
serial.on('open', gotOpen);
// what to do when the port closes
serial.on('close', gotClose);
}
function serverConnected() {
console.log("Connected to Server");
}
// list the ports
function gotList(thelist) {
console.log("List of Serial Ports:");
for (let i = 0; i < thelist.length; i++) {
console.log(i + " " + thelist[i]);
}
}
function gotOpen() {
console.log("Serial Port is Open");
}
function gotClose() {
console.log("Serial Port is Closed");
value = "Serial Port is Closed";
}
function gotError(theerror) {
console.log(theerror);
}
// when data is received in the serial buffer
function gotData() {
let currentString = serial.readLine(); // store the data in a variable
trim(currentString); // get rid of whitespace
if (!currentString) return; // if there's nothing in there, ignore it
console.log(currentString); // print it out
value = currentString; // save it to the global variable
}
function draw() {
background(255, 255, 255);
fill(0, 0, 0);
text(value, 10, 10); // print the data to the sketch
// in this example, we are reciving a 0 and a 1
// if the button is not pressed we get a 0
//if (latestData == 0) {
// ellipse(width / 2, height / 2, 100, 100);
//} else { // if it is pressed, we get a 1
// rectMode(CENTER);
//rect(width / 2, height / 2, 100, 100);
if (value == 0) {
console.log("NO SENSOR")
}
// Test first bit:
if (value % 2 == 1) {
console.log("FIRST SENSOR")
}
// Test second bit:
if ((value >> 1) % 2 == 1) {
console.log("SECOND SENSOR")
}
if ((value >> 2) % 2 == 1) {
console.log("THIRD SENSOR")
}
if ((value >> 3) % 2 == 1) {
console.log("FOURTH SENSOR")
}
}
I am pretty new to all of this and really need to get this working in time for my assessments to come around. Unfortunately, I am completely stuck. I've been able to make p5.js read the output of a single button and of a joystick and react accordingly, but I can't get it to print out the value, I would be getting out of my setup.
Setup: 4 buttons, that all get checked for being on or off and a value variable that tells me if and which buttons are on and off by adding specific numbers together. (Can post the Arduino Code if necessary)
Is there any reason my Code in P5.Js isn't working right now? (No output whatsoever from serial communication) (probs to Scott Fitzgerald for the code groundwork)
let serial; // variable for the serial object
let value = "waiting for data"; // variable to hold the data
function setup() {
createCanvas(windowWidth, windowHeight);
// serial constructor
serial = new p5.SerialPort();
// get a list of all connected serial devices
serial.list();
// serial port to use - you'll need to change this
serial.open('COM7');
// callback for when the sketchs connects to the server
serial.on('connected', serverConnected);
// callback to print the list of serial devices
serial.on('list', gotList);
// what to do when we get serial data
serial.on('data', gotData);
// what to do when there's an error
serial.on('error', gotError);
// when to do when the serial port opens
serial.on('open', gotOpen);
// what to do when the port closes
serial.on('close', gotClose);
}
function serverConnected() {
console.log("Connected to Server");
}
// list the ports
function gotList(thelist) {
console.log("List of Serial Ports:");
for (let i = 0; i < thelist.length; i++) {
console.log(i + " " + thelist[i]);
}
}
function gotOpen() {
console.log("Serial Port is Open");
}
function gotClose() {
console.log("Serial Port is Closed");
value = "Serial Port is Closed";
}
function gotError(theerror) {
console.log(theerror);
}
// when data is received in the serial buffer
function gotData() {
let currentString = serial.readLine(); // store the data in a variable
trim(currentString); // get rid of whitespace
if (!currentString) return; // if there's nothing in there, ignore it
console.log(currentString); // print it out
value = currentString; // save it to the global variable
}
function draw() {
background(255, 255, 255);
fill(0, 0, 0);
text(value, 10, 10); // print the data to the sketch
// in this example, we are reciving a 0 and a 1
// if the button is not pressed we get a 0
//if (latestData == 0) {
// ellipse(width / 2, height / 2, 100, 100);
//} else { // if it is pressed, we get a 1
// rectMode(CENTER);
//rect(width / 2, height / 2, 100, 100);
if (value == 0) {
console.log("NO SENSOR")
}
// Test first bit:
if (value % 2 == 1) {
console.log("FIRST SENSOR")
}
// Test second bit:
if ((value >> 1) % 2 == 1) {
console.log("SECOND SENSOR")
}
if ((value >> 2) % 2 == 1) {
console.log("THIRD SENSOR")
}
if ((value >> 3) % 2 == 1) {
console.log("FOURTH SENSOR")
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论