Symbol.WPAN.Bluetooth example that transfers data - c#

I am trying to use Symbol.WPAN.Bluetooth that comes with the EMDK for Symbol devices.
Does anyone happen to have a working example that transfers data?
Symbol's example just pairs the devices. (They apparently think that transfering data is not really needed in a Personal Area network example.)
Anyway, I know this is a long shot, but if anyone has gotten this to work I would love to see some code.
This is what I have tried. I have one device press button1 and another device press button2. The read value is always a zero length byte array.
using System.Text;
using System.Windows.Forms;
using Symbol.WPAN;
using Symbol.WPAN.Bluetooth;
namespace SmartDeviceProject1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Bluetooth bluetooth = new Bluetooth();
if (bluetooth.IsEnabled != true)
{
bluetooth.Enable();
bluetooth.RadioMode = BTH_RADIO_MODE.BTH_DISCOVERABLE_AND_CONNECTABLE;
}
RemoteDevice connectedDevice = null;
foreach (RemoteDevice remoteDevice in MakeEnumerable(bluetooth.RemoteDevices))
{
if ((remoteDevice.Name == "WM_Dan") && (remoteDevice.IsPaired == false))
{
remoteDevice.Pair();
connectedDevice = remoteDevice;
}
}
string test;
test = "Testing this out";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] encTest = encoding.GetBytes(test);
if (connectedDevice != null)
{
connectedDevice.WriteTimeout = 20000;
connectedDevice.Write(encTest);
}
}
public static IEnumerable<RemoteDevice> MakeEnumerable(RemoteDevices devices)
{
for (var i = 0; i < devices.Length; i++)
{
yield return devices[i];
}
}
private void button2_Click(object sender, EventArgs e)
{
Bluetooth bluetooth = new Bluetooth();
if (bluetooth.IsEnabled != true)
{
bluetooth.Enable();
bluetooth.RadioMode = BTH_RADIO_MODE.BTH_DISCOVERABLE_AND_CONNECTABLE;
}
RemoteDevice connectedDevice = null;
foreach (RemoteDevice remoteDevice in MakeEnumerable(bluetooth.RemoteDevices))
{
if ((remoteDevice.Name == "WM_Dan2") && (remoteDevice.IsPaired == false))
{
remoteDevice.Pair();
connectedDevice = remoteDevice;
}
}
string test;
test = "Testing this out";
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] encTest = encoding.GetBytes(test);
byte[] encTest2;
string test2;
if (connectedDevice != null)
{
connectedDevice.ReadTimeout = 20000;
encTest2 = connectedDevice.Read(encTest.Length);
test2 = encoding.GetString(encTest2, 0, encTest2.Length);
MessageBox.Show(test2);
}
}
}
}

I gave up on using the built in com port connection and opened a SerialPort object on the connection.
SerialPort sp = new SerialPort();
sp.PortName = "COM" + connectedDevice.LocalComPort.ToString();
sp.BaudRate = 9600;
sp.DataBits = 8;
sp.Parity = Parity.None;
sp.StopBits = StopBits.One;
sp.Open();
sp.Open();
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
sp.ErrorReceived += new SerialErrorReceivedEventHandler(sp_ErrorReceived);
sp.WriteLine(textBoxSend.Text);
I also found that even though their docs said that LocalComPort was auto assigned, this was not always the truth. It was best to use their BTExplorer to set it first.
As well, there OpenComPort would work in situations where is should not -- using Reflector it is pretty obviously wrong. There are checking the return of ::CreateFile("COM" + port...) against 0 instead of -1 (INVALID_HANDLE_VALUE)

I don't know if this can ever help anyone, but here is an old piece of code that I wrote a few years back.
You'll have to clean it up so that it works for your application. My app had a TextBox control that it read from and logged errors to a Global class. Change that to work with what you have, and it should basically be good.
static class Scanner {
const string _CODEFILE = "Scanner.cs - Scanner::";
static int _baud = 9600;
static int _bits = 8;
static string _dataIn = null;
static string _port = "COM1";
static Parity _parity = Parity.None;
static StopBits _stop = StopBits.One;
static SerialPort _com1 = null;
static TextBox _textbox = null;
public enum ControlType { None, BadgeID, PartNumber, SerialNumber, WorkOrder };
static ControlType _control;
public static bool Available { get { return ((_com1 != null) && (_com1.IsOpen)); } }
public static bool Close {
get {
if (_com1 == null) return true;
try {
if (_com1.IsOpen) {
_com1.Close();
}
return (!_com1.IsOpen);
} catch { }
return false;
}
}
public static string Open() {
const string na = "Not Available";
if (_com1 == null) {
string reset = Reset();
if (!String.IsNullOrEmpty(reset)) return reset;
}
try {
_com1.Open();
return (_com1.IsOpen) ? null : na;
} catch (Exception err) {
return err.Message;
}
}
static void ProcessData(string incoming) {
_dataIn += incoming;
if ((_control != ControlType.None) && (_textbox != null)) {
bool ok = false;
string testData = _dataIn.Trim();
switch (_control) {
case ControlType.BadgeID:
if (testData.Length == 6) {
if (testData != BarCode.LOGOFF) {
Regex pattern = new Regex(#"[0-9]{6}");
ok = (pattern.Matches(testData).Count == 1);
} else {
ok = true;
}
}
break;
case ControlType.PartNumber:
if (testData.Length == 7) {
Regex pattern = new Regex(#"[BCX][P057][0-9]{5}");
ok = (pattern.Matches(testData).Count == 1);
}
break;
case ControlType.SerialNumber:
if (testData.Length == 15) {
Regex pattern = new Regex(#"[BCX][P057][0-9]{5} [0-9]{4} [0-9]{2}");
ok = (pattern.Matches(testData).Count == 1);
}
break;
case ControlType.WorkOrder:
if (testData.Length == 6) {
Regex pattern = new Regex(#"[0-9]{6}");
ok = (pattern.Matches(testData).Count == 1);
}
break;
}
if (ok) {
_textbox.Text = testData;
_textbox.ScrollToCaret();
_dataIn = null;
}
}
}
static string Reset() {
if (_com1 != null) {
try {
if (_com1.IsOpen) {
_com1.DiscardInBuffer();
_com1.Close();
}
} catch (Exception err) {
return err.Message;
}
Global.Dispose(_com1);
_com1 = null;
}
try {
_com1 = new SerialPort(_port, _baud, _parity, _bits, _stop);
_com1.DataReceived += new SerialDataReceivedEventHandler(Serial_DataReceived);
_com1.Open();
} catch (Exception err) {
return err.Message;
}
return null;
}
public static void ScanSource(ref TextBox objTextBox, ControlType objType) {
_textbox = objTextBox;
_control = objType;
_dataIn = null;
}
static void Serial_DataReceived(object sender, SerialDataReceivedEventArgs e) {
ProcessData(_com1.ReadExisting());
}
public static void Settings(string ComPort, int BaudRate, Parity ParityValue, int Bits, StopBits StopBit) {
_port = ComPort;
_baud = BaudRate;
_parity = ParityValue;
_bits = Bits;
_stop = StopBit;
}
/// <summary>
/// Closes the COM Port
/// COM Port routines are ready to add as soon as I am
/// </summary>
static bool ComPortClose {
get {
if (_com1 == null) ComPortReset();
return ((_com1 == null) ? true : _com1.IsOpen ? false : true);
}
set {
if (_com1 == null) ComPortReset();
else if (_com1.IsOpen) {
_com1.DiscardInBuffer();
_com1.Close();
}
}
}
/// <summary>
/// Opens the COM Port
/// </summary>
static bool ComPortOpen {
get {
if (_com1 == null) ComPortReset();
return (_com1 == null) ? false : _com1.IsOpen;
}
set {
if (_com1 == null) ComPortReset();
if ((_com1 != null) && (!_com1.IsOpen)) _com1.Open();
}
}
/// <summary>
/// Initialized the Serial Port on COM1
/// </summary>
static void ComPortReset() {
if ((_com1 != null) && (_com1.IsOpen)) {
_com1.Close();
_com1 = null;
}
try {
_com1 = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
} catch (IOException err) {
Global.LogError(_CODEFILE + "ComPortReset", err);
}
}
}

Related

How to find items in a ListBox?

I am creating an application where I have a ListBox which has items that are read in from a text file using StreamReader. I have created a search form but I'm not sure what to do next. Can anyone give me some suggestions please? Here is my code:
My code for the ListBox (sorry it's so long)
public partial class frmSwitches : Form
{
public static ArrayList switches = new ArrayList();
public static frmSwitches frmkeepSwitches = null;
public static string inputDataFile = "LeckySafe.txt";
const int numSwitchItems = 6;
public frmSwitches()
{
InitializeComponent();
frmkeepSwitches = this;
}
private void btnDevices_Click(object sender, EventArgs e)
{
frmDevices tempDevices = new frmDevices();
tempDevices.Show();
frmkeepSwitches.Hide();
}
private bool fileOpenForReadOK(string readFile, ref StreamReader dataIn)
{
try
{
dataIn = new StreamReader(readFile);
return true;
}
catch (FileNotFoundException notFound)
{
MessageBox.Show("ERROR Opening file (when reading data in) - File could not be found.\n"
+ notFound.Message);
return false;
}
catch (Exception e)
{
MessageBox.Show("ERROR Opening File (when reading data in) - Operation failed.\n"
+ e.Message);
return false;
}
}
private bool getNextSwitch(StreamReader inNext, string[] nextSwitchData)
{
string nextLine;
int numDataItems = nextSwitchData.Count();
for (int i = 0; i < numDataItems; i++)
{
try
{
nextLine = inNext.ReadLine();
if (nextLine != null)
nextSwitchData[i] = nextLine;
else
{
return false;
}
}
catch (Exception e)
{
MessageBox.Show("ERROR Reading from file.\n" + e.Message);
return false;
}
}
return true;
}
private void readSwitches()
{
StreamReader inSwitches = null;
Switch tempSwitch;
bool anyMoreSwitches = false;
string[] switchData = new string[numSwitchItems];
if (fileOpenForReadOK(inputDataFile, ref inSwitches))
{
anyMoreSwitches = getNextSwitch(inSwitches, switchData);
while (anyMoreSwitches == true)
{
tempSwitch = new Switch(switchData[0], switchData[1], switchData[2], switchData[3], switchData[4], switchData[5]);
switches.Add(tempSwitch);
anyMoreSwitches = getNextSwitch(inSwitches, switchData);
}
}
if (inSwitches != null) inSwitches.Close();
}
public static bool fileOpenForWriteOK(string writeFile, ref StreamWriter dataOut)
{
try
{
dataOut = new StreamWriter(writeFile);
return true;
}
catch (FileNotFoundException notFound)
{
MessageBox.Show("ERROR Opening file (when writing data out)" +
"- File could not be found.\n" + notFound.Message);
return false;
}
catch (Exception e)
{
MessageBox.Show("ERROR Opening File (when writing data out)" +
"- Operation failed.\n" + e.Message);
return false;
}
}
public static void writeSwitches()
{
StreamWriter outputSwitches = null;
if (fileOpenForWriteOK(inputDataFile, ref outputSwitches))
{
foreach (Switch currSwitch in switches)
{
outputSwitches.WriteLine(currSwitch.getSerialNo());
outputSwitches.WriteLine(currSwitch.getType());
outputSwitches.WriteLine(currSwitch.getInsDate());
outputSwitches.WriteLine(currSwitch.getElecTest());
outputSwitches.WriteLine(currSwitch.getPatId());
outputSwitches.WriteLine(currSwitch.getNumDevice());
}
outputSwitches.Close();
}
if (outputSwitches != null) outputSwitches.Close();
}
private void showListOfSwitches()
{
lstSwitch.Items.Clear();
foreach (Switch b in switches)
lstSwitch.Items.Add(b.getSerialNo()
+ b.getType() + b.getInsDate()
+ b.getElecTest() + b.getPatId() + b.getNumDevice());
}
My code for the search form:
private void btnSearch_Click(object sender, EventArgs e)
{
frmSearchSwitch tempSearchSwitch = new frmSearchSwitch();
tempSearchSwitch.Show();
frmkeepSwitches.Hide();
}
If using a List<T> and lambda is not possible for you then go no farther.
Here I use a List<T> for the data source of a ListBox and mocked up data as where the data comes from is not important but here I am focusing on searching on a property within a class where a select is used to index the items then the where searches in this case) for a specific item, if found the index is used to move to the item. No code present for if not located as this is easy for you to do if the code here is something that is doable for you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace ListBoxSearch_cs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
var results =
((List<Item>)ListBox1.DataSource)
.Select((data, index) => new
{ Text = data.SerialNumber, Index = index })
.Where((data) => data.Text == "BB1").FirstOrDefault();
if (results != null)
{
ListBox1.SelectedIndex = results.Index;
}
}
private void Form1_Load(object sender, EventArgs e)
{
var items = new List<Item>() {
new Item {Identifier = 1, SerialNumber = "AA1", Type = "A1"},
new Item {Identifier = 2, SerialNumber = "BB1", Type = "A1"},
new Item {Identifier = 3, SerialNumber = "CD12", Type = "XD1"}
};
ListBox1.DisplayMember = "DisplayText";
ListBox1.DataSource = items;
}
}
/// <summary>
/// Should be in it's own class file
/// but done here to keep code together
/// </summary>
public class Item
{
public string SerialNumber { get; set; }
public string Type { get; set; }
public int Identifier { get; set; }
public string DisplayText
{
get
{
return SerialNumber + " " + this.Type;
}
}
}
}

C# Using SoundPlayer to Pause and Resume

I am another person dabbling with C# and wanting to create a simple audio application that plays wav files loaded into the program via an upload program. My issue is that I need to get whatever audio file currently being played to pause with the track timer of the audio file being used when I start the audio file again via my Play button. I already have a global timer, 'baseTimer', that I think I can use to set the audio file, that was stopped, track duration point. However I do not know how to accomplish this nor do I really know how to use all of the mci commands yet.
I have displayed all of my code for my main application... I also have read that I may need to utilize threading, but I've also read that it would be impossible to set an audio files track duration with a thread.
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
System.Timers.Timer baseTimer = new System.Timers.Timer();
List<string> PlayList = new List<string>();
List<byte> PlayList_byte;
int soundNum = 0;
private string music_PATH { get; set; }
private string talk_PATH { get; set; }
private byte Pause_TIME { get; set; }
private string Pause_RADIO { get; set; }
bool isStopped = new bool();
bool isPaused = new bool();
[DllImport("winmm.dll")]
private static extern uint mciSendString(string command, StringBuilder returnValue, int returnLength, IntPtr winHandle);
public static int GetSoundLength(string fileName)
{
StringBuilder lengthBuf = new StringBuilder(32);
mciSendString(string.Format("open \"{0}\" type waveaudio alias wave", fileName), null, 0, IntPtr.Zero);
mciSendString("status wave length", lengthBuf, lengthBuf.Capacity, IntPtr.Zero);
mciSendString("close wave", null, 0, IntPtr.Zero);
int length = 0;
int.TryParse(lengthBuf.ToString(), out length);
return length;
}
private void SetPath()
{
music_PATH = #"..\\..\\commercial\list.txt";
talk_PATH = #"..\\..\\main\list.txt";
StreamReader myReader;
using (myReader = new StreamReader(music_PATH))
{
while (myReader.Peek() != -1)
{
string read = myReader.ReadLine();
PlayList.Add(read);
}
}
using (myReader = new StreamReader(talk_PATH))
{
while (myReader.Peek() != -1)
{
string read = myReader.ReadLine();
PlayList.Add(read);
}
myReader.Close();
}
foreach (string sound in PlayList)
{
soundNum++;
}
}
private string CurrentSound()
{
try
{
Random _randx = new Random();
int pick = _randx.Next(0, soundNum);
string currentaudio = PlayList[pick];
Pause_RADIO = currentaudio;
return currentaudio;
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
return null;
}
string _SelectedSound = "";
private string _Sound(string currentradio, string pattern)
{
foreach (Match entry in Regex.Matches(currentradio, pattern))
{
_SelectedSound = entry.Value.ToString();
}
if (_SelectedSound == "music")
{
return "commercial";
}
else if (_SelectedSound == "talk")
{
return "main";
}
return null;
}
private void _SetTimer(string currentradio, string pattern)
{
baseTimer.Interval = GetSoundLength(#"..\\..\\" + pattern + #"\" + currentradio);
}
private bool isRepeat(string lastradio, string currentradio)
{
if (lastradio == currentradio)
{
return true;
}
else
{
return false;
}
}
private void baseTimerElasped(object sender, ElapsedEventArgs e)
{
Radio.FrmMain play = new Radio.FrmMain();
play.PlayPlayer();
}
private void PlayPlayer()
{
MediaPlayer wavplayer;
try
{
if (soundNum == 0)
{
SetPath();
PlayPlayer();
}
else
{
string currentradio = CurrentSound();
bool localcheck = isRepeat(_SelectedSound, currentradio);
if (localcheck == true)
{
PlayPlayer();
}
else
{
string Pattern = #"(music|talk)";
string selected = _Sound(currentradio, Pattern);
_SetTimer(currentradio, selected);
switch (selected)
{
case "commercial":
music_PATH = #"..\\..\\commercial\";
PlayList_byte = new List<byte>(File.ReadAllBytes(music_PATH + currentradio));
wavplayer = new MediaPlayer(PlayList_byte.GetRange(0, PlayList_byte.Count).ToArray());
wavplayer.Play();
baseTimer.Start();
break;
case "main":
talk_PATH = #"..\\..\\main\";
PlayList_byte = new List<byte>(File.ReadAllBytes(talk_PATH + currentradio));
wavplayer = new MediaPlayer(PlayList_byte.GetRange(0, PlayList_byte.Count).ToArray());
wavplayer.Play();
baseTimer.Start();
break;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message + ex.StackTrace + ex.Source);
}
}
private void PausePlayer()
{
MediaPlayer wavplayer = new MediaPlayer(PlayList_byte.GetRange(0, PlayList_byte.Count).ToArray());
baseTimer.Stop();
MessageBox.Show("Count: " + PlayList_byte.Count + "Pause_TIME: " + Pause_TIME + "\nPlaylist_byte" + PlayList_byte.ToString());
try
{
switch (isPaused)
{
case false:
isPaused = true;
wavplayer.Stop();
break;
case true:
isPaused = false;
string localcheck = _Sound(Pause_RADIO, #"(music|talk)");
switch (localcheck)
{
case "commercial":
music_PATH = #"..\\..\\commercial\";
wavplayer.Play(PlayList_byte.GetRange(PlayList_byte.Count - Pause_TIME, PlayList_byte.Count - Pause_TIME).ToArray());
break;
case "main":
talk_PATH = #"..\\..\\main\";
wavplayer.Play(PlayList_byte.GetRange(PlayList_byte.Count - Pause_TIME, PlayList_byte.Count - Pause_TIME).ToArray());
break;
}
break;
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message + ex.Data);
}
}
private void btnPlay_Click(object sender, EventArgs e)
{
switch (isStopped)
{
case false:
isStopped = true;
btnPlay.Image = Image.FromFile(#"..\\..\\Pause.png");
lblPlay.Text = "Pause";
if (isPaused == false)
{
PlayPlayer();
}
else
{
PausePlayer();
}
break;
case true:
isStopped = false;
btnPlay.Image = Image.FromFile(#"..\\..\\Play.png");
lblPlay.Text = "Play";
PausePlayer();
break;
}
baseTimer.Elapsed += new ElapsedEventHandler(baseTimerElasped);
}
private void btnNext_Click(object sender, EventArgs e)
{
PlayPlayer();
}
private void btnStop_Click(object sender, EventArgs e)
{
MediaPlayer wavplayer = new MediaPlayer();
wavplayer.Stop();
baseTimer.Stop();
}
private void btnQuit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btnGetUpload_Click(object sender, EventArgs e)
{
Uploader FrmUpload = new Uploader();
FrmUpload.Show();
this.Hide();
}
}
class MediaPlayer
{
SoundPlayer wavplayer;
public MediaPlayer()
{
Stop();
}
public MediaPlayer(byte[] buffer)
{
MemoryStream memStream = new MemoryStream(buffer, true);
wavplayer = new SoundPlayer(memStream);
}
public void Play()
{
wavplayer.Play();
}
public void Play(byte[] buffer)
{
wavplayer.Stream.Seek(0, SeekOrigin.Begin);
wavplayer.Stream.Write(buffer, 0, buffer.Length);
wavplayer.Play();
}
public void Stop()
{
wavplayer.Stop();
}
}
Edit:
For clarity currentaudio has a file as such "music3.wav" or "talk1.wav" in it.
Super old question but just for anyone looking here's what worked for me:
double currentPos = audioplayer.CurrentPosition;
audioplayer.Play();
audioplayer.Seek(currentPos);
SoundPlayer is extremely limited, and doesn't support Pause and Resume. Are you able to use the WPF MediaElement instead? You will find it is much more powerful, supporting playing files of many types, repositioning, setting volume, pausing and resuming. You can also use it to get the file length instead of mci.

Why the number of threads is continuously increasing with SerialPort.CatchReceivedEvents()

I´m running a C# application with .NET Framework 2.0 to read data from SerialPort to get the weight from a scale.
The application works fine, does what it is supposed to do, but the number of threads keeps increasing and more memory is consumed until the application crashes, usually after around 4 hours.
When running with a serialport simulator the number of threads is stable around 30. But when I use an actual scale it goes greater than 500 threads.
I used Microsoft Managed Stack Explorer 1.0 to take a dump of the threads and almost all of them have exactly the following stack:
0. System.IO.Ports.SerialPort.CatchReceivedEvents (Source Unavailable)
1. System.IO.Ports.SerialStream.EventLoopRunner.CallReceiveEvents (Source Unavailable)
2. System.Threading._ThreadPoolWaitCallback.WaitCallback_Context (Source Unavailable)
3. System.Threading.ExecutionContext.Run (Source Unavailable)
4. System.Threading._ThreadPoolWaitCallback.PerformWaitCallbackInternal (Source Unavailable)
5. System.Threading._ThreadPoolWaitCallback.PerformWaitCallback (Source Unavailable)
I'm not able to identify the reason these threads are being created. Does anybody have any idea of what I'm missing here? Thanks!
This is my code:
Scale.cs -> creates a thread when method open() is called. The thread reads the value from getWeight().
Scales.cs -> treats events from serial port in method SerialPort_DataReceived(...). It's where m_SerialPort.ReadLine() is called and ends up providing the value to getWeight().
Scale.cs:
using System;
using System.Collections.Generic;
using System.Threading;
using ScalesGSS;
using StateMachine.Exceptions;
using StateMachine.Log;
using StateMachine.MessageOutput;
namespace StateMachine.DriverImplementation
{
class Scale : AScale
{
private const int Scale_version = 1;
private Thread thread = null;
private IScales gScale = null;
//
private string m_Type;
private string m_PortName;
private int m_BaudRate;
private char m_Parity;
private int m_DataBits;
private string m_StopBits;
private int m_CommandReturnLength;
private string m_CommandType;
private string m_CommandValue;
private int m_ReadTimeOutInMilliseconds;
private int m_WeightInitialPosition;
private int m_WeightFinalPosition;
private int m_TimeBetweenReadsInMilliseconds;
private int m_StableReadQuantity;
private int m_MinimumWeight;
private int m_ScaleID;
//
private double m_OldWeight = 0.0;
private double m_Offset = 0.0;
private double m_CurrentWeight = 0.0;
int m_WeightEqualCount = 0;
//
byte m_Status = 3; // "NO COMMUNICATION"
//
private bool m_Closed = false;
private static LogFactory m_Log = new LogFactory(LogCategory.Device, "");
ErrorDialog m_ErrorDialog = new ErrorDialog();
public Scale()
{
this.setClassName("Scale");
this.setDeviceType(DeviceType.Scale);
}
public void run()
{
try
{
if (this.m_Type.ToUpper().Equals("GENERICSCALES")) // GENERICSCALES or MOCKSCALES
this.gScale = new ScalesGSS.GenericScales();
else
this.gScale = new ScalesGSS.MockScales();
this.gScale.PortName = this.m_PortName;
this.gScale.BaudRate = this.m_BaudRate;
this.gScale.Parity = this.m_Parity.ToString();
this.gScale.DataBits = this.m_DataBits;
this.gScale.StopBits = this.m_StopBits;
this.gScale.CommandReturnLength = this.m_CommandReturnLength;
this.gScale.CommandType = this.m_CommandType;
this.gScale.CommandValue = this.m_CommandValue;
this.gScale.ReadTimeOut = this.m_ReadTimeOutInMilliseconds;
this.gScale.WeightInitialPosition = this.m_WeightInitialPosition;
this.gScale.WeightFinalPosition = this.m_WeightFinalPosition;
this.gScale.setParameters();
this.gScale.configurePort();
while (true)
{
if (this.m_Closed)
{
if (this.OpenedPort())
this.gScale.closePort();
break;
}
Thread.Sleep(this.m_TimeBetweenReadsInMilliseconds);
if (!this.OpenedPort())
{
if (!this.OpenPort())
{
m_Log.writeLogWarning("Error opening serialport.", " Port: " + this.m_PortName, true);
}
}
if (this.ErrorReadingWeight())
{
m_Log.writeLogWarning("Invalid weight.", " Port: " + this.m_PortName, true);
}
this.m_CurrentWeight = getWeight();
if (!ReadingTimeout())
{
if (this.m_WeightEqualCount > m_StableReadQuantity)
{
if (m_CurrentWeight > m_MinimumWeight)
m_Status = 2; // "WEIGHT STABLE"
else
{
m_Status = 0; // "SCALE FREE"
m_WeightEqualCount = 0;
}
}
else
{
if (m_CurrentWeight > m_MinimumWeight)
{
m_Status = 1; // "STABILIZING"
if ((this.m_CurrentWeight >= (this.m_OldWeight - this.m_Offset)) && (this.m_CurrentWeight <= (this.m_OldWeight + this.m_Offset)))
this.m_WeightEqualCount++;
else
this.m_WeightEqualCount = 0;
this.m_OldWeight = this.m_CurrentWeight;
}
else
{
m_Status = 0; // "SCALE FREE"
m_WeightEqualCount = 0;
}
}
}
else
{
m_WeightEqualCount = 0;
m_Status = 3; // "NO COMMUNICATION"
string v_Message = "No communication with scale. Port: " + m_PortName;
m_Log.writeLogWarning(v_Message, "", true);
AutoClosingMessageBox.Show(v_Message, "Scale", 10000);
}
}
}
catch (Exception v_Exception)
{
m_Log.writeLogError("run()", v_Exception);
}
}
private bool OpenedPort()
{
return this.gScale.OpenedPort;
}
private bool OpenPort()
{
bool v_OpenPort;
v_OpenPort = this.gScale.openPort();
if (!v_OpenPort)
{
m_ErrorDialog.getScaleErrorMessage(gScale);
}
return v_OpenPort;
}
private bool ErrorReadingWeight()
{
return this.gScale.ErrorReadingWeight;
}
private double getWeight()
{
return this.gScale.getWeight();
}
private DateTime LastGoodReading()
{
return gScale.LastGoodReading;
}
private void setLastGoodReading(DateTime p_Value)
{
gScale.LastGoodReading = p_Value;
}
private bool ReadingTimeout()
{
if (m_ReadTimeOutInMilliseconds > 0)
{
DateTime v_LastGoodReading = LastGoodReading() == DateTime.MinValue ? DateTime.Now : LastGoodReading();
setLastGoodReading(DateTime.Now);
return DateTime.Now > v_LastGoodReading.AddMilliseconds(m_ReadTimeOutInMilliseconds);
}
else
return false;
}
#region "IDriverService"
public override byte getStatus()
{
return m_Status;
}
public override byte[] read()
{
return System.Text.ASCIIEncoding.ASCII.GetBytes(m_CurrentWeight.ToString());
}
public override byte[] read(int p_InitialPosition, int p_Size)
{
return read();
}
public override byte[] write(byte[] p_Data)
{
string v_Temp = System.Text.ASCIIEncoding.ASCII.GetString(p_Data);
if (v_Temp.Equals("getScaleNumber"))
return System.Text.ASCIIEncoding.ASCII.GetBytes(m_ScaleID.ToString());
else
throw new EDriverAccess(1, "Not implemented");
}
public override bool open()
{
this.thread = new Thread(run);
this.thread.Name = "SCALE";
this.thread.IsBackground = true;
this.thread.Start();
return true;
}
public override bool close()
{
try
{
this.release();
return true;
}
catch
{
return false;
}
}
public override int getVersion()
{
return Scale_version;
}
public override void setProperties(Dictionary<string, string> p_props)
{
try
{
this.m_Type = p_props["type"];
this.m_PortName = p_props["portName"];
this.m_BaudRate = Int32.Parse(p_props["baudRate"]);
this.m_Parity = char.Parse(p_props["parity"]);
this.m_DataBits = Int32.Parse(p_props["dataBits"]);
this.m_StopBits = p_props["stopBits"];
this.m_CommandReturnLength = Int32.Parse(p_props["returnLength"]);
this.m_CommandType = p_props["commandType"];
this.m_CommandValue = p_props["commandValue"];
this.m_ReadTimeOutInMilliseconds = Int32.Parse(p_props["readTimeout"]);
this.m_WeightInitialPosition = Int32.Parse(p_props["weightInitPos"]);
this.m_WeightFinalPosition = Int32.Parse(p_props["weightFinPos"]);
this.m_TimeBetweenReadsInMilliseconds = Int32.Parse(p_props["delayLeitura"]);
this.m_StableReadQuantity = Int32.Parse(p_props["qtdeLeituraEstavel"]);
this.m_MinimumWeight = Int32.Parse(p_props["pesoMinimo"]);
this.m_ScaleID = Int32.Parse(p_props["numBalanca"]);
if (p_props.ContainsKey("precision"))
this.m_Offset = Int32.Parse(p_props["precision"]);
}
catch (Exception)
{
throw new Exception();
}
}
public override void release()
{
this.m_Closed = true;
m_Status = 3; // "NO COMMUNICATION"
}
#endregion
}
}
Scales.cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Reflection;
using System.Timers;
using Scales.Util;
namespace Scales.DLL
{
public class Scales : Status
{
public event EventHandler StableWeightChanged;
protected virtual void OnCountdownCompleted(EventArgs e)
{
if (StableWeightChanged != null)
StableWeightChanged(this, e);
}
System.Timers.Timer timerTimeWithoutSample;
private int m_IntervalsWithoutSample = 0;
private string m_EndOfWeightChar = "";
private void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
m_IntervalsWithoutSample++;
}
public int IntervalsWithoutSample { get { return m_IntervalsWithoutSample; } }
private SerialPort m_SerialPort;
public string PortName { get; set; }
public int BaudRate { get; set; }
public int DataBits { get; set; }
private Double m_Weight = 0;
public Double Weight
{
get
{
if (m_BufferWeights.Count > 0)
{
try
{
m_Weight = treatReceivedValue(m_BufferWeights[m_BufferWeights.Count - 1]);
}
catch
{
}
finally
{
ErrorReadingWeight = (m_Weight != -1 ? false : true);
}
}
else
{
m_Weight = 0;
}
return m_Weight;
}
}
public List<Double> getAndFlushPastWeights()
{
List<Double> v_FlushedValues = new List<double>();
Double v_WeightCursor;
while (m_BufferWeights.Count > 1 && v_FlushedValues.Count < 200)
{
v_WeightCursor = treatReceivedValue(m_BufferWeights[0]);
if (v_WeightCursor >= 0)
{
v_FlushedValues.Add(v_WeightCursor);
}
m_BufferWeights.RemoveAt(0);
}
return v_FlushedValues;
}
public void ResetWeights()
{
if (m_BufferWeights != null)
{
m_BufferWeights.Clear();
}
}
public string NewLineCommandType { get; set; }
public string NewLineCommand { get; set; }
public int ReturnLength { get; set; }
public int WeightInitialPosition { get; set; }
public int WeightFinalPosition { get; set; }
public int MotionBitPos { get; set; }
public int ReadTimeOut { get; set; }
public bool OpenedPort { get; private set; }
public bool ErrorReadingWeight { get; private set; }
public DateTime LastGoodReading { get; private set; }
public bool IsStable { get; private set; }
private Parity PortParity { get; set; }
public string SerialParity
{
get { return PortParity.ToString(); }
set
{
setParity(value);
}
}
public int WeightReadLength
{
get
{
if (WeightFinalPosition >= WeightInitialPosition)
{
return WeightFinalPosition - WeightInitialPosition + 1;
}
else
{
return 0;
}
}
}
private StopBits PortStopBits { get; set; }
public string SerialStopBits
{
get { return PortStopBits.ToString(); }
set
{
setStopBits(value);
}
}
private void setParity(string p_Parity)
{
if (p_Parity.Equals(Parity.Even.ToString()))
{
PortParity = Parity.Even;
}
else if (p_Parity.Equals(Parity.Mark.ToString()))
{
PortParity = Parity.Mark;
}
else if (p_Parity.Equals(Parity.Odd.ToString()))
{
PortParity = Parity.Odd;
}
else if (p_Parity.Equals(Parity.Space.ToString()))
{
PortParity = Parity.Space;
}
else
{
PortParity = Parity.None;
}
}
private void setStopBits(string p_StopBits)
{
if (p_StopBits.Equals(StopBits.One.ToString()))
{
PortStopBits = StopBits.One;
}
else if (p_StopBits.Equals(StopBits.OnePointFive.ToString()))
{
PortStopBits = StopBits.OnePointFive;
}
else if (p_StopBits.Equals(StopBits.Two.ToString()))
{
PortStopBits = StopBits.Two;
}
else if (p_StopBits.Equals("1"))
{
PortStopBits = StopBits.One;
}
else if (p_StopBits.Equals("1.5"))
{
PortStopBits = StopBits.OnePointFive;
}
else if (p_StopBits.Equals("2"))
{
PortStopBits = StopBits.Two;
}
else
{
PortStopBits = StopBits.None;
}
}
public Scales()
{
OpenedPort = false;
ErrorReadingWeight = false;
IsStable = false;
m_IntervalsWithoutSample = 999999;
timerTimeWithoutSample = new System.Timers.Timer(5);
timerTimeWithoutSample.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
}
private int ignoreNextXValues;
public void resetScale()
{
ErrorReadingWeight = false;
IsStable = false;
m_IntervalsWithoutSample = 999999;
ignoreNextXValues = 2;
m_BufferWeights.Clear();
m_BufferTime.Clear();
if (m_SerialPort != null && m_SerialPort.IsOpen)
{
m_SerialPort.Close();
m_SerialPort.Open();
m_SerialPort.DiscardInBuffer();
}
}
List<String> m_BufferWeights = new List<String>();
List<String> m_BufferTime = new List<String>();
public bool openPort()
{
try
{
if (m_SerialPort.IsOpen)
{
m_SerialPort.Close();
}
m_SerialPort.Open();
resetScale();
OpenedPort = true;
return true;
}
catch (Exception ex)
{
MessageDetail = ex.Message;
Return = -100;
OpenedPort = false;
return false;
}
}
public bool closePort()
{
try
{
if (m_SerialPort != null)
{
if (m_SerialPort.IsOpen)
{
m_SerialPort.Close();
}
}
OpenedPort = false;
return true;
}
catch (Exception ex)
{
MessageDetail = ex.Message;
Return = -101;
return false;
}
}
public bool configurePort()
{
try
{
m_SerialPort = new SerialPort();
m_SerialPort.PortName = PortName;
m_SerialPort.BaudRate = BaudRate;
m_SerialPort.Parity = PortParity;
m_SerialPort.DataBits = DataBits;
m_SerialPort.StopBits = PortStopBits;
m_SerialPort.ReadTimeout = ReadTimeOut > 0 ? ReadTimeOut : SerialPort.InfiniteTimeout;
m_SerialPort.NewLine = getNewLineCommand();
m_SerialPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort_DataReceived);
return true;
}
catch (Exception ex)
{
MessageDetail = ex.Message;
Return = -102;
return false;
}
}
private string getNewLineCommand()
{
string v_Command = string.Empty;
if (NewLineCommandType.ToUpper().Equals(CommandTypes.CHAR.ToUpper()))
{
byte v_Char = Convert.ToByte(NewLineCommand);
v_Command = Convert.ToChar(v_Char).ToString();
}
else if (NewLineCommandType.ToUpper().Equals(CommandTypes.STRING.ToUpper()))
{
v_Command = NewLineCommand;
}
else
{
char[] v_delimiters = { '|' };
String[] v_Strings = NewLineCommand.Split(v_delimiters);
if (v_Strings.Length == 2)
{
v_Command = v_Strings[0];
m_EndOfWeightChar = v_Strings[1];
}
else
{
v_Command = NewLineCommand;
}
}
return v_Command;
}
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
LastGoodReading = DateTime.Now;
string ReadLine = m_SerialPort.ReadLine();
m_BufferWeights.Add(ReadLine);
}
catch (Exception)
{
m_Weight = 0;
LastGoodReading = DateTime.MinValue;
}
}
private Double treatReceivedValue(string p_ReceivedValue)
{
try
{
if (ignoreNextXValues > 0) ignoreNextXValues--;
if (ignoreNextXValues > 0) return 0;
double v_Value = double.MinValue;
p_ReceivedValue = p_ReceivedValue.Replace("\r", "").Replace("\n", "");
m_IntervalsWithoutSample = 0;
if (p_ReceivedValue.Length < WeightInitialPosition + WeightReadLength)
{
return -1;
}
if (MotionBitPos != -1 && p_ReceivedValue.Length < MotionBitPos - 1)
{
return -1;
}
string strValor = "";
if (NewLineCommandType.ToUpper().Equals(CommandTypes.VARIABLE_LENGTH.ToUpper()))
{
int v_EndCharPos = p_ReceivedValue.IndexOf(m_EndOfWeightChar);
if (v_EndCharPos != -1)
{
strValor = p_ReceivedValue.Substring(0, v_EndCharPos).Trim();
}
}
else
{
strValor = p_ReceivedValue.Substring(WeightInitialPosition, WeightReadLength).Trim();
}
bool IsDouble = double.TryParse(strValor, out v_Value);
if (IsDouble)
{
if (MotionBitPos != -1)
{
string bit = p_ReceivedValue.Substring(MotionBitPos, 1).Trim();
if (bit == "1")
{
IsStable = true;
}
else IsStable = false;
}
else
{
IsStable = true;
}
return v_Value;
}
else
{
return -1;
}
}
catch (Exception ex)
{
Return = -200;
MessageDetail = ex.Message + " - Fonte:readScales";
ErrorReadingWeight = true;
}
return -1;
}
}
}
I had a similar problem, using SerialPort.ReadExisting() insted of SerialPort.ReadLine() I was able to avoid the creation of infinite threads.
You should try to reduce your problematic code down to something more manageable, as it will make it easier for others to debug. There's a lot of application logic in there that's probably not relevant to the problem which can make it hard for people to see what's going on. You'll get a lot more answers if your example is shorter. You may even figure the problem out yourself in the process!
Having said that, I have a hunch about what's wrong but you'll need to do a little bit of the leg-work yourself to discover if I'm right or wrong:
The .NET serial port works by waiting for data to come in, and then firing the DataReceived event on a worker thread whenever it notices that there's new data. I believe you have 400 or 500 of these worker threads that never complete their work, so they never go away.
Your event handler for the SerialPort.DataReceived event looks like it's blocking waiting for a whole line to come in, but the event is going to be fired whenever there's some amount of new data on the serial port (not necessarily a whole line). If a long line of text comes in, the DataReceived event is going to fire many times, each on it's own worker thread. These worker threads are synchronized to each other, so they're all going to wait for the previous one to finish.
The first thread that gets queued up is going to wait for a while at m_SerialPort.ReadLine() until the whole line comes in.
A bunch of threads queue up behind the first thread as more characters come in. The rest of the threads will end up waiting for the first thread to finish running your event handler.
Finally, the whole line comes in. The first thread finishes, and one of the 5 or 6 that are queued up behind it gets to run and the process starts all over.
The running thread blocks on ReadLine, 5 or 6 more queue up behind it. (We're now back at 1)
Eventually you have so many threads queued up that you run into memory issues.
You probably have the read-timeout on m_SerialPort set to timeout.Infinite. If you set the timeout to something smaller, like 1 second (1000) and you get a lot of TimeoutExceptions in your SerialPort_DataReceived method, then I'm probably right
Side Note You should catch a more specific exception type in your DataReceived event handler. Catching exception can mask exactly this type of problem.
If I've correctly diagnosed the problem, you'll need to change the architecture of your program a little bit. The simplest thing to do is not subscribe to the DataReceived event and to have a single worker thread call m_SerialPort.ReadLine(); with an infinite timeout. When it reads a line, have that worker thread raise an event with the whole line of text received and subscribe to THAT event instead of the SerialPort.DataReceived(); event.
Alternatively, if you want to subscribe to the SerialPort.DataReceived(); event, then read individual characters out of the SerialPort until SerialPort.BytesToRead is zero and stick them in a buffer. Then, when you have a whole line raise some "LineReceived" event that you make yourself that returns the whole line at once as one of the EventArgs. This method is doesn't require you to spool up your own thread that persists for a really long time.

Kinect Speech Recognition not Working

I have a program that when the user says "Start" or "Stop", the program makes a skeleton display on the screen. I use the same code as the Shape Game, and it works fine there, but not on my code. I dont know which part of the code doesn't work since this is my first time eith speech recognition programming. Thanks for your help(Sorry if my code is messy)Recognizing the Speech
public class SpeechRecognizer : IDisposable
{
private KinectAudioSource kinectAudioSource;
private struct WhatSaid
{
public Verbs Verb;
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
this.Stop();
if (this.sre != null)
{
// NOTE: The SpeechRecognitionEngine can take a long time to dispose
// so we will dispose it on a background thread
ThreadPool.QueueUserWorkItem(
delegate(object state)
{
IDisposable toDispose = state as IDisposable;
if (toDispose != null)
{
toDispose.Dispose();
}
},
this.sre);
this.sre = null;
}
this.isDisposed = true;
}
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
public EchoCancellationMode EchoCancellationMode
{
get
{
this.CheckDisposed();
return this.kinectAudioSource.EchoCancellationMode;
}
set
{
this.CheckDisposed();
this.kinectAudioSource.EchoCancellationMode = value;
}
}
public static SpeechRecognizer Create()
{
SpeechRecognizer recognizer = null;
try
{
recognizer = new SpeechRecognizer();
}
catch (Exception)
{
// speech prereq isn't installed. a null recognizer will be handled properly by the app.
}
return recognizer;
}
private void CheckDisposed()
{
if (this.isDisposed)
{
throw new ObjectDisposedException("SpeechRecognizer");
}
}
public void Stop()
{
this.CheckDisposed();
if (this.sre != null)
{
this.kinectAudioSource.Stop();
this.sre.RecognizeAsyncCancel();
this.sre.RecognizeAsyncStop();
this.sre.SpeechRecognized -= this.SreSpeechRecognized;
this.sre.SpeechHypothesized -= this.SreSpeechHypothesized;
this.sre.SpeechRecognitionRejected -= this.SreSpeechRecognitionRejected;
}
}
public void Start(KinectAudioSource kinectSource)
{
this.CheckDisposed();
this.kinectAudioSource = kinectSource;
this.kinectAudioSource.AutomaticGainControlEnabled = false;
this.kinectAudioSource.BeamAngleMode = BeamAngleMode.Adaptive;
var kinectStream = this.kinectAudioSource.Start();
this.sre.SetInputToAudioStream(
kinectStream, new SpeechAudioFormatInfo(EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
this.sre.RecognizeAsync(RecognizeMode.Multiple);
}
public enum Verbs
{
None = 0,
Start,
Stop,
Resume,
Pause
}
private bool isDisposed;
private readonly Dictionary<string, WhatSaid> speechCommands = new Dictionary<string, WhatSaid>
{
{ "Start", new WhatSaid { Verb = Verbs.Start } },
{ "Stop", new WhatSaid { Verb = Verbs.Stop } },
{ "Resume", new WhatSaid { Verb = Verbs.Resume } },
{ "Pause", new WhatSaid { Verb = Verbs.Pause } },
};
private SpeechRecognitionEngine sre;
private static RecognizerInfo GetKinectRecognizer()
{
Func<RecognizerInfo, bool> matchingFunc = r =>
{
string value;
r.AdditionalInfo.TryGetValue("Kinect", out value);
return "True".Equals(value, StringComparison.InvariantCultureIgnoreCase) && "en-US".Equals(r.Culture.Name, StringComparison.InvariantCultureIgnoreCase);
};
return SpeechRecognitionEngine.InstalledRecognizers().Where(matchingFunc).FirstOrDefault();
}
private SpeechRecognizer()
{
RecognizerInfo ri = GetKinectRecognizer();
this.sre = new SpeechRecognitionEngine(ri);
this.LoadGrammar(this.sre);
}
private void LoadGrammar(SpeechRecognitionEngine speechRecognitionEngine)
{
// Build a simple grammar of shapes, colors, and some simple program control
var single = new Choices();
foreach (var phrase in this.speechCommands)
{
single.Add(phrase.Key);
}
var objectChoices = new Choices();
objectChoices.Add(single);
var actionGrammar = new GrammarBuilder();
actionGrammar.AppendWildcard();
actionGrammar.Append(objectChoices);
var allChoices = new Choices();
allChoices.Add(actionGrammar);
allChoices.Add(single);
// This is needed to ensure that it will work on machines with any culture, not just en-us.
var gb = new GrammarBuilder { Culture = speechRecognitionEngine.RecognizerInfo.Culture };
gb.Append(allChoices);
var g = new Grammar(gb);
speechRecognitionEngine.LoadGrammar(g);
speechRecognitionEngine.SpeechRecognized += this.SreSpeechRecognized;
speechRecognitionEngine.SpeechHypothesized += this.SreSpeechHypothesized;
speechRecognitionEngine.SpeechRecognitionRejected += this.SreSpeechRecognitionRejected;
}
private void SreSpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
{
var said = new SaidSomethingEventArgs { Verb = Verbs.None, Matched = "?" };
this.SetLabel("Word not Recognized.... Try 'Start', 'Stop', 'Pause' or 'Resume'");
if (this.SaidSomething != null)
{
this.SaidSomething(new object(), said);
}
}
private void SreSpeechHypothesized(object sender, SpeechHypothesizedEventArgs e)
{
this.SetLabel("I think you said: " + e.Result.Text);
}
public event EventHandler<SaidSomethingEventArgs> SaidSomething;
private void SreSpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
this.SetLabel("\rSpeech Recognized: \t" + e.Result.Text);
if ((this.SaidSomething == null) || (e.Result.Confidence < 0.3))
{
return;
}
var said = new SaidSomethingEventArgs { Verb = 0, Phrase = e.Result.Text };
foreach (var phrase in this.speechCommands)
{
if (e.Result.Text.Contains(phrase.Key) && (phrase.Value.Verb == Verbs.Pause))
{
//pause = true;
break;
}
else if ((e.Result.Text.Contains(phrase.Key) && (phrase.Value.Verb == Verbs.Resume)))
{
//resume = true;
break;
}
else if ((e.Result.Text.Contains(phrase.Key) && (phrase.Value.Verb == Verbs.Start)))
{
//start = true;
break;
}
else if ((e.Result.Text.Contains(phrase.Key) && (phrase.Value.Verb == Verbs.Stop)))
{
//stop = true;
break;
}
}
// Look for a match in the order of the lists below, first match wins.
List<Dictionary<string, WhatSaid>> allDicts = new List<Dictionary<string, WhatSaid>> { this.speechCommands };
bool found = false;
for (int i = 0; i < allDicts.Count && !found; ++i)
{
foreach (var phrase in allDicts[i])
{
if (e.Result.Text.Contains(phrase.Key))
{
found = true;
break;
}
}
}
if (!found)
{
return;
}
}
public class SaidSomethingEventArgs : EventArgs
{
public Verbs Verb { get; set; }
public string Phrase { get; set; }
public string Matched { get; set; }
}
public event Action<string> SetLabel = delegate { };
}
In my Code
private void RecognizeSaidSomething(object sender, SpeechRecognizer.SpeechRecognizer.SaidSomethingEventArgs e)
{
FlyingText.FlyingText.NewFlyingText(this.skeleton.Width / 30, new Point(this.skeleton.Width / 2, this.skeleton.Height / 2), e.Matched);
switch (e.Verb)
{
case SpeechRecognizer.SpeechRecognizer.Verbs.Pause:
pause = true;
break;
case SpeechRecognizer.SpeechRecognizer.Verbs.Resume:
resume = true;
break;
case SpeechRecognizer.SpeechRecognizer.Verbs.Start:
start = true;
break;
case SpeechRecognizer.SpeechRecognizer.Verbs.Stop:
stop = true;
break;
}
}
It doesn't look like you ever call RecognizeSaidSomething() from SreSpeechRecognized(). You create the event args:
var said = new SaidSomethingEventArgs { Verb = 0, Phrase =
e.Result.Text };
But it doesn't appear that you do anything with it.
The foreach loop below that doesn't seem to serve any purpose, you test for the phrase then just break out of the loop. You don't set any variables or call any functions in that loop that I can see.
Then there is a for loop that appears to do something similar to the foreach loop (just in a different manner). It searches for matches to the recognized phrase, but then doesn't do anything with what it finds. It just returns.
I would think somewhere in the SreSpeechRecognized() event handler you want to call RecognizeSaidSomething() and pass it the SaidSomethingEventArgs.

NullReferenceException WIA C#

When I run the code below I get this error
NullreferenceException was unhandled..
below is my code
private void showScannerDialog()
{
this.scanner = null;
this.imageItem = null;
this.getScanner();
WIA.CommonDialog dialog = new WIA.CommonDialog();
Items imageItems = dialog.ShowSelectItems(this.scanner, WiaImageIntent.TextIntent, WiaImageBias.MinimizeSize, false, true, false);
if (imageItems != null)
{
foreach (Item item in imageItems)
{
imageItem = item;
break;
}
}
}
thanks
// complete code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
string scannerName;
private Device scanner;
private Item imageItem;
private const int ADF = 1;
private const int FLATBED = 2;
private const int DEVICE_NAME_PROPERTY_ID = 7;
private const int DOCUMENT_HANDLING_PROPERTY_ID = 3088;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
showScannerDialog();
}
public static string[] GetScannerList()
{
ArrayList scannerList = new ArrayList();
DeviceManager deviceManager = new DeviceManager();
if (deviceManager.DeviceInfos.Count == 0)
{
return new string[0]; // return an empty string array
}
foreach (DeviceInfo deviceInfo in deviceManager.DeviceInfos)
{
if (deviceInfo.Type == WiaDeviceType.ScannerDeviceType)
{
Device device = deviceInfo.Connect();
scannerList.Add(getDeviceProperty(device, DEVICE_NAME_PROPERTY_ID));
}
}
return (string[])scannerList.ToArray(typeof(string));
}
public int Init(string scannerName)
{
this.scannerName = scannerName;
this.showScannerDialog();
if (this.imageItem == null)
{
return 0;
}
else
{
return 1;
}
}
public int Scan(string filePath)
{
Tiff tiff = new Tiff(filePath);
bool adf = false;
int numScans = 0;
ArrayList tempFiles = new ArrayList();
// determine if the scanner is set to use an ADF
string docHandlingSelect = getDeviceProperty(this.scanner, DOCUMENT_HANDLING_PROPERTY_ID);
if (docHandlingSelect != "")
{
try
{
if ((int.Parse(docHandlingSelect) & ADF) == ADF)
{
adf = true;
}
}
catch { }
}
while (true)
{
string tempFile = Path.GetTempFileName();
tempFiles.Add(tempFile);
File.Delete(tempFile);
ImageFile wiaFile = (ImageFile)imageItem.Transfer(FormatID.wiaFormatTIFF);
wiaFile.SaveFile(tempFile);
Image tempImage = Image.FromFile(tempFile);
tiff.AddImage(tempImage);
tempImage.Dispose();
numScans++;
if (!adf)
{
DialogResult result =
MessageBox.Show("Do you wish to scan another page and save it to the same file?",
"Scanner Message", MessageBoxButtons.YesNo);
if (result == DialogResult.No)
{
break;
}
this.showScannerDialog();
if (this.imageItem == null)
{
break;
}
}
}
tiff.Close();
foreach (string f in tempFiles.ToArray(typeof(string)))
{
File.Delete(f);
}
Marshal.ReleaseComObject(imageItem);
if (numScans == 0)
{
throw new Exception("Nothing was scanned.");
}
return 1;
}
private void getScanner()
{
DeviceManager deviceManager = new DeviceManager();
foreach (DeviceInfo deviceInfo in deviceManager.DeviceInfos)
{
if (deviceInfo.Type == WiaDeviceType.ScannerDeviceType)
{
Device device = deviceInfo.Connect();
if (this.scannerName == getDeviceProperty(device, DEVICE_NAME_PROPERTY_ID))
{
this.scanner = device;
}
}
}
}
private void showScannerDialog()
{
this.scanner = null;
this.imageItem = null;
this.getScanner();
WIA.CommonDialog dialog = new WIA.CommonDialog();
Items imageItems = dialog.ShowSelectItems(this.scanner, WiaImageIntent.TextIntent, WiaImageBias.MinimizeSize, false, true, false);
if (imageItems != null)
{
foreach (Item item in imageItems)
{
imageItem = item;
break;
}
}
}
private static string getDeviceProperty(Device device, int propteryID)
{
string retVal = "";
foreach (Property prop in device.Properties)
{
if (prop.PropertyID == propteryID)
{
retVal = prop.get_Value().ToString();
break;
}
}
return retVal;
}
}
}
this.scanner is set to null when you pass it to dialog.ShowSelectItems(). The function may be trying to use it without checking if it is null.

Categories