Serial port 使用Arduino Duemilanove和Cutedigi RS-232接口的RS-232通信
让我的Arduino微控制器读取信号有点困难。我的项目要求我读取空气质量监测器输出的数据 我的组件:Serial port 使用Arduino Duemilanove和Cutedigi RS-232接口的RS-232通信,serial-port,arduino,Serial Port,Arduino,让我的Arduino微控制器读取信号有点困难。我的项目要求我读取空气质量监测器输出的数据 我的组件: 为了测试串行通信是否正常工作,我在Arduino网站上找到了一些示例代码。这正是我正在运行的代码: //Created August 23 2006 //Heather Dewey-Hagborg //http://www.arduino.cc #include <ctype.h> #define bit9600Delay 84 #define halfBit9600De
//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));
}
//创建于2006年8月23日
//希瑟·杜威·哈伯格
//http://www.arduino.cc
#包括
#定义位9600Delay 84
#定义半位9600Delay 42
#定义位4800延迟188
#定义半位4800Delay 94
字节rx=0;
字节tx=1;
字节SWval;
无效设置(){
pinMode(接收、输入);
pinMode(发送、输出);
数字写入(tx,高);
digitalWrite(13,高);//打开调试指示灯
SWprint('h');//调试您好
SWprint(‘i’);
SWprint(10);//回车
}
无效SWprint(整型数据)
{
字节掩码;
//开始
数字写入(tx,低电平);
延迟微秒(位9600Delay);
对于(mask=0x01;mask>0;mask我找出了问题所在
事实证明,我试图将两个DCE设备连接在一起,这意味着需要一个空调制解调器适配器来交换电缆上的TX/RX引脚。以前,我使用的是一个简单的性别转换器,但这正是导致我出现问题的原因
如果您遇到这样的问题,请尝试使用空调制解调器适配器。虽然这个问题肯定有一些软件开发角度,但它可能更适合您[好的,谢谢,那就删除这篇文章吧。我会在electronics.stackexchange.com中查找,如果你的字符有乱码,这通常表示波特率不匹配。要么是这样,要么启动时的同步是由噪声触发的。我会在半位暂停后添加一个去盎司循环,或者重新测试“if(digitalRead(rx)=LOW)”。