|
各位好心的朋友们,帮帮我好吗?我想快点解决这问题。是这样的,最近这几天研究这processing和arduino互动的问题,总是不成功的。
我在processing写了一些代码,processing和arduino的串口都是COM4,可是,在arduino下,就出现这问题就出现以下的一串java错误。
processing.app.SerialException: 串口'COM4'在使用中。尝试退出任何可能在使用它的其他程序。
at processing.app.Serial.<init>(Serial.java:171)
at processing.app.Serial.<init>(Serial.java:77)
at processing.app.debug.Uploader.flushSerialBuffer(Uploader.java:77)
at processing.app.debug.AvrdudeUploader.uploadViaBootloader(AvrdudeUploader.java:175)
at processing.app.debug.AvrdudeUploader.uploadUsingPreferences(AvrdudeUploader.java:67)
at processing.app.Sketch.upload(Sketch.java:1671)
at processing.app.Sketch.exportApplet(Sketch.java:1627)
at processing.app.Sketch.exportApplet(Sketch.java:1599)
at processing.app.Editor$DefaultExportHandler.run(Editor.java:2380)
at java.lang.Thread.run(Thread.java:619)
processing.app.debug.RunnerException: 串口'COM4'在使用中。尝试退出任何可能在使用它的其他程序。
at processing.app.debug.Uploader.flushSerialBuffer(Uploader.java:101)
at processing.app.debug.AvrdudeUploader.uploadViaBootloader(AvrdudeUploader.java:175)
at processing.app.debug.AvrdudeUploader.uploadUsingPreferences(AvrdudeUploader.java:67)
at processing.app.Sketch.upload(Sketch.java:1671)
at processing.app.Sketch.exportApplet(Sketch.java:1627)
at processing.app.Sketch.exportApplet(Sketch.java:1599)
at processing.app.Editor$DefaultExportHandler.run(Editor.java:2380)
at java.lang.Thread.run(Thread.java:619)
希望大家帮帮我解决。万分感谢!
arduino代码:
#define time 4 // time in miliseconds between each motor's step
#define pentime 200 //time of the pulse in the pen motor
#define pulltime 6200 //time(in microseconds to allow a alrge range) of the pulse on the motor to pull the paper.
#define penPin 4 //pen motor pin
#define paperPin 5 //paper motor pin
#define StepperA1 9 //stepper motor pins
#define StepperA2 10 //
#define StepperB1 11 //
#define StepperB2 12 //
byte data;//byte to receive and check data from computer
void setup() {
Serial.begin(9600);//Start the Serial port.
pinMode(StepperA1, OUTPUT);//Stepper motor pins as output
pinMode(StepperA2, OUTPUT);
pinMode(StepperB1, OUTPUT);
pinMode(StepperB2, OUTPUT);
pinMode(penPin,OUTPUT);//Pen motor pin as a output
pinMode(paperPin,OUTPUT);//Paper motor pin as a output
back(80);//makes the car return, to be sure it will start at the begin of the line
}
void loop() {
if(Serial.available()){//if there is data on the Serial buffer
while(Serial.available()>0){//continues reading the values on the buffer.
data=Serial.read();
if(data==1){
dot();//if a one was received, makes a dot.
forward(1);//avances one time (four steps)
}
if(data==0){
forward(1);// if a zero was received just avances.
}
if(data=='L'){//if a "L" was received, pull the paper to begin a new line.
pullPaper();//call the pulling paper function.
back(80);//return the car.
}
}
}
}
void forward(int number){//forward function, you can choose the number of steps when calling the function
int i=0;//to control the number of steps
while(i<number){
digitalWrite(StepperA1, HIGH); //this sequence is part of the basic algoritm to control a bipolar stepper motor.
digitalWrite(StepperA2, LOW);
digitalWrite(StepperB1, HIGH);
digitalWrite(StepperB2, LOW);
delay(time);//wait the time defined.
digitalWrite(StepperA1, HIGH);
digitalWrite(StepperA2, LOW);
digitalWrite(StepperB1, LOW);
digitalWrite(StepperB2, HIGH);
delay(time);
digitalWrite(StepperA1, LOW);
digitalWrite(StepperA2, HIGH);
digitalWrite(StepperB1, LOW);
digitalWrite(StepperB2, HIGH);
delay(time);
digitalWrite(StepperA1, LOW);
digitalWrite(StepperA2, HIGH);
digitalWrite(StepperB1, HIGH);
digitalWrite(StepperB2, LOW);
delay(time);
i++;
}
digitalWrite(StepperA1, LOW);
digitalWrite(StepperA2, LOW);
digitalWrite(StepperB1, LOW);
digitalWrite(StepperB2, LOW);
}
void back(int number){//As the forward function, but makes the reverse sequence.
int i=0;
while(i<number){
digitalWrite(StepperA1, LOW);
digitalWrite(StepperA2, HIGH);
digitalWrite(StepperB1, HIGH);
digitalWrite(StepperB2, LOW);
delay(time);
digitalWrite(StepperA1, LOW);
digitalWrite(StepperA2, HIGH);
digitalWrite(StepperB1, LOW);
digitalWrite(StepperB2, HIGH);
delay(time);
digitalWrite(StepperA1, HIGH);
digitalWrite(StepperA2, LOW);
digitalWrite(StepperB1, LOW);
digitalWrite(StepperB2, HIGH);
delay(time);
digitalWrite(StepperA1, HIGH);
digitalWrite(StepperA2, LOW);
digitalWrite(StepperB1, HIGH);
digitalWrite(StepperB2, LOW);
delay(time);
i++;
}
digitalWrite(StepperA1, LOW);
digitalWrite(StepperA2, LOW);
digitalWrite(StepperB1, LOW);
digitalWrite(StepperB2, LOW);
}
void dot(void){//actives the pen motor for some time
digitalWrite(penPin,HIGH);
delay(pentime);
digitalWrite(penPin,LOW);
}
void pen(void){ //this functions can be used if you want to make lines instead of dots,
digitalWrite(penPin,HIGH); // I think that the dots are cool.
} //
void nopen(void){ //
digitalWrite(penPin,LOW); //
} //
void pullPaper(void){ //function to pull the paper
digitalWrite(paperPin,HIGH);
delayMicroseconds(pulltime);
digitalWrite(paperPin,LOW);
}
processing代码是:
import processing.serial.*;
Serial myPort; // Create object from Serial class
PImage IMG; //create a PImage named IMG
void setup() {
size(60, 118);//Put here the size of your image
String portName = Serial.list()[0];
myPort = new Serial(this, "COM4", 9600);//opens the Serial Port
IMG = loadImage("link2.jpg");//Open your image, put here the name of your image, including the extension.
image(IMG, 0, 0);
}
void draw() {
IMG.loadPixels();
for (int y = 0; y < height; y++) {//check the lines
for (int x = 0; x < width; x++) { //check each pixel in the line
int loc = x + y*width; //the pixels are loaded as in a aray, so its position is only a number.
if (IMG.pixels[loc]>color(128)) {
myPort.write(0);//if a pixel is not black, send a '0'.
delay(50);
}
else {
myPort.write(1);//if a pixel is black, send a '1'.
delay(50);
}
}
delay(10000);//Wait for some time, time enough to the printer finish this line.
myPort.write('L');//send a "L" to indicate a new line.
}
noLoop();
}
|
|