Main Site Documentation

Equivalante to SerialPort.ReadLine (string []), on .net Micro Framework


#1

hello
I want to read a frame (string []) on the serial port .
is there a method or code share equivalante to the method of SerialPort.ReadLine (string []) on Net Micro Frame ?


#2

sorry,
hello
I want to read trame of message type: string [] on the serial port .
is there a method or code share equivalante to the method of SerialPort.ReadLine (string []) on .Net Frame (on the PC)?
THX


#3

@ riad1986 - This is untested but should provide assistance. The code will only work for bytes in the lower ASCII table (0-127). Higher entries will cause issues as they are converted to char.

        void ReadSerialString(out string dataBuffer)
        {
            int transactionSize = port.BytesToRead;
            int read = 0;
            byte[] tempBuffer = new byte[transactionSize];

            while (read < transactionSize)
            {
                int thisRead = port.Read(tempBuffer, 0, transactionSize - read);
                dataBuffer += new string(System.Text.Encoding.UTF8.GetChars(tempBuffer));
                read += thisRead;

                if(read < transactionSize)
                {
                    for(int i = 0; i < transactionSize; i++)
                        tempBuffer[i] = 0; //Clear out buffer in case we read multiple times
                }
            }
        }

#4

hello,
thx for reply
how I can use this code( void ReadSerialString(out string dataBuffer) )?

my code is :

using System.Threading;
using System.IO.Ports;
using System.Text;

namespace change_this_to_your_namespace
{
public class Program
{
public static void Main()
{
SerialPort UART = new SerialPort(“COM1”, 115200);
int read_count = 0;
byte[] rx_byte = new byte[1];

     UART.Open();
     while (true)
     {
        // read one byte
        read_count = UART.Read(rx_byte, 0, 1);
        if (read_count > 0)// do we have data?
        {
           // create a string
           string counter_string =
                 "You typed: " + rx_byte[0].ToString() + "\r\n";
           // convert the string to bytes
           byte[] buffer = Encoding.UTF8.GetBytes(counter_string);
           // send the bytes on the serial port
           UART.Write(buffer, 0, buffer.Length);
           //wait...
           Thread.Sleep(10);
        }
     }
  }

}
}

thx