作者cktheonly (影)
看板Electronics
標題[問題] Arduino Serial Port 連接問題
時間Sat Oct 3 09:31:53 2015
我是這方面的新手,現在遇到一些問題想上來請教一下~
以下是我的設置:
http://imgur.com/ixpWdrv
最後的目標是讓Arduino讀取device的資料後,可以自動控制Relay。
現階段則是想先用PC確保可以讀到device上傳來的資訊(MODBUS)
目前遇到的問題是,根據收到的結果判斷,似乎連接失敗了!(communication error)
連接時間過長,可能根本沒有抓到...
device直接連PC是可以讀到資料的,把程式燒錄到Arduino也沒問題
RS232的模組也確認過了沒壞掉,不知道是在連接上還是code有錯
想請大家幫幫忙,拯救一下小弟QAQ
(資料參考來源:
http://tinyurl.com/pua5sbs)
以下是code:
// Include these libraries for using the RS-232 and Modbus functions
#include <RS232.h>
#include <ModbusMaster232.h>
#include <SPI.h>
// Instantiate ModbusMaster object as slave ID 1
ModbusMaster232 node(1);
// Define one addres for reading
#define address 0x0000
// Define the number of bytes to read
#define bytesQty 2
void setup()
{
// Power on the USB for viewing data in the serial monitor
Serial.begin(115200);
delay(100);
// Initialize Modbus communication baud rate
node.begin(9600);
// Print hello message
Serial.println("Modbus communication over RS-232");
//node.PrintTest();
delay(100);
}
void loop()
{
// This variable will store the result of the communication
// result = 0 : no errors
// result = 1 : error occurred
int result = node.readHoldingRegisters(address, bytesQty);
if (result != 0) {
// If no response from the slave, print an error message
Serial.println(result,DEC);
Serial.println("Communication error");
delay(1000);
}
else {
// If all OK
Serial.print("Read value : ");
// Print the read data from the slave
Serial.print(node.getResponseBuffer(0));
delay(1000);
}
Serial.print("\n");
delay(2000);
// Clear the response buffer
node.clearResponseBuffer();
}
請問一下,我連接的方式有誤嗎?
code有哪邊不對嗎?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.104.83
※ 文章網址: https://www.ptt.cc/bbs/Electronics/M.1443835917.A.F0F.html
→ boson5566: 兩者baud rate不一樣怎麼會收的到.. 10/03 12:53
→ beggerchou: 快的會收到 值是錯誤的而已 10/03 16:22
→ easypro: 232接modbus??? 兩個訊號不同你怎麼接在一起? 10/03 20:20
→ easypro: modbus是走RS485半雙工模式 RS232是全雙工 而且電壓準位 10/03 20:22
→ easypro: 是不一樣的 10/03 20:22
→ easypro: 網頁上有寫RS485那個模組才能走Modbus RS232不能 10/03 20:29
推 gabbana: 485是diff. rx tx全雙工有四條232是single ended txrx 10/09 14:42