【问题标题】:Xbee and Arduino Serial CommunicationXbee 和 Arduino 串行通信
【发布时间】:2016-03-05 13:51:05
【问题描述】:

我有一个连接到显示图像的 Arduino 的 LCD,如果触摸它,图像会显示在串行上。我想使用 Xbee 将串行上写入的数据发送到 PC。我已经配置了 Xbee 模块,它们工作正常。

我面临的问题是没有自动发送到 PC 的数据。相反,如果我在串行中写了一些东西,那么它就会显示在 PC 上。我使用的代码是

 // UTFT_SdRaw_800x480_Demo
// Copyright (C)2015 Graham Lawrence (GHLawrence2000). All Rights reserved.
// web: https://github.com/ghlawrence2000/UTFT_SdRaw
//
// This program is a demo of how to use the functions provided by UTFT_SdRaw.
//
// This program requires the UTFT, UTouch, UTFT_Buttons and SdFat libraries.
//
#include <SPI.h>
// SdFat lib from here :-
// https://github.com/greiman/SdFat/archive/master.zip
#include <SdFat.h>
#include <UTFT.h>
#include <UTouch.h>
#include <UTFT_SdRaw.h>
#include <SoftwareSerial.h>
extern uint8_t SmallFont[];
extern uint8_t BigFont[];

#define SD_CHIP_SELECT  53  // SD chip select pin
// file system object
SdFat sd;
// print stream
SoftwareSerial XBee(10, 11); // RX, TX


// Initialize display
// ------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield            : <display model>,19,18,17,16
// Standard Arduino Mega/Due shield            : <display model>,38,39,40,41
// CTE TFT LCD/SD Shield for Arduino Due       : <display model>,25,26,27,28
// Teensy 3.x TFT Test Board                   : <display model>,23,22, 3, 4
// ElecHouse TFT LCD/SD Shield for Arduino Due : <display model>,22,23,31,33
//
// Remember to change the model parameter to suit your display module!

//UTFT myGLCD(CPLD, 38, 39, 40, 41);
//UTFT myGLCD(CTE50, 38, 39, 40, 41);
UTFT myGLCD(CTE70, 38, 39, 40, 41);


// Initialize touchscreen
// ----------------------
// Set the pins to the correct ones for your development board
// -----------------------------------------------------------
// Standard Arduino Uno/2009 Shield            : 15,10,14, 9, 8
// Standard Arduino Mega/Due shield            :  6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due       :  6, 5, 4, 3, 2
// CTE TFT LCD/SD Shield for Arduino Due (JP10):  6, 5,32, 3, 2
// Teensy 3.x TFT Test Board                   : 26,31,27,28,29
// ElecHouse TFT LCD/SD Shield for Arduino Due : 25,26,27,29,30
//
UTouch  myTouch(6, 5, 4, 3, 2);

UTFT_SdRaw myFiles(&myGLCD);

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  XBee.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for DUE & Leonardo only
  }
  Serial.println(F("Initialising SD card..."));
  bool mysd = 0;
  // see if the card is present and can be initialized:
  while (!mysd) {
    if (!sd.begin(SD_CHIP_SELECT, SPI_FULL_SPEED)) {
      Serial.println(F("Card failed, or not present"));
      Serial.println(F("Retrying...."));
    } else {
      mysd = 1;
      Serial.println(F("Card initialised."));
    }
  }
  Serial.println(F("Initialising LCD."));
  myGLCD.InitLCD();
  myGLCD.setFont(SmallFont);
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_MEDIUM);
  Serial.println(F("LCD initialised."));
  myGLCD.clrScr();
  myFiles.load(0, 0, 800, 480, "lcd1.raw", 2, 0);
}

byte dispScreen = 1;  // Currently displayed screen, screens shown below                                        //
// 1-home, 2-lights, , 3-temp, 4-fogger, 5-mister, 6-fan, 7-clock, 8-screen                                     //

