Solved

Regex TCP/IP break-out box

Posted on 2010-09-20
8
440 Views
Last Modified: 2012-05-10
Hi all,
I have a device sending a string pattern every half second. I am using TCP/IP to connect to it and read info.

The format goes as this:
<STX><SIGN><NUM(7)><STATUS><ETX>

STX: Start of transmission character (ASCII 02).
ETX: End of transmission character (ASCII 03).
SIGN: The sign of reading (space for positive, dash (-) for negative).
NUM: A 7 character string containing the number including the decimal point.
STATUS: A 1 character provifing info on the nature of the number

So when I connect to my device, it sends the info like this:

<STX><SIGN><NUM(7)><STATUS><ETX><STX><SIGN><NUM(7)><STATUS><ETX><STX><SIGN><NUM(7)><STATUS><ETX><STX><SIGN><NUM(7)><STATUS><ETX><STX><SIGN><NUM(7)><STATUS><ETX>...etc...

I only need to be displaying in realtime on a computer screen. What is the best way to do this? The string never ends, unless I close the port of course.

Thanks
0
Comment
Question by:vortex350
  • 4
  • 3
8 Comments
 
LVL 2

Expert Comment

by:vsosu
ID: 33715392
If it is a web application, you can show it as marquee.
0
 

Author Comment

by:vortex350
ID: 33715613
It is not a web app. It is a winforms app. I have a textbox to refresh every second or so to read the info as it is extracted. My question has more to do with how to perform that extraction. And I need to display one info at a time.

The info being the pattern I described earlier.

Thanks
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 33716584
I would create a seperate class to do this reading, you could pass in the Tcp socket to it's constructor, then have it start a loop on another thread and have it raise events as it decodes data.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:vortex350
ID: 33717211
That sounds good. Could you give me a code sample for what you are talking about?

Thanks
0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 500 total points
ID: 33717314
This is a pretty rough mock-up, needs quite a bit of refinement, my goal really is just to demonstrate a concept not give you bug-free code. ;)
Using a class like the one below would allow you to simply handle the DataReceived event in your application...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
using System.ComponentModel;

namespace WindowsFormsApplication1
{
	public class DataDecoder
	{
		private bool disposed = false;
		Socket socket;
		Thread decodingThread;
		private ManualResetEvent workerStopSignal = new ManualResetEvent(false);

		private const byte STX = 0x02;
		private const byte ETX = 0x03;

		public delegate void DataDecodedEventHandler(object sender, string DecodedData);
		public event DataDecodedEventHandler DataDecoded;

		public DataDecoder(Socket ReceivingSocket)
		{
			if (!ReceivingSocket.Connected)
				throw new ArgumentException("Socket must be connected prior to instantiating DataDecoder.", "ReceivingSocket");
			socket = ReceivingSocket;
		}

		public void Start()
		{
			workerStopSignal.Reset();
			decodingThread = new Thread(DecodingThreadProc);
			decodingThread.IsBackground = true;
			decodingThread.Start();
		}

		public void Stop()
		{
			workerStopSignal.Set();
			decodingThread.Join();
		}

		protected virtual void OnDataDecoded(string DecodedData)
		{
			if (DataDecoded != null)
				DataDecoded(this, DecodedData);
		}

		private void DecodingThreadProc()
		{
			byte[] buffer = new byte[1024];
			bool txStarted = false;
			int bytesRead;
			StringBuilder decodedData = new StringBuilder();

			while (!workerStopSignal.WaitOne(0))
			{
				bytesRead = socket.Receive(buffer);
				for (int i = 0; i < bytesRead; i++)
				{
					if (txStarted)
					{
						if (buffer[i] == ETX)
						{
							txStarted = false;
							OnDataDecoded(decodedData.ToString());
							decodedData.Remove(0, decodedData.Length);
						}
						else
							decodedData.Append(buffer[i]);
					}
					else
						if (buffer[i] == STX)
							txStarted = true;
				}
			}
		}
	}
}

Open in new window

0
 

Author Comment

by:vortex350
ID: 33719007
Ok, now I am trying to understand how to get the data back from the class, how do I read the raised events as the data is decoded?

Let's say I instantiate the class on an event such as loading the form once the socket is connected.

DataDecoder dataDecode = new DataDecoder(formSocket);
dataDecode.Start();

I would send a dataDecode.Stop(); only once I close the form.

Let's say I have a textbox in my calling form, how do I refresh it's value based on the public event. Sorry if it seems like a stupid question. I am not familiar with threads because it is a vast subject I never had the courage to tackle. But after this, I will have to study it.

Thanks
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 33719222
You would wire up the DataDecoded event of the DataDecoder object.
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;

namespace WindowsFormsApplication1
{
	public partial class Form1 : Form
	{
		private DataDecoder dataDecode;

		public Form1()
		{
			InitializeComponent();
		}

		private void Form1_Load(object sender, EventArgs e)
		{
			dataDecode = new DataDecoder();
			dataDecode.DataDecoded += new DataDecoder.DataDecodedEventHandler(dataDecode_DataDecoded);
			dataDecode.Start();
		}

		void dataDecode_DataDecoded(object sender, string DecodedData)
		{
			// This code block is essentially just "textBox1.Text = DecodedData",
			// but in a thread-safe manner
			if (textBox1.InvokeRequired)
			{
				textBox1.Invoke(new MethodInvoker(
					delegate()
					{
						textBox1.Text = DecodedData;
					}));
			}
		}
	}
}

Open in new window

0
 

Author Comment

by:vortex350
ID: 33720038
As I understand it, each time the class retrieves new information, it will be reflected in the text?

1.When I issue a dataDecode.Stop() , the program freezes as it seems to be waiting for something, I have to kill the program. Am I missing something?

2.The characters I seem to get back from my unit are numbers only, could it be an encoding option? Normally the .Append() method of the StringBuilder is supposed to append the string representation of a specified 8-bit unsigned integer when you pass it a byte which is the case here.
 I know I should probably use something like Encoding.ASCII.GetString(buffer,0,1) but don't seem to be able to implement it in the code.

Thanks for your help.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# parent child form 5 28
Graphics 2 28
How to make a Gridview column hidden (visibility false) in the HTML code? 2 16
tableview is not updating 1 9
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question