2016-03-05 166 views
0

我有一個液晶顯示器連接到顯示圖像的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模塊上。

任何解決方案。

由於

回答

1

簡短的回答:

你需要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