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

netduino arduino serial interface


  • Please log in to reply
1 reply to this topic

#1 mr_sargent

mr_sargent

    New Member

  • Members
  • Pip
  • 4 posts

Posted 01 April 2012 - 09:41 PM

I am working on a project that requires me to read an encoder signal. I am using an arduino board to read the encoder input and send the data to the netduino via serial transmission. However, I am running into problems sending this data from the arduino to the netduino. I can view data the arduino is sending through a serial monitor program on my computer but I can't seem to receive it with the netduino.

The netduino seems to be producing random numbers that I am viewing in the debug window. Also, the trasmit led on the arduino is not activating like it does when viewing the data with a serial monitor. I have made sure that the com ports are the same for both devices and the transmit pin of the arduino is connected to the receive pin of the netduino.

Does anyone see errors in my code or have a suggestion for me to try? Thanks for any help.

Arduino code
enum PinAssignments
{
  encoderPinA = 2,
  encoderPinB = 3,
  clearButton = 8
};

volatile unsigned int encoderPos = 0;
unsigned int lastReportedPos = 1;

boolean A_set = false;
boolean B_set = false;

void setup(){
  
  pinMode(encoderPinA,INPUT);
  pinMode(encoderPinB,INPUT);
    
  attachInterrupt(0,doEncoderA,CHANGE);
  attachInterrupt(1,doEncoderB, CHANGE);
  Serial.begin(9600);
}

void loop(){
  
  if(lastReportedPos != encoderPos)
  {
    Serial.print("Index:");
    Serial.print(encoderPos, DEC);
    Serial.println();
    lastReportedPos = encoderPos;
  }
}
  //interrupt A
    void doEncoderA(){   
    A_set = digitalRead(encoderPinA) == HIGH;
    encoderPos += (A_set != B_set) ? +1 : -1;
  }  
  void doEncoderB(){
     B_set = digitalRead(encoderPinB) == HIGH;
     encoderPos += (A_set == B_set) ? +1 : -1;
  }


netduino code
namespace read_encoder
{
    public class Program
    {
        const int MaxValue = 40;
        static SerialPort com;
        static byte[] ByteBuffer = new byte[MaxValue];
        static int index = 0;

        public static void Main()
        {
          com = new SerialPort(SerialPorts.COM1, 9600, Parity.None, 8, StopBits.One);
          com.DataReceived += new SerialDataReceivedEventHandler(com_DataReceived);
          com.Handshake = Handshake.None;
          com.Open();
          Thread.Sleep(Timeout.Infinite);
                             
        }

        static void com_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            byte[] bytes = new byte[1];

            while(true)
            {
            com.Read(bytes, 0, bytes.Length);
            ByteBuffer[index++] = bytes[0];

            if (index >= MaxValue)
            {
                string line = "";

                for (int i = 0; i < MaxValue; i++)
                {
                    line = ByteBuffer[i].ToString();
                }
                Debug.Print(line);
                index = 0;
            }
            }
        }

    }
}


#2 SpitfireMike

SpitfireMike

    Member

  • Members
  • PipPip
  • 17 posts
  • LocationColorado Springs, CO, USA

Posted 12 April 2012 - 09:51 PM

Garbage chars in serial usually means mismatch between rate, databits, parity or stop bits. You're specifically setting the Arduino's rate, might want to try also setting the other params (the port may have been configured otherwise).




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.