Hello Guys,
I tried to combine some of the FEZ Panda II Functions and I have the following problem: I am using an Interrupt to count a number and I am using an endless loop to scan the analog input.
Now I want to send these informations via UART to the computer when the character “1” is received. But the system is throwing an exception when [quote]UART.open[/quote] is called.
Do you have any ideas?
This is my code. Quite simple but I can’t find the mistake.
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);
}
}
}
}