簡體   English   中英

使用 Arduino 通過 RS232 進行通信的問題

[英]Issues communicating over RS232 using Arduino

我正在做一個項目,我必須使用 RS232 與機器通信。 我一直在使用 RS232 到 USB 適配器通過在串行終端(白蟻)上進行通信來使機器工作。 所以現在這台機器完全是 function,我可以在我的電腦上工作。

然而,下一步是自動化這台機器所做的一些過程。 所以我想使用 Arduino Uno 來控制它。

在此處輸入圖像描述

因此,我從機器手冊中獲得了 RS232 的這些引腳,但這實際上是使用 RS232 連接時的全部內容。 所以我接下來要做的是:

RXD -> TX (PIN 2 on RS232, to PIN 3 on Arduino)

TXD -> RX (PIN 3 on RS232, to PIN 2 on Arduino)

ground to ground (PIN 5 to ground)

我遇到的一個問題是,要上傳到 Arduino,我無法連接到專用的 RX 和 TX 引腳(引腳 0 和 1),所以我做了以下操作。 超級基礎。

void setup()
{
  byte rx = 2;  //rx pin number
  byte tx = 3;  //tx pin number
  pinMode(rx, INPUT);
  pinMode(tx, OUTPUT);
  Serial.begin(9600, SERIAL_8N1); //9600 baudrate, 8 data bit, no parity, 1 stop bit
}

void loop() 
{
  Serial.write("/1P200R\n");  //writes/1P200R (this is a built in command on the machine that works when connected to the machine using USB. The \n is the character return (CR)
  delay(1000);
}

當我這樣做時,我可以在串行監視器上每秒看到 /1P200R 的輸出,但我的機器什么也沒做。

有人對我如何通過 Arduino 與我的機器進行一些通信有任何建議嗎? 我應該使用引腳 0 和 1,專用的 RX 和 TX 嗎? 我已經嘗試過了,但 output 沒有任何改變。

void setup()
{
  byte rx = 2;  //rx pin number
  byte tx = 3;  //tx pin number
  pinMode(rx, INPUT);
  pinMode(tx, OUTPUT);
  Serial.begin(9600, SERIAL_8N1); //9600 baudrate, 8 data bit, no parity, 1 stop bit
}

Serial 是連接到引腳 0 和 1 的 Arduino 微控制器的硬件串行。命名變量 rx 和 tx 並使各自的引腳 2 和 3 輸入,output 對您一點幫助都沒有。

如果您想使用 Arduino 的其他數字 I/O 引腳進行串行通信,您必須使用SoftwareSerial在軟件中進行仿真

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup()
{
  // set the data rate for the SoftwareSerial port
  mySerial.begin(38400);
  mySerial.println("Hello, world?");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM