极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 13767|回复: 2

两块arduinoRX,TX通信问题

[复制链接]
发表于 2016-1-5 23:18:24 | 显示全部楼层 |阅读模式
目标是按下遥控器,发送一个a或者A,然后另一块的lcd有显示,但目前是发送一次a后,再发A没反应,必须通过关闭电脑上的串口监视器窗口才行,也就是发一次a,lcd有显示,关了IDE的串口监视器,在打开,发送A,另一块板子的lcd有显示,想发送a必须关了串口监视器,求大神解释一下,是tx,rx就这样,还是代码没处理好,清空缓存啥的,求解释


************从机代码*****************************************************
// IMPORTANT: Adafruit_TFTLCD LIBRARY MUST BE SPECIFICALLY
// CONFIGURED FOR EITHER THE TFT SHIELD OR THE BREAKOUT BOARD.
// SEE RELEVANT COMMENTS IN Adafruit_TFTLCD.h FOR SETUP.
////Technical support:[email protected]

#include <Adafruit_GFX.h>    // Core graphics library
#include <Adafruit_TFTLCD.h> // Hardware-specific library

// The control pins for the LCD can be assigned to any digital or
// analog pins...but we'll use the analog pins as this allows us to
// double up the pins with the touch screen (see the TFT paint example).
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0

#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin

// When using the BREAKOUT BOARD only, use these 8 data lines to the LCD:
// For the Arduino Uno, Duemilanove, Diecimila, etc.:
//   D0 connects to digital pin 8  (Notice these are
//   D1 connects to digital pin 9   NOT in order!)
//   D2 connects to digital pin 2
//   D3 connects to digital pin 3
//   D4 connects to digital pin 4
//   D5 connects to digital pin 5
//   D6 connects to digital pin 6
//   D7 connects to digital pin 7
// For the Arduino Mega, use digital pins 22 through 29
// (on the 2-row header at the end of the board).

// Assign human-readable names to some common 16-bit color values:
#define  BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF

Adafruit_TFTLCD tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
// If using the shield, all control and data lines are fixed, and
// a simpler declaration can optionally be used:
// Adafruit_TFTLCD tft;

Adafruit_GFX_Button buttons[15];

char ss;
void setup()
{
Serial.begin(9600);
  tft.reset();
   uint16_t identifier = tft.readID();
  tft.begin(identifier);
}

void loop()
{
  if (Serial.available()>0){
  ss=Serial.read();
  if(ss=='a')
  {
  tft.fillScreen(BLACK);
  tft.setCursor(40, 40);  
  tft.setTextColor(RED);  tft.setTextSize(2);
  tft.println("Danger!!!");
  ss=0;  
  delay(3000);                                                                                                                    
Serial.print('b');   //收到'a'或者'A'后向主机发一个'b',
}                     //目的是让主机可以循环读取下一个数
   else if(ss=='A')
  {
  tft.fillScreen(BLACK);
  tft.setCursor(40, 40);  
  tft.setTextColor(RED);  tft.setTextSize(2);
  tft.println("Safe!!!");
  ss=0;   
  delay(3000);
Serial.print('b');//收到'a'或者'A'后向主机发一个'b',
                   //目的是让主机可以循环读取下一个数                                                                                                                  
  }

}
}
   

*********************主机代码****************
#include <IRremote.h>
#include <IRremoteInt.h>
const int irReceiverPin = 3;  
IRrecv irrecv(irReceiverPin);
decode_results results;
char ss;
int i=1;
void setup()
{
Serial.begin(9600); //设置串口速度
irrecv.enableIRIn();
}
void loop()
{
  if (irrecv.decode(&results)){
     if(results.value==275196119){
        if(i>0){                 //启动时的开关信号,这样可以避免主程序陷入死循环
          Serial.print('a');     //先向从机发一个‘a’,
          i=0;
          }
        ss=Serial.read();       //再读串口,接受从机的字符‘b’
        if(ss=='b'){
         Serial.print("ok1");
           ss=0;
           i=1;//让i重新赋值
           }
  irrecv.resume();  
  delay(500);
}

else if(results.value==275212439){
        if(i>0){                 //启动时的开关信号,这样可以避免主程序陷入死循环
          Serial.print('A');     //先向从机发一个‘A’,
          i=0;
          }
       ss=Serial.read();       //再读串口,接受从机的字符‘b’
       if(ss=='b'){
          Serial.print("ok2");
          ss=0;
          i=1;//让i重新赋值
          }
  irrecv.resume();  
  delay(500);
}

}
}
回复

使用道具 举报

发表于 2016-1-6 09:51:45 | 显示全部楼层
除非你是用 mega 之類有多個硬串口的板子, 否則 "电脑上的串口" 連接的 USB 就等同於板子上的 Tx/Rx.

如果需要同時使用電腦連線, 而通訊速度沒有大要求的話, 可以嘗試改用 軟串口作 arduino 之間通訊.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-1-6 19:24:02 | 显示全部楼层
Super169 发表于 2016-1-6 09:51
除非你是用 mega 之類有多個硬串口的板子, 否則 "电脑上的串口" 連接的 USB 就等同於板子上的 Tx/Rx.

如 ...

一块是mega,一块是UNO,我就想问问,两块板子独立供电,不连接电脑,传输速率别太慢的话,tx,rx通信可以不,是不是spi更好一点···
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-7 14:20 , Processed in 0.062921 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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