void processMyTouch() {
  myTouch.read();
  int x = myTouch.getX();
  int y = myTouch.getY();
  switch (dispScreen) { //                                                                                        //
    //                                                                                                          //
    case 1:  // home screen                                                                                     //

      // do stuff here for screen 1

      if ((x > 650 && x < 800) && (y > 20 && y < 460)) {
        //button 'back'
        myGLCD.clrScr();
        dispScreen = 2; // <======================================= change screen each time you go to a new screen
        myFiles.load(0, 0, 800, 480, "lcd_2.raw", 2, 0);
      }
      if ((x > 19 && x < 340) && (y > 21 && y < 212)) {
        //button 'news'
        Serial.println("Drinks");

      }
      if ((x > 400 && x < 5700) && (y > 21 && y < 212)) {
        //button 'news'
        Serial.println(F("Food"));
      }

      if ((x > 19 && x < 340) && (y > 240 && y < 448)) {
        //button 'news'
        Serial.println(F("Tea"));
      }

      if ((x > 400 && x < 550) && (y > 240 && y < 448)) {
        //button 'news'
        Serial.println(F("Water"));
      }


      break;

    case 2:

      // do stuff here for screen 2


      if ((x > 170 && x < 430) && (y > 21 && y < 212)) {
        //button 'news'
        Serial.println(F("news"));
      }

      if ((x > 17 && x < 100) && (y > 30 && y < 400)) {
        //button 'news'
        myGLCD.clrScr();
        dispScreen = 1; // <======================================= change screen each time you go to a new screen
        myFiles.load(0, 0, 800, 480, "lcd1.raw", 2, 0);
      }

      if ((x > 530 && x < 750) && (y > 21 && y < 212)) {
        //button 'news'
        Serial.println(F("light."));
      }

      if ((x > 530 && x < 750) && (y > 240 && y < 448)) {
        //button 'news'
        Serial.println(F("Blanket"));
      }

      if ((x > 170 && x < 430) && (y > 240 && y < 448)) {
        //button 'news'
        Serial.println(F("Medic"));
      }


      break;

  }
}


unsigned long prevMillisTouch = 0; // track time between touches                                                //
unsigned long TouchRepeatDelay = 500; // millis                                                                 //

void loop() {
  unsigned long currentMillis = millis(); // get current millis                                                 //
  if (myTouch.dataAvailable())  { //                                                                            //
    //                     make sure it's been .5 sec (initially) between touches                               //
    if (currentMillis - prevMillisTouch > TouchRepeatDelay) {                                                   //
      //                                                                                                        //
      prevMillisTouch = currentMillis; // reset the touch timer                                                 //
      processMyTouch();                                                                                         //
    } //                                                                                                        //
  } //  

 //
  if (Serial.available()) {
    // If data comes in from serial monitor, send it out to XBee
    XBee.write(Serial.read());
  }
  if (XBee.available()) {
    // If data comes in from XBee, send it out to serial monitor
    Serial.write(XBee.read());
  }
}

例子:

if ((x > 19 && x < 340) && (y > 21 && y < 212)) {
        //button 'news'
        Serial.println("Drinks");

如果我触摸上面的坐标,串口上会显示“饮料”。但是,数据不会发送到另一个 Xbee 模块。如果我在串口中手动写入饮料,它会显示在另一个 Xbee 模块上。

任何解决方案。

谢谢

【问题讨论】:

    标签: arduino xbee


    【解决方案1】:

    简答:

    您需要 XBee.println("Drinks"); 而不是 Serial.println("Drinks"); 将字符串“Drinks”发送到另一个 XBee 模块。

    Serial.println 只会在本地控制台上写入文本,XBee.println 只会向其他 XBee 发送数据。如果您希望在控制台上发送和显示数据,则需要调用这两个函数。

    长答案:

      if (Serial.available()) {
        // If data comes in from serial monitor, send it out to XBee
        XBee.write(Serial.read());
      }
    

    这部分代码读取您在串行控制台上编写的任何内容并将其发送到 Xbee。

      if (XBee.available()) {
        // If data comes in from XBee, send it out to serial monitor
        Serial.write(XBee.read());
      }
    

    这部分代码读取 Xbee 上接收到的任何内容并将其写入串行控制台。

    Serial 是您的 PC 和 arduino 之间的连接。 XBee 是 arduino 和 XBee 之间的连接。在控制台中手动写入文本是可行的,因为第一块代码通过调用XBee.write 函数将您写入的内容重新传输到 XBee。如果你想向 XBee 发送数据,你需要调用 XBee.write/XBee.println 函数。

    【讨论】:

    • 谢谢哈桑,它有效。但是,现在我试图在 LCD 上显示 xbee 上的数据。我正在使用带 LCD 的 i2c 端口。我正在尝试使用函数 lcd.write(XBee.read());功能,但它只在液晶显示器上显示黑框。我使用了 lcd.print("test") 并且它有效。如何使用 lcd.write 函数
    • @shayaan123 这很可能是因为您正在为 Xbee 使用软件序列号。软件串行无法正确捕获字符,因为 arduino 正忙于将前一个字符写入 lcd。
    • 是的,我正在使用软件序列号。有没有办法来解决这个问题。我真的很感激。
    • @shayaan123 读取您在数组中收到的字符串,并在 Xbee 没有传入数据时从数组中写入 lcd。
    • 我不知道该怎么做。你能举一个我可以改变我的代码的例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多