使用 Arduino Duemilanove 和 Cutedigi RS-232 接口的 RS-232 通信 [英] RS-232 communication using an Arduino Duemilanove and the Cutedigi RS-232 interface

查看:26
本文介绍了使用 Arduino Duemilanove 和 Cutedigi RS-232 接口的 RS-232 通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在让我的 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 网站上找到了一些示例代码.这是我正在运行的确切代码:

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æóÙöÇ æü æ).

I changed the RX and TX pins to 0 and 1 respectively, because these are the pins that the Cutedigi RS-232 chip uses. Now, when I open a terminal window and type characters in, I get garbled symbols and letters (like this: ¾_ò_òòËÌßÌËßÌÊÌòyofyofsæóÙöÇ æü æ).

根据示例代码网站,如果我输入abcdefg,终端窗口应该显示ABCDEFG.

According to the example code website, if I type abcdefg, the terminal window should display ABCDEFG.

为什么会这样?我已将波特率设置为 9600,如 sketch 中所述,但我仍然有问题.重置 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.

推荐答案

我知道问题出在哪里了.

I figured out what the issue was.

事实证明,我试图将两个 DCE 设备连接在一起,这意味着需要一个空调制解调器适配器来交换电缆上的 TX/RX 引脚.以前,我使用的是简单的性别转换程序,但这正是导致我出现问题的原因.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