quqianghao 发表于 2013-2-17 22:13:17

求助,串口发送数据的问题

我使用的是2560的板子 在ARDUINO IDE 中的串口查看器中输入的数据都正常,就是用PYTHON 或 PROCESSING 的时候发送的数据就不能成功解析 MACLINUX WINDOWS都测试了不行啊,为啥啊,自带的例子都不行。

ARDUINO 代码是接收串口的数据为H 时 灯亮 否则灭
用IDE 的串口查看器发送参数就可行
用PROCESSING发送参数就不行   串口速率全无问题求救啊


下面的代码是ARDUINO 的是PROCESSING 中的例子


// Wiring/Arduino code:
// Read data from the serial and turn ON or OFF a light depending on the value

char val; // Data received from the serial port
int ledPin = 45; // Set the pin to digital I/O 4

void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop() {
if (Serial.available()) { // If data is available to read,
val = Serial.read(); // read it and store it in val
}
if (val == 'H') { // If H was received
digitalWrite(ledPin, HIGH); // turn the LED on
} else {
digitalWrite(ledPin, LOW); // Otherwise turn it OFF
}
delay(100); // Wait 100 milliseconds for next reading
}


PROCESSION 代码


import processing.serial.*;

Serial myPort;// Create object from Serial class
int val;      // Data received from the serial port

void setup()
{
size(200, 200);
// I know that the first port in the serial list on my mac
// is always myFTDI adaptor, so I open Serial.list().
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = "COM4";
myPort = new Serial(this, portName, 9600);
println(portName);
}

void draw() {
background(255);
if (mouseOverRect() == true) {// If mouse is over square,
    fill(204);                  // change color and
    myPort.write('H');            // send an H to indicate mouse is over square
    println('H');
}
else {                        // If mouse is not over square,
    fill(0);                      // change color and
    myPort.write('L');            // send an L otherwise
    println('L');
}
rect(50, 50, 100, 100);         // Draw a square
}

boolean mouseOverRect() { // Test if mouse is over square
return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}
页: [1]
查看完整版本: 求助,串口发送数据的问题