FEZ Panda II UART mit Interrupt

OP #2722682
Lesenswert?

Hallo Leute

Ich versuche, einige der FWZ Panda II-Funktionen zu kombinieren und ich 
habe folgendes Problem:Ich verwende ein Interrupt, um eine Zahl zu 
zählen um die in einer Endlosschleife an einen Eingang zu senden.Jetzt 
möchte ich diese Informationen per UART an den Computer senden, wenn das 
Zeichen "1" empfangen wird. Aber das System ist eine Ausnahme 
auszulösen, wenn
UART.open
aufgerufen wird.
Haben ihr irgendwelche Ideen?

Dies ist mein Code. Ganz einfach, aber ich kann den Fehler nicht finden.

using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
using System.IO.Ports;
using System.Text;

namespace MFConsoleApplication1
{

public class Program
{

public static class GlobalVar
{
static int _globalValue;

public static int GlobalValue
{
get
{
return _globalValue;
}

set
{
_globalValue = value;
}
}
}


public static void Main()
{

GlobalVar.GlobalValue = 0;

// the pin will generate interrupt on high and low edges
InterruptPort IntButton =
new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.LDR, true,
Port.ResistorMode.PullUp,
Port.InterruptMode.InterruptEdgeLow);



// add an interrupt handler to the pin
IntButton.OnInterrupt +=
new NativeEventHandler(IntButton_OnInterrupt);

//do anything you like here
// Thread.Sleep(Timeout.Infinite);

AnalogIn voltagePort = new AnalogIn((AnalogIn.Pin)FEZ_Pin.AnalogIn.An3);
OutputPort lowPort = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.An0, 
false);
OutputPort highPort = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.An2, 
true);

const double maxVoltage = 3.3;
const int maxAdcValue = 1023;


InterruptPort RX =
new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.Di0, true,
Port.ResistorMode.PullUp,
Port.InterruptMode.InterruptEdgeLow);

RX.OnInterrupt +=
new NativeEventHandler(RX_OnInterrupt);

while (true)
{
double mess = 0;
int sample = 10000;
for (int i = 0; i <= (sample - 1); i++)
{
mess += (double)voltagePort.Read();
}
double valueRaw = mess / sample;
double valueVoltage = (valueRaw * maxVoltage) / maxAdcValue;
double valuePercent = (valueVoltage / maxVoltage) * 100;

Debug.Print("Voltage: " + valueVoltage + "V");
Debug.Print("Percent: " + valuePercent + "%");

//Thread.Sleep(1000);

}
}

static void IntButton_OnInterrupt(uint port, uint state,
DateTime time)
{
GlobalVar.GlobalValue++;
string zaehler_string = GlobalVar.GlobalValue.ToString();
Debug.Print(zaehler_string);
}



static void RX_OnInterrupt(uint port, uint state,
DateTime time)
{
// Initial Daten
SerialPort UART = new SerialPort("COM1", 115200);
int read_count = 0;
byte[] tx_data;
byte[] rx_data = new byte[10];
UART.ReadTimeout = 0;
UART.Open();

read_count = UART.Read(rx_data, 0, rx_data.Length);
string empfangen = rx_data.ToString();
string zaehler_string = GlobalVar.GlobalValue.ToString();
if (empfangen == "1")
{
tx_data = Encoding.UTF8.GetBytes(zaehler_string);
UART.Write(tx_data, 0, tx_data.Length);
}


}
}
}

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren