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

Help with Serial.Read for a newbie

RS232

  • Please log in to reply
1 reply to this topic

#1 drewr3737

drewr3737

    New Member

  • Members
  • Pip
  • 1 posts

Posted 09 December 2014 - 12:33 PM

Hello, this is my first post, so go easy...

 

I'm a very experienced C, C++, C# programmer and work as a developer for embedded equipment, so I have quite a bit of experience with serial communications.  I'm brand new the Netduino world tho.

 

My problem is that I have a home built RS232-TTL (using a MAX3232) converter which seems to work on 'scope but in a very simple program using Serial.Read() when my dataReceived handler get called, I get a value for serial.BytesToRead that varies, (as you would expect from a serial port) but the Read() function either never fills the buffer or it fills it will all 0's.  

 

In further testing I've noticed that with nothing attached to any of the pins (no incoming serial data, nothing is connected) I STILL get my dataReceiver handler called and there is a positive value for the serial.BytesToRead. Which does not make sense.

 

The board is Netduino Plus 2 running 4.2.2 and I am using VS2010 Ultimate with the 4.2 SDK.  I would have thought the RS232->TTL board was not working, but when I say it's home built, it's actually not, it's a device we use in house with the Ethernet module removed (we do serial to ethernet conversion on our equipment). I had an EE put the converter on a scope and he said it works perfectly.  

 

The source of the serial data is a GPS running at 4800 baud and it is sending data correctly verified by Hyperterminal on a PC.

 

I've broken down to a simple program, and here is the essence of it. In main:

 

 serial = new SerialPort(SerialPorts.COM1, 4800, Parity.None, 8, StopBits.One);
 serial.Open();
 serial.DataReceived += new SerialDataReceivedEventHandler(serial_DataReceived);
 
 
And my dataReceived handler:
 
 static void serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
 {
        while (serial.BytesToRead > 0)
        {
             byte[] bytes = new byte[serial.BytesToRead];
            serial.Read(bytes, 0, bytes.Length);
        }
    }
 
Any help would be appreciated!

 



#2 eizner23

eizner23

    Member

  • Members
  • PipPip
  • 15 posts

Posted 23 May 2015 - 04:30 PM

Ever figure this out?  I have used this Serial Port helper class (attached) in my previous projects.  

Add a reference to it, then initialize it 

//Defaults to: SerialPorts.COM1 (uses D0 and D1), 9600, Parity.None, 8, StopBits.One
        public static SerialPortHelper serialPortHelper = new SerialPortHelper();
 
in the code,  you can write like this
 
 
                
 lock (serialPortHelper){
                     serialPortHelper.PrintLine("sending a command"); }
 
reading response:
 
          
string response = string.Empty;
          response = serialPortHelper.ReadLine();

Attached Files







Also tagged with one or more of these keywords: RS232

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.