Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Reading Multi Byte from Serial Port

serialport

Best Answer asciiman, 22 July 2014 - 03:43 AM

hi

 

I wrote this 


/// <summary>
/// Start of Text Transmission (STX) character 
/// <para>Hexadecimal: 0x02 ~ Decimal: 2</para>
/// </summary>
public const char STX = '\u0002';

/// <summary>
/// End of Transmission Block (ETB) character
/// <para>Hexadecimal: 0x17 ~ Decimal: 23</para>
/// </summary>
public const char ETB = '\u0017';
		
private static System.Collections.ArrayList _SerialInBuffer;

public static void Main()
{
	_SerialInBuffer = new System.Collections.ArrayList();

	var xSerialPort = new System.IO.Ports.SerialPort("COM1", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
	xSerialPort.Open();

	xSerialPort.DataReceived += xSerialPort_DataReceived;
	
}

private static void xSerialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
	var xSerialPort = sender as System.IO.Ports.SerialPort;

	byte[] xReceivedBytes = new byte[xSerialPort.BytesToRead];
	xSerialPort.Read(xReceivedBytes, 0, xReceivedBytes.Length);

	foreach (byte xReceivedByte in xReceivedBytes)
		if (xReceivedByte == STX)
		{
			_SerialInBuffer.Clear();
			_SerialInBuffer.Add(xReceivedByte);
		}
		else if (xReceivedByte != ETB)
		{
			_SerialInBuffer.Add(xReceivedByte);
		}
		else    // ETB
		{
			byte[] tmpBytes = new byte[_SerialInBuffer.Count];
			_SerialInBuffer.CopyTo(tmpBytes);
			_SerialInBuffer.Clear();

			string xSerialCommand = new string(System.Text.Encoding.UTF8.GetChars(tmpBytes)) + ETB;

			string xValue = xSerialCommand.Trim(new char[] { STX, ETB });
			 

			Microsoft.SPOT.Debug.Print(xSerialCommand);
		}
}

and you must send data like this  

string xTransmittingData = STX + "this data will be send" + ETB
Go to the full post


  • Please log in to reply
2 replies to this topic

#1 Senthil

Senthil

    New Member

  • Members
  • Pip
  • 5 posts

Posted 20 July 2014 - 11:20 PM

 I am trying to read a value from COM1 using DataReceived Event. But with below sample I was able to ready only Single Byte ,  What I wanted to do is to read String . Say "On" "Off" I do I do it ?
public class Program
    {
        static SerialPort port;
    
        public static void Main()
        { 
            port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One );
            port.Open();
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); 
            Thread.Sleep(Timeout.Infinite);

        }

        static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] byte1 = new byte[1];
            port.Read(byte1, 0, byte1.Length);
char[] c = System.Text.Encoding.UTF8.GetChars(byte1);

            String a = new String(c);
            if (a == "n")
            {
              //on led
            }
            else if (a == "f")
            {
               //off led
            }
} }


#2 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 21 July 2014 - 05:15 PM

Generally I find that you have little control over the way the data is presented.  So you may send a 20 byte string to the Netduino from say a PC but you my get that in chunks.  In the past I have captured all of the data being sent over the serial line in a buffer and looked out for a special "end of line" marker, say carriage return.  When the marker is received I process all of the data from the start of the buffer to the marker.

 

If you want some example code then you can have a look at what I did connecting a PC to a Netduino using a Bluetooth serial port.

 

Hope this helps,

Mark


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#3 asciiman

asciiman

    Advanced Member

  • Members
  • PipPipPip
  • 56 posts
  • LocationTurkey

Posted 22 July 2014 - 03:43 AM   Best Answer

hi

 

I wrote this 


/// <summary>
/// Start of Text Transmission (STX) character 
/// <para>Hexadecimal: 0x02 ~ Decimal: 2</para>
/// </summary>
public const char STX = '\u0002';

/// <summary>
/// End of Transmission Block (ETB) character
/// <para>Hexadecimal: 0x17 ~ Decimal: 23</para>
/// </summary>
public const char ETB = '\u0017';
		
private static System.Collections.ArrayList _SerialInBuffer;

public static void Main()
{
	_SerialInBuffer = new System.Collections.ArrayList();

	var xSerialPort = new System.IO.Ports.SerialPort("COM1", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
	xSerialPort.Open();

	xSerialPort.DataReceived += xSerialPort_DataReceived;
	
}

private static void xSerialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
	var xSerialPort = sender as System.IO.Ports.SerialPort;

	byte[] xReceivedBytes = new byte[xSerialPort.BytesToRead];
	xSerialPort.Read(xReceivedBytes, 0, xReceivedBytes.Length);

	foreach (byte xReceivedByte in xReceivedBytes)
		if (xReceivedByte == STX)
		{
			_SerialInBuffer.Clear();
			_SerialInBuffer.Add(xReceivedByte);
		}
		else if (xReceivedByte != ETB)
		{
			_SerialInBuffer.Add(xReceivedByte);
		}
		else    // ETB
		{
			byte[] tmpBytes = new byte[_SerialInBuffer.Count];
			_SerialInBuffer.CopyTo(tmpBytes);
			_SerialInBuffer.Clear();

			string xSerialCommand = new string(System.Text.Encoding.UTF8.GetChars(tmpBytes)) + ETB;

			string xValue = xSerialCommand.Trim(new char[] { STX, ETB });
			 

			Microsoft.SPOT.Debug.Print(xSerialCommand);
		}
}

and you must send data like this  

string xTransmittingData = STX + "this data will be send" + ETB






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.