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 Mini - sending data to COM2


  • Please log in to reply
11 replies to this topic

#1 Nissim

Nissim

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts
  • LocationNew Zealand

Posted 16 November 2011 - 05:26 AM

Hello I am wondering if someone can assist me I am trying to send data to comport RS232 - I get unexpected overflow buffer and then I do not get any data out private static void sendDataToComport (byte[] data) { SerialPort serialPortCP5200 = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One); if (!serialPortCP5200.IsOpen) { serialPortCP5200.Open(); } Thread.Sleep(300); serialPortCP5200.Write(data, 0, data.Length); Thread.Sleep(500); if (serialPortCP5200.IsOpen) { serialPortCP5200.Close(); } }

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 16 November 2011 - 12:43 PM

Hi Nissim, Are you using TTL for deploying (and writing over the RS232 port)? What version of firmware are you using? Also, probably unrelated but: how much data are you trying to send? Have you tried smaller chunks? Chris

#3 Nissim

Nissim

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts
  • LocationNew Zealand

Posted 17 November 2011 - 08:38 AM

Hello Chris I am using Netduino Mini I am not sure how to find out the firmware version - the time stamp is 16 Oct 2011 ( I downloaded according to your previous instruction I found online) I am reading data successfully from TTL UART (COM1) - (The new version seems to work perfectly) I am trying to send byte[17] to RS232 comport (COM2) . if I send the 17 bytes inside while(true) loop I get buffer overflown and unexpected data to my port monitor. Doing the same outside the while loop seems to work perfectly (as expected). I am puzzled by this problem - I checked my code and I see no reason to the above problem Its a wee bit tricky to debug it since COM2 is used for deployments cheers

#4 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 17 November 2011 - 12:39 PM

Hi Nissim, If you try to write out of COM2 while debugging over COM1, does that work? It sounds like you're only having an issue if you're reading/writing to both COM ports and are disconnected from the debugger? Chris

#5 Nissim

Nissim

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts
  • LocationNew Zealand

Posted 17 November 2011 - 05:56 PM

Hi Chris

Sorry if I was not clear.
I have no problem Deploying my program.
To answer your question : I debugged COM1 and the results are as expected. I cannot debug my program writing to COM2 because COM2 is used for Deployment . The tool I use to debug COM2 is COMPORT MONITOR PROGRAM which I connected to COM2 of my Mini device .
I deployed my program which is reading and attempting to write 17 bytes within While(true) loop .
The problem I have is writing to COM 2 is not working as expected . I connected my serial monitor to COM2 to capture data sent via Mini device and I get buffer overflown and a lot of rubbish. I tested same program by writing bytes to COM2 outside While ( true) loop I captured successfully data coming out from COM2 - Within my While (true) loop ,I read data from COM1 ( No problem there) and trying to send bytes to COM2 . In other words within While (true) I am reading data from COM1 and at the same loop calling a method to write data to COM 2 .

using System.Threading;
using System.Text;
using System.IO.Ports;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoMini;

namespace GPS
{
    public class Program
    {
     private static  byte[] testByte = new byte[] { 0x6f, 0x6b };
     private static byte[] testByteinsideLoop = new byte[] { 165, 104, 50, 1, 71, 1, 0, 0, 0, 0, 16, 0, 18, 19, 20, 21, 174, 165 };

         private static byte[] timeToBeUpdated = new byte[] { 0xA5, 0x68, 0x32, cardID, 0x47, 0x01, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x12, 0x13, 0x14, 0x15, 0xae };
    private static SerialPort serialPortCP5200 = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);//" COM2" TTL   Pin 2,3 




        private  static void  sendDataToComport (byte[] data)
        {
           
           
                  //      serialPortCP5200.Flush(); this line did not help 

                        if (!serialPortCP5200.IsOpen)
                        {
                            serialPortCP5200.Open();
                        }

                      serialPortCP5200.Write(data, 0, data.Length);

                       if (serialPortCP5200.IsOpen)
                       {
                           serialPortCP5200.Close();
                       }
           
        }


        public static void Main()

            SerialPort serialPortGPS = new SerialPort("COM1", 4800, Parity.None, 8, StopBits.One);//" COM1"  TTL  Pin 0,1 
          
            serialPortGPS.Open();
  

           int timerIndex = 0;
         
      
           byte[] dataReadFromGPS = new byte[] { };

          sendDataToComport(testByte); // THIS IS WORKING !!! here we sending 'ok ' 
           Thread.Sleep(5000);

        
            while (true)
        
            {
              sendDataToComport(testByteinsideLoop); // NOT WORKING !!!!!
               
                  int numberOfBytesToRead = 0;
                  numberOfBytesToRead = serialPortGPS.BytesToRead;

                if (numberOfBytesToRead > 0)
                {
                    dataReadFromGPS = new byte[numberOfBytesToRead]; // defining buffer size 
                    numberOfBytesToRead = serialPortGPS.Read(dataReadFromGPS, 0, numberOfBytesToRead); // return the actual numbers of bytes read from Comport

                    message = string.Empty;
                    foreach (var byteValue in dataReadFromGPS)
                    {
                        message += (char)byteValue;
                    }
                    var messageLines = message.Split(new char[] { '\r', '\n' });
                    string hour = string.Empty;
                    string min = string.Empty;
                    string seconds = string.Empty;
                 
                    foreach (var messageLine in messageLines)
                    {
                        var messageLineSegments = messageLine.Split(',');
                        if (messageLineSegments[0].IndexOf("$GPRMC") == 0)
                        {
                            if (messageLineSegments[2] == messageValid)
                            {
                             string     timeNow = messageLineSegments[1];
                              hour = timeNow[0].ToString() + timeNow[1].ToString();
                              min = timeNow[2].ToString() + timeNow[3].ToString();
                              seconds = timeNow[4].ToString() + timeNow[5].ToString();
                           
                            
                              foreach (var byteValue in hour)
                              {
                                hourChar += (char)byteValue;
                              }

                              foreach (var byteValue in min)
                              {
                                  minChar += (char)byteValue;
                              }

                              foreach (var byteValue in seconds)
                              {
                                  secondsChar += (char)byteValue;
                              }

                              if ((Convert.ToInt16(hourChar) >= 0) && (Convert.ToInt16(hourChar) < 11))
                              {
                                  hourChar = (Convert.ToInt16(hourChar) +13).ToString();
                              }
                              else
                              {
                                  hourChar = (Convert.ToInt16(hourChar) - 11).ToString();
                              }
                              timerIndex++;
                        //       if (timerIndex == 10)
                               {
                           //       timeToBeUpdated[9] =(StringToByteArray( hourChar))[0];
                             //    sendDataToComport(timeToBeUpdated); //  here I call  it 
                                   sendDataToComport(testByteinsideLoop);
                                 timerIndex = 0;
                       //            byte[] test = timeToBeUpdated;

                               }
       

                           }

                        }
                    }
                   

                }
       
            

            }

    
           
        }

    }
}
Thank you

#6 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 17 November 2011 - 11:18 PM

Hi Nissim, Sorry to ask this again, but do you have a USB-TTL cable that you plug into your PC (so you can debug over COM1 while testing COM2)? Chris

#7 Nissim

Nissim

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts
  • LocationNew Zealand

Posted 18 November 2011 - 04:42 AM

Hi Chris I am using COM1 to receive data. COM2 (RS232)is used to send data out Can I use COM1 to debug ? If I can then I can then I can use USB to serial adapter + MAX232 Nissim

#8 Nissim

Nissim

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts
  • LocationNew Zealand

Posted 18 November 2011 - 06:50 AM

Does Mini requires external pull up resistor( say 10k) on Rx and Tx ?

#9 Nissim

Nissim

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts
  • LocationNew Zealand

Posted 19 November 2011 - 08:30 PM

Adding pull up 10k did not help

#10 Nissim

Nissim

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts
  • LocationNew Zealand

Posted 19 November 2011 - 09:07 PM

I simplified the code and I really puzzled by this problem- I would really appreciated if someone can assist me using System; using System.Threading; using System.Text; using System.IO.Ports; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.NetduinoMini; namespace GPS { public class Program { public static void Main() { SerialPort serialPortCP5200 = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One); serialPortCP5200.Open(); SerialPort serialPortGPS = new SerialPort("COM1", 4800, Parity.None, 8, StopBits.One); serialPortGPS.Open();// COM1 uart ttl lever while (true) { Thread.Sleep(3000); serialPortCP5200.WriteByte(0x65); // I receive on my serial comport monitor ' buffer overflown ' Thread.Sleep(3000); } } } }

#11 Nissim

Nissim

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts
  • LocationNew Zealand

Posted 19 November 2011 - 10:20 PM

If I disconnect the input to COM1 ( TTL) or close COM1 in software then COM2 is working fine. Com1 is working fine while COM2 is active when debugging it In other words COM2 RS232 is not working while COM 1 is open Any idea why ?

#12 Nissim

Nissim

    Advanced Member

  • Members
  • PipPipPip
  • 54 posts
  • LocationNew Zealand

Posted 08 December 2011 - 09:22 AM

After spending countless hours on Mini comports sage . The solution to the problem I had was solved by adding a delay after sending data Thread.Sleep(1000);




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.