关于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