關於485的一個問題 已解決 |連接方法 RX TX引腳


#include <M5Stack.h>
#define RX_PIN      16
#define TX_PIN      17

#define X_OFF 160
#define Y_OFF 30

int i=0,s=0;

void setup() {
  M5.begin();
  M5.Power.begin();
  M5.Lcd.setTextSize(1);
  M5.Lcd.drawString("RS485 Unit test", 75, 3, 4);
  Serial.begin(115200);
  Serial2.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);  //Set the baud rate of serial port 2 to 115200,8 data bits, no parity bits, and 1 stop bit, and set RX to 16 and TX to 17.  設置串口二的波特率為115200,8位數據位,沒有校驗位,1位停止位,並設置RX為16,TX為17
}


void loop() {
 

  if(Serial2.available()){
    M5.Lcd.print(char(Serial2.read()));
 }
  delay(100);
}

 

 

 

 

 

 可以正常顯示出向串口發送的數據;可是使用如下的代碼向串口寫(不改變接線),llcom讀不到任何內容

#include <M5Stack.h>
#define RX_PIN      16
#define TX_PIN      17

#define X_OFF 160
#define Y_OFF 30

int i=0,s=0;

void setup() {
  M5.begin();
  M5.Power.begin();
  M5.Lcd.setTextSize(1);
  M5.Lcd.drawString("RS485 Unit test", 75, 3, 4);
  Serial.begin(115200);
  Serial2.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);  //Set the baud rate of serial port 2 to 115200,8 data bits, no parity bits, and 1 stop bit, and set RX to 16 and TX to 17.  設置串口二的波特率為115200,8位數據位,沒有校驗位,1位停止位,並設置RX為16,TX為17
}


void loop() {
  Serial2.write("Hello\n");

  if(Serial2.available()){
    M5.Lcd.print(char(Serial2.read()));
 }
  delay(100);
}

 

 llcom讀不到任何內容,但是依然能顯示出來

改成下面這樣,llcom依舊讀不到

#include <M5Stack.h>
#define RX_PIN      16
#define TX_PIN      17

#define X_OFF 160
#define Y_OFF 30

int i=0,s=0;

void setup() {
  M5.begin();
  M5.Power.begin();
  M5.Lcd.setTextSize(1);
  M5.Lcd.drawString("RS485 Unit test", 75, 3, 4);
  Serial.begin(115200);
  Serial2.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);  //Set the baud rate of serial port 2 to 115200,8 data bits, no parity bits, and 1 stop bit, and set RX to 16 and TX to 17.  設置串口二的波特率為115200,8位數據位,沒有校驗位,1位停止位,並設置RX為16,TX為17
}


void loop() {
  Serial2.write("Hello\n");
  delay(100);
}

 

不知道是為什么

解決:

 

 

電路接的不對,portC出來是TTL電平,485轉USB需要的是485電平;

上面的例程需要portC接一個485單元,再將485單元與485轉USB的轉換器連接;

M5Stack IOT Base集成了485單元,底座有專門的485接口,直接將其與轉換器連接即可。下面是正確的連接方法:

 

 可是按照這樣的接法,依然無法通信,一番探索后(找了一個基於485的modbus例程,其中TX_pin是15)發現是官網給的引腳號錯了,

 

 這里TX_pin 應該是15 ,RX_pin 是13,圖上標錯了,最后給出可以正常通信的例程:

#include <M5Stack.h>
#define RX_PIN      13
#define TX_PIN      15
void setup() {
  M5.begin();
  M5.Lcd.setTextSize(1);
  M5.Lcd.drawString("RS485 Unit test", 75, 3, 4);
  Serial2.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN);  //Set the baud rate of serial port 2 to 115200,8 data bits, no parity bits, and 1 stop bit, and set RX to 16 and TX to 17.  設置串口二的波特率為115200,8位數據位,沒有校驗位,1位停止位,並設置RX為16,TX為17
}  

void loop() {
  if(Serial2.available()){
    M5.Lcd.print(char(Serial2.read()));
  }
  delay(100);
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM