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

Readd SerialPort


  • Please log in to reply
5 replies to this topic

#1 bubury

bubury

    New Member

  • Members
  • Pip
  • 3 posts

Posted 15 August 2012 - 10:09 AM

Hello

i am reading and writing Command AT using the serial port of netduino, the problem is that when the program have made some reads and writes, the information begins to split into several lines and I can not capture the answers because it is in two lines.

For example, i am waiting for "+CHTTPACT: REQUEST" but instead i catch two lines:
+CHTTPACT: R
EQUEST

Enclosed my code if anybody can give me some guidance,
Thanks so much

static void serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {  
           while (serial.BytesToRead > 0)
           {
               serial.Flush();
               byte[] bytes = new byte[serial.BytesToRead];
               
                serial.Read(bytes, 0, bytes.Length);

                String  cadena =  new string(Encoding.UTF8.GetChars(bytes));
                
                Respuestas(cadena);

                printscreen(bytes);
            }
            serial.Flush();
        }

Edited by Chris Walker, 15 August 2012 - 10:43 AM.
added [code][/code] tags


#2 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 15 August 2012 - 10:50 AM

Hi and welcome to the forums!

The SerialPort.Read() call doesn't read until end of line, you need to add some more logic into it if you really need to do so. I did this, for example, for a GPS class, full source code:
http://netmftoolbox....ew/16508#259982

Here's the snippet that matters:
private void _Uart_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    byte[] Buffer = new byte[this._Uart.BytesToRead];
    this._Uart.Read(Buffer, 0, Buffer.Length);
    this._Buffer += new string(Tools.Bytes2Chars(Buffer));
    
    // Have we received a full line of data?
    int Pos = this._Buffer.IndexOf("\r\n");
    if (Pos >= 0)
    {
    	// Data gets parsed
    }
}

As you can see I made a private string, this._Buffer, and append all data to that buffer.
As soon as a CrLf (Carriage Return + Line Feed) is in the buffer, we know there's a full line of data, so we can parse that.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#3 bubury

bubury

    New Member

  • Members
  • Pip
  • 3 posts

Posted 15 August 2012 - 10:34 PM

Hello Thanks for the welcome, I keep passing the same, all answers are correct but after several minutes I returned the answer in two lines. could be by the buffer of the serial port? thanks +CHTTPACT: R EQUEST

#4 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 16 August 2012 - 05:03 AM

+CHTTPACT: R
EQUEST

As I tried to explain before, it's not two lines, but indeed the buffer of the serial port. That's why you have to read until line endings yourself, like in the code snippet I pasted earlier.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#5 bubury

bubury

    New Member

  • Members
  • Pip
  • 3 posts

Posted 16 August 2012 - 05:48 PM

Thanks, now working well. one more thing, what you recommend to analyze the responses, depending of the answers I get, i'll start a diferent task. I will have 8 or 9 different answers, and i don't know if is better compare variables with "IndexOf" or make something similar to a select case. what do you recommend? what is more optimal. thanks

#6 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 16 August 2012 - 06:13 PM

I think IndexOf is not the best way, but I think you can better test different scenarios and see if it performs as you'd expect it.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs




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.