Xbee和Arduino串行通信

Xbee和Arduino串行通信

问题描述:

我有一个液晶显示器连接到显示图像的Arduino,如果它被触摸,图像显示在串行。我想使用Xbee将串口上写入的数据发送到PC。我配置了Xbee模块,它们工作正常。Xbee和Arduino串行通信

我面临的问题是数据传输到个人电脑不是自动发送。相反,如果我在串行中写入某些内容,那么它将显示在PC上。使用的代码IM是

// 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模块上。

任何解决方案。

由于

简短的回答:

你需要XBee.println("Drinks");而不是Serial.println("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函数。

+0

谢谢哈桑,它的工作原理。但是,现在我正试图在LCD上显示xbee上的数据。我在LCD上使用了一个i2c端口。我正在尝试使用函数lcd.write(XBee.read());功能,但它只在液晶显示屏上显示黑框。我使用了lcd.print(“测试”),它工作。我如何使用lcd.write函数 – shayaan123

+0

@ shayaan123这很可能是因为您正在为Xbee使用软件序列。由于arduino忙于将前一个字符写入液晶显示器,软件序列无法正确捕获字符。 –

+0

是的,我正在使用一个软件序列。有没有办法来解决这个问题。我真的很感激。 – shayaan123