Windows Mobile Developer Center > Smart Device Development Forums > .NET Compact Framework > Serial Port Communication using C# - .NET CF v2.0
Ask a questionAsk a question
 

AnswerSerial Port Communication using C# - .NET CF v2.0

  • Wednesday, August 22, 2007 7:55 AMRachad Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi All,

     

    I encountered the strangest thing today.  I have developed an application that uses RS-232 serial port.  Today I began to test it.  I found that when I send my application a data packet that contains 0x1A, it causes a Timeout Exception and I can't for the life of me figure out what could have gone wrong.  The application has been written using the .NET Compact Framework 2.0 and has been tested on a reference board, the device emulator and the Desktop (Windows XP) and it has done it in all situations. 

     

    I also wrote a desktop application that reads byte by byte and then displays each byte (using a message box).  Once again, when 0x1A comes in on the serial port and triggers the data received event I get a timeout exception.  I have also tried connecting using different baud rates.  It still didn't change a thing.

     

    0x1A (hex) is the ascii value for substitue, I don't know if Windows XP, CE and Mobile have aproblem with that or if there is something that I am doing that is not correct.

     

    This is the basic data received event

    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

    {

    bool bPortOpen = this.serialPort1.IsOpen;

     

    if (bPortOpen)

    {

    //if 0x1A then causes Timeout Exception

    byte aByte = (byte)this.serialPort1.ReadByte();

    MessageBox.Show("value = " + aByte);

    }

    else

    {

    MessageBox.Show("Not open", "serial port");

    }

    }

     

    OS used: CE 6.0 and Mobile 5.0,  XP,

    Serial Port Baud Rate Used : 115200, 57600, 38400, 9600

    DataBits = 8

    DiscardNull = false

    DtrEnable = false

    Handshake = None

    Parity = None

    ReadBufferSize = 4096

    ReadTimeout = 1000

    ReceivedBytesThreshold = 1

    RtsEnable = false

    StopBits = One

    WriteBufferSize = 2048

    WriteTimeout - 1000

     

    Does anyone know what could be happening?

     

    Thanks,

     

    Rachad

Answers

  • Wednesday, September 26, 2007 2:11 AMRachad Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

     

    Hi FSSI,

     

    I resolved the issue by using a seperate thread to process the data received on the UART.  My problem was there was a threading issue because I had a major part of source code in the DataReceived event.  Now the data received event only puts the data received on an internal buffer.  Then there is a sperated thread that will wait for the rxBuffer and process the data.

     

    Rachad

All Replies

  • Tuesday, September 25, 2007 4:47 PMDarin Rousseau Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi Rachad;

     

    Sounds like the classes have some decoding going on internally... 

     

    Check SerialPort.Encoding.

     

    ... where the input is decoded into ascii - oddly enough, the AsciiEncoding description says "Represents an ASCII character encoding of Unicode characters. " on http://msdn2.microsoft.com/en-us/library/ms186177(VS.80).aspx which most likely means the 0x1a is flagging something to the encoder, which attempts another read internally and times out when it doesn't get something meaningful.  perhaps you can create or use a separate codepage, whatever code page is native ASCII, AKA "do nothing with my serial port stream".

     

    Hope this helps;

  • Wednesday, September 26, 2007 2:11 AMRachad Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

     

    Hi FSSI,

     

    I resolved the issue by using a seperate thread to process the data received on the UART.  My problem was there was a threading issue because I had a major part of source code in the DataReceived event.  Now the data received event only puts the data received on an internal buffer.  Then there is a sperated thread that will wait for the rxBuffer and process the data.

     

    Rachad

  • Tuesday, June 30, 2009 11:44 AMsureshbush5 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Hi i am Suresh,

    I have one question regarding serial port,
    Here i done a simple program to read and write through serial port using visual studio 2005 in C#. Write operation is working good but i have problem in to read the existing data in readline.Here i done in program by using one button control for receiving data.After clicking dis button den only the existing readline data will be displayed in richtextbox2.

    but my aim is without using any windows control, the receiving data will be displayed in richtextbox2.can anyone give me solution for this?

    here i given what i done in my program.Please anyone find and give me the solution for this.

    using

    System;

    using

    System.Collections.Generic;

    using

    System.ComponentModel;

    using

    System.Data;

    using

    System.Drawing;

    using

    System.Text;

    using

    System.Windows.Forms;

    namespace

    Serialport_test

    {

     


    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

     

     


    private void button1_Click(object sender, EventArgs e)

    {

     

     


    try

    {
    port.Write(richTextBox1.Text);

     

    }

     

    catch(System.Exception ex)

    {

    }

    }

     


    private void Form1_Load(object sender, EventArgs e)

    {

     


    try

    {

    port.Open();

     

    }

     

    catch (System.Exception ex)

    {

     


    MessageBox.Show("Another COM port is opened");

    }

     

    }

     


    private void button2_Click(object sender, EventArgs e)

    {

    richTextBox2.Text = port.ReadExisting();

    port.ReadTimeout = 50;

    }

     

     

    }

    }

  • Saturday, September 12, 2009 7:05 PMbradaker Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Regarding your 0x1A issue, I know you said you worked around it, but I just wanted to let you know that when doing ASCII communication and you're in text mode as opposed to binary mode, a 0x1A in DOS and Windows is seen as an end of file character. I can't imagine that's a coincidence that 0x1A was what caused you problems. I don't know anything about what you're doing, but did you have a choice when you opened the port for whether or not you were doing communication as text or as binary (perhaps on an "open" command when you opened the port, similar to when you open a file)? If so, choose binary instead of text and I would bet that 0x1A won't give you any more problems.


    Brad