极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 2691|回复: 4

各位好心朋友,帮帮我好吗?关于processing和arduino问题

[复制链接]
发表于 2014-12-10 22:55:27 | 显示全部楼层 |阅读模式
各位好心的朋友们,帮帮我好吗?我想快点解决这问题。是这样的,最近这几天研究这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();
}
回复

使用道具 举报

发表于 2014-12-11 09:28:38 | 显示全部楼层
把   myPort = new Serial(this, "COM4", 9600);//opens the Serial Port
改成 myPort = new Serial(this, portName,9600);//opens the Serial Port
看看
回复 支持 反对

使用道具 举报

发表于 2014-12-11 09:54:35 | 显示全部楼层
打开注册表HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/COM Name Arbiter
把ComDB删除后重新尝试即可!(注:这样会重建所有COM口)。重启电脑!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-12-11 21:04:18 | 显示全部楼层
275891381 发表于 2014-12-11 09:54
打开注册表HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/COM Name Arbiter
把ComDB删除后重新尝试 ...

按你方法后正常,但是不知道processing情况下怎样才算是正常
回复 支持 反对

使用道具 举报

发表于 2014-12-12 08:21:29 | 显示全部楼层
cjhhao1 发表于 2014-12-11 21:04
按你方法后正常,但是不知道processing情况下怎样才算是正常

写个简单的程序 西安测试一下通信正常不不就知道了
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊 ( 浙ICP备09023225号 )

GMT+8, 2019-9-21 17:58 , Processed in 0.054156 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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