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" + ETBGo to the full post