极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9440|回复: 0

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

[复制链接]
发表于 2013-2-17 22:13:17 | 显示全部楼层 |阅读模式
我使用的是2560的板子 在ARDUINO IDE 中的串口查看器中输入的数据都正常,就是用PYTHON 或 PROCESSING 的时候发送的数据就不能成功解析 MAC  LINUX WINDOWS都测试了  不行啊,为啥啊,自带的例子都不行。

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


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


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

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

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

  9. void loop() {
  10. if (Serial.available()) { // If data is available to read,
  11. val = Serial.read(); // read it and store it in val
  12. }
  13. if (val == 'H') { // If H was received
  14. digitalWrite(ledPin, HIGH); // turn the LED on
  15. } else {
  16. digitalWrite(ledPin, LOW); // Otherwise turn it OFF
  17. }
  18. delay(100); // Wait 100 milliseconds for next reading
  19. }
复制代码


PROCESSION 代码


  1. import processing.serial.*;

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

  4. void setup()
  5. {
  6.   size(200, 200);
  7.   // I know that the first port in the serial list on my mac
  8.   // is always my  FTDI adaptor, so I open Serial.list()[0].
  9.   // On Windows machines, this generally opens COM1.
  10.   // Open whatever port is the one you're using.
  11.   String portName = "COM4";
  12.   myPort = new Serial(this, portName, 9600);
  13.   println(portName);
  14. }

  15. void draw() {
  16.   background(255);
  17.   if (mouseOverRect() == true) {  // If mouse is over square,
  18.     fill(204);                    // change color and
  19.     myPort.write('H');              // send an H to indicate mouse is over square
  20.     println('H');
  21.   }
  22.   else {                        // If mouse is not over square,
  23.     fill(0);                      // change color and
  24.     myPort.write('L');              // send an L otherwise
  25.     println('L');
  26. }
  27.   rect(50, 50, 100, 100);         // Draw a square
  28. }

  29. boolean mouseOverRect() { // Test if mouse is over square
  30.   return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
  31. }
复制代码
回复

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-9 15:04 , Processed in 0.036737 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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