sultanuzzaman
Member
- Joined
- Sep 13, 2015
- Messages
- 18
- Programming Experience
- 1-3
I have a serial port controller (for sensing any object that passes through its transport module) connected to USB. It responses with either 0 or 1 after input command "t" when it senses any dropping or no dropping of object through the transport. The page CshDeposit5 contains the code. Start.xaml is the page the activity starts and ends up in CshDeposit5.xaml. TmpErrorMsg and TmpMsg are the pages to show the user what happened. Everything runs fine for the first time. But when it tries to come to this CshDeposit5.xaml 2nd time it throws exception on the follwoing highlighted codes:
CshDeposit5.xaml.cs
CshDeposit5.xaml.cs
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using System.Windows.Threading;
namespace NWCDM
{
public partial class CshDeposit5 : Page
{
private System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
private static int i;
public CshDeposit5()
{
InitializeComponent();
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
i = Global.xDropTime; //Stipulated Time for the object to pass through before timeout - declared in Global.cs
TimeLeft.Text = i.ToString();
dispatcherTimer.Tick += dispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
Global.xController.WriteLine("t"); //serialport declared in Global.cs file
dispatcherTimer.Start();
SerialPort sp2 = Global.xController; //Declared in Global.cs as serial port
sp2.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}
public void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Dispatcher.Invoke(DispatcherPriority.Send, new Action(() => DoTrans(indata)));
}
private void DoTrans(string x){
if (i >= 0)
{
if (x.IndexOf("1") >= 0)
{
dispatcherTimer.Stop();
Dispatcher.BeginInvoke(new System.Threading.ThreadStart(delegate
{
[B][COLOR=#ff0000] NavigationService.Navigate(new Uri("TmpMsg.xaml", UriKind.RelativeOrAbsolute)); //Throws Null exception 2nd time it comes here[/COLOR][/B]
}));
}
else
{
if (x.IndexOf("0") >= 0)
{
Dispatcher.BeginInvoke(new System.Threading.ThreadStart(delegate
{
[COLOR=#ff0000][B] NavigationService.Navigate(new Uri("TmpErrorMsg.xaml", UriKind.RelativeOrAbsolute)); [/B][/COLOR][B][COLOR=#FF0000]//Throws Null exception 2nd time it comes here[/COLOR][/B]
}));
}
}
}
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
if (i >= 0)
{
if (Global.VarLang == 0)
{
TimeLeft.Text = i.ToString();
}
i--;
}
else
{
dispatcherTimer.Stop();
Global.xController.WriteLine("r"); //Shutter Gate Closes after timeout
Dispatcher.BeginInvoke(new System.Threading.ThreadStart(delegate
{
NavigationService.Navigate(new Uri("TmpErrorMsg.xaml", UriKind.RelativeOrAbsolute)); [B][COLOR=#008000]//No Error whatsoever[/COLOR][/B]
}));
}
}
}
}
Last edited by a moderator: