Arduino与C#串口通信的问题
背景说明:
互动多媒体程序是用C#写的,Arduino只负责发送四个值,A,B,C,D。不接受任何来自C#程序的值。使用的板子是 Arduino ATMEGA328 或者ATMEGA2560,均为淘宝山寨板。Arduino烧录的代码如下:
unsigned long time;
int KeyA=0;
int KeyB=0;
int KeyA_Flag = 1;
int KeyB_Flag = 1;
int Hall1_Flag=1;
int Hall2_Flag=1;
int HallSensor1=1;
int HallSensor2=1;
void setup(){
Serial.begin(9600);
}
void loop(){
time = millis();
if((time%180000) == 0)
{
//Serial.print("undo");
// Serial.print("\n");
}
KeyA = digitalRead(4);
KeyB = digitalRead(5);
HallSensor1 = digitalRead(2);
HallSensor2 = digitalRead(3);
while(KeyA == 1)
{
KeyA = digitalRead(4);
if(KeyA == 1)
KeyA_Flag = 1;
else KeyA_Flag = 0;
}
if(KeyA_Flag==0){
KeyA_Flag = 1;
Serial.print("A");
delay(500);
}
while(KeyB==1){
KeyB=0;
Serial.print("B");
delay(500);
}
while(HallSensor1==0){
HallSensor1=digitalRead(2);
if(HallSensor1==0)
Hall1_Flag=1;
else Hall1_Flag=0;
}
if(Hall1_Flag==0){
Hall1_Flag=1;
Serial.print("C");
// delay(500);
}
while(HallSensor2==0){
HallSensor2=digitalRead(3);
if(HallSensor2==0)
Hall2_Flag=1;
else Hall2_Flag=0;
}
if(Hall2_Flag==0){
Hall2_Flag=1;
Serial.print("D");
// delay(500);
}
}
遇到的状况是:
在arduino-1.03下模拟测试一点问题都没有。但是在C#程序下,在按A,B,C,D值切换的时候很容易造成程序死机,而退出程序后重新打开程序后就OK了。
今天测试的时候发现一个情况,说明如下:ATMEGA328板子,正常情况下,往C#传值的时候,Arduino板子上的TX指示灯会闪一下。在arduino-1.0.3软件的模拟下一切正常。但是在C#程序中会出现 L指示灯(闪红灯)跟TX指示灯同时闪的情况,而且当A,B通信几次后就程序死机,再按A,B键TX灯也不闪了。退出C#程序后再运行的时候TX指示灯会闪烁一下(绿灯)。程序可以正常运行。 将板子换成ATMEGA2560后,L指示灯偶尔会闪黄灯,测试C#程序没有死机的情况,但偶尔会报“0X7c930cee"的内存错误。出现的几率比死机的情况低一半。再按A,B按钮TX指示灯正常闪烁。 请大神们指点,问题出在哪里,是板子芯片不稳定呢还是我的程序有问题,内存开销不够造成的死机?或者是其他问题,谢谢! 先用Arduino自带IDE的串口监视器看看 数据是否显示正常。
如果这个正常,那就是C#程序问题了。 另外Arduino程序中,加个delay(100); Ricky 发表于 2014-4-30 16:11
另外Arduino程序中,加个delay(100);
谢谢你的回复,问题不在这里。今天收到意大利原版的UDO板子,经测试没有死机的情况,跟ATMEGA2560出现的报错情况一样。这么说山寨板子还是有一定问题。剩下的内存报错问题,我从系统跟程序上着手找找! Ricky 发表于 2014-4-30 16:11
先用Arduino自带IDE的串口监视器看看 数据是否显示正常。
如果这个正常,那就是C#程序问题了。 ...
用Arduino自带IDE的串口监视器看,是正常的,但是一开程序测就出问题了,板子的TX指示灯也有变化。C#程序数据传输方面 可能有一定问题。 lyhlyly 发表于 2014-5-4 09:50
谢谢你的回复,问题不在这里。今天收到意大利原版的UDO板子,经测试没有死机的情况,跟ATMEGA2560出现的 ...
没有delay串口会堵塞。最好加点delay,会稳定很多。 另外可已把
Serial.begin(9600) 改成Serial.begin(115200) Ricky 发表于 2014-5-4 10:47
没有delay串口会堵塞。最好加点delay,会稳定很多。 另外可已把
Serial.begin(9600) 改成Serial.beg ...
改了行代码:清串口缓存,
串口比特率是跟C#统一的。delay 后边两个被我注释掉了,因为每次出问题都出在串口输出A,B上,C,D没有问题。
if((time%1000) == 0)
{
Serial.flush();
//Serial.print("undo");
// Serial.print("\n");
}
另外还有个问题请教下:一般的按钮,按照我贴出的代码,输出A的代码在按钮按下跟弹起都会有输出,输出B的代码 在按钮按下不放的情况下一直会有输出B,好像这个问题只能通过delay 加时间间隔解决。 可以参考这个代码:
/*
Debounce
Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
press), the output pin is toggled from LOW to HIGH or HIGH to LOW.There's
a minimum delay between toggles to debounce the circuit (i.e. to ignore
noise).
The circuit:
* LED attached from pin 13 to ground
* pushbutton attached from pin 2 to +5V
* 10K resistor attached from pin 2 to ground
* Note: On most Arduino boards, there is already an LED on the board
connected to pin 13, so you don't need any extra components for this example.
created 21 November 2006
by David A. Mellis
modified 30 Aug 2011
by Limor Fried
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/Debounce
*/
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin =13; // the number of the LED pin
// Variables will change:
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;// the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH),and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
buttonState = reading;
}
// set the LED using the state of the button:
digitalWrite(ledPin, buttonState);
// save the reading.Next time through the loop,
// it'll be the lastButtonState:
lastButtonState = reading;
} 无论arduino如何,C#一定写的有问题。
页:
[1]