使用一个Arduino Duemilanove和Cutedigi RS-232接口,RS-232通讯 [英] RS-232 communication using an Arduino Duemilanove and the Cutedigi RS-232 interface
问题描述
我有一些麻烦我的Arduino单片机读取 RS-232信号 。我的项目需要我读正由一个空气质量监测输出的数据。
I'm having some trouble getting my Arduino microcontroller to read RS-232 signals. My project requires me to read data that is being outputted by an air quality monitor.
我的组件:
- Arduino Duemilanove
- Cutedigi RS-232 interface
要测试是否串行通信工作正常,我发现Arduino的网站上的一些例如code。这是我运行的确切code:
To test if the serial communications is working properly, I found some example code on the Arduino website. This is the exact code that I am running:
//Created August 23 2006
//Heather Dewey-Hagborg
//http://www.arduino.cc
#include <ctype.h>
#define bit9600Delay 84
#define halfBit9600Delay 42
#define bit4800Delay 188
#define halfBit4800Delay 94
byte rx = 0;
byte tx = 1;
byte SWval;
void setup() {
pinMode(rx,INPUT);
pinMode(tx,OUTPUT);
digitalWrite(tx,HIGH);
digitalWrite(13,HIGH); //turn on debugging LED
SWprint('h'); //debugging hello
SWprint('i');
SWprint(10); //carriage return
}
void SWprint(int data)
{
byte mask;
//startbit
digitalWrite(tx,LOW);
delayMicroseconds(bit9600Delay);
for (mask = 0x01; mask>0; mask <<= 1) {
if (data & mask){ // choose bit
digitalWrite(tx,HIGH); // send 1
}
else{
digitalWrite(tx,LOW); // send 0
}
delayMicroseconds(bit9600Delay);
}
//stop bit
digitalWrite(tx, HIGH);
delayMicroseconds(bit9600Delay);
}
int SWread()
{
byte val = 0;
while (digitalRead(rx));
//wait for start bit
if (digitalRead(rx) == LOW) {
delayMicroseconds(halfBit9600Delay);
for (int offset = 0; offset < 8; offset++) {
delayMicroseconds(bit9600Delay);
val |= digitalRead(rx) << offset;
}
//wait for stop bit + extra
delayMicroseconds(bit9600Delay);
delayMicroseconds(bit9600Delay);
return val;
}
}
void loop()
{
SWval = SWread();
SWprint(toupper(SWval));
}
我改变RX和TX引脚分别为0和1,因为这些是在Cutedigi的RS-232芯片使用的管脚。现在,当我打开一个终端窗口和类型的字符,我得到乱码的符号和字母。(是这样的:¾_ò_òòËÌßÌËßÌÊÌòyofyofsæóÙöÇAEUæ
)
根据该示例code网站上,如果键入 ABCDEFG
,终端窗口应该显示 ABCDEFG
According to the example code website, if I type abcdefg
, the terminal window should display ABCDEFG
.
为什么会变成这样?我已经设置波特率为9600,如草图规定,但我仍然得到的问题。重置Arduino的似乎并没有任何帮助 - 我仍然得到乱码
Why would this be the case? I have set the baud rate to 9600, as specified in the sketch, but I'm still getting issues. Resetting the Arduino doesn't seem to help either - I still get garbled text.
推荐答案
我想出什么问题了。
原来,我是想两个DCE设备连接在一起,这意味着空调制解调器适配器是需要交换电缆上的TX / RX引脚。 previously,我用一个简单的对接转换器,但是这是什么导致我的问题。
It turns out that I was trying to connect two DCE devices together, which means that a null modem adapter was necessary to swap the TX/RX pins on the cable. Previously, I was using a simple gender changer, but this is what was causing my problems.
尝试获得,如果你有这样的问题零调制解调器适配器。
Try getting a null modem adapter if you're having problems like this.
这篇关于使用一个Arduino Duemilanove和Cutedigi RS-232接口,RS-232通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!