/// <summary> /// Data receieved event /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected virtual void serial_DataReceived(object sender, SerialDataReceivedEventArgs e) { if (serial.IsOpen) { List <char> readBuffer = new List <char>(); while (serial.BytesToRead > 0) { readBuffer.Add((char)serial.ReadByte()); } if (readBuffer.Count > 0) { m_parent.CardSwipeDetected(this, 1, new string(readBuffer.ToArray()).Trim()); } } }
/// <summary> /// The method that reads for cards. /// </summary> private void ThreadWorker() { while (!TerminateThread) { try { NetworkStream stream = null; StreamWriter writer = null; StreamReader reader = null; // Are we reading for cards? if (ReadingCards) { char[] buffer; // Check our TCP connection to make sure we are open and // ready to go. if (CheckConnection()) { // We had the restablish the connection, so make // sure we are talking to a CPCL printer. stream = m_tcpClient.GetStream(); writer = new StreamWriter(stream); reader = new StreamReader(stream); writer.WriteLine(CPCLVersion); writer.Flush(); // Attempt to read the version. buffer = new char[VersionLength]; reader.Read(buffer, 0, buffer.Length); // Check to make sure it returned valid values. for (int x = 0; x < buffer.Length; x++) { if (!char.IsLetterOrDigit(buffer[x])) { throw new ApplicationException(); } } // Everything seems okay, so send the mag. init function. short track; lock (m_settingSync) { track = m_track; } writer.WriteLine(string.Format(CPCLMagInit, track, (track == 1?'%':';'))); writer.Flush(); } // Is there any data to read? if (m_tcpClient != null && m_tcpClient.Connected) { if (writer == null) { stream = m_tcpClient.GetStream(); writer = new StreamWriter(stream); } if (reader == null) { stream = m_tcpClient.GetStream(); reader = new StreamReader(stream); } writer.WriteLine(CPCLMagQuery); writer.Flush(); if (m_tcpClient.Available > 0) { buffer = new char[m_tcpClient.Available]; reader.Read(buffer, 0, buffer.Length); // Send the data to the parent class. m_parent.CardSwipeDetected(this, m_track, new string(buffer).Trim()); } } } else { // We are no longer reading, should we disconnect? if (m_tcpClient != null && m_tcpClient.Connected) { stream = m_tcpClient.GetStream(); writer = new StreamWriter(stream); writer.WriteLine(CPCLMagEnd); writer.Flush(); } CloseConnection(); } } catch { CloseConnection(); } Thread.Sleep(ThreadPause); } // Clean up the connection. CloseConnection(); }