Using serial data

Hi,
did you change the name of the namespace of the SerialBuffer class into the name of the namespace of your project?

this in ProgramStarted part.

SerialBuffer mySerialBuffer = new SerialBuffer(256);

something to print only after a “\r\n” was sent

Edit:
sorry;


SerialBuffer mySerialBuffer;  // 
           
            void ProgramStarted()
            {
            mySerialBuffer = new SerialBuffer(256);

            } 

it seems to not change anything.
it says :

" if (lineEndPos > 0)" = false and skips it all

can you Show the code and the error Messages?
what did you send?

i use the program “terminal” to send “test”
as an message. and want that to be displayed on the char display.
::slight_smile:

im now using this code for reciving it but it doesnt seem to work.


      SerialBuffer mySerialBuffer;

  void ProgramStarted()

        {
  mySerialBuffer = new SerialBuffer(256);
            rs232.Initialize(38400, GT.Interfaces.Serial.SerialParity.None, GT.Interfaces.Serial.SerialStopBits.One, 8, GT.Interfaces.Serial.HardwareFlowControl.NotRequired);
            rs232.serialPort.DataReceived += serialPort_DataReceived;
         }

  void serialPort_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)       
        {      
            byte[] myBuffer = new byte[rs232.serialPort.BytesToRead]; 
            rs232.serialPort.Read(myBuffer, 0, myBuffer.Length);               
            mySerialBuffer.LoadSerial(myBuffer, 0, myBuffer.Length);         
            string dataLine = "";  
            while ((dataLine = mySerialBuffer.ReadLine()) != null)  
            {     
                Debug.Print(dataLine);  
            } 
        }

and the serialbuffer.cs from that other post. it goes into that till the point it reaches :
if (lineEndPos > 0) and returns null.

my old code gave me this(in the debugger console, not the char display) :
T
e
s
t

old code :

        void serialPort_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
        {

            int B2R = sender.BytesToRead;
            int myChar;

            var printstring = "";
    
            for (int i = 0; i < B2R; i++)
            {
                myChar = sender.ReadByte();
                char converted = (char)myChar;
                printstring = printstring + converted;
            }

            Debug.Print(printstring);
        }

perhaps you don’t send with CRLF.
Check +CR on your terminal program

ive tryed it with on and off.

Hi,
just tried, for me the SerialPort Class works,
try this faked message with CRLF


void serialPort_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
        {
            // byte[] myBuffer = new byte[rs232.serialPort.BytesToRead]; 
            // rs232.serialPort.Read(myBuffer, 0, myBuffer.Length);
             byte[] myBuffer = new byte[5] { 0x51, 0x52, 0x53, 0x0d, 0x0a };
             mySerialBuffer.LoadSerial(myBuffer, 0, myBuffer.Length);
             string dataLine = "";  
             while ((dataLine = mySerialBuffer.ReadLine()) != null)  
             {     
                 Debug.Print(dataLine);  
             }
             //Debug.Print("arrived");         
} 

did you open the port?


rs232.Initialize(38400, GT.Interfaces.Serial.SerialParity.None, GT.Interfaces.Serial.SerialStopBits.One, 8, GT.Interfaces.Serial.HardwareFlowControl.NotRequired);
            rs232.serialPort.Open();
            rs232.serialPort.DataReceived += new GT.Interfaces.Serial.DataReceivedEventHandler(serialPort_DataReceived);

Yes… and i cant get it to work.
if it works theire. can i get your source code? ::slight_smile: :smiley:

Yes, send me a “Direct Message” over my Profile with your E-Mail Address.
But I have to do other things in the next hours.

@ rayhvh - Use the debugger to single step and determine what is happening or add Console.Debug messages to tell you what is happening.

@ rayhvh -
Set these two checkboxes on your terminal an it should work

@ rayhvh -
now I have tested with the rs232Module (did before with the usbSerial Module) and it really did not work.
Brett’s eventhandler was the right one.
Here is now the code tested with the rs232 Module:


// This is a little test-program for Cobra 2 with a rs232 Module or usbSerial Module
// Data are received over the serial connection and are output to Debug.Print after
// the delimiter Hex 0x0A is received

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace Serial_Test_Snippet
{
    public partial class Program
    {
        SerialBuffer mySerialBuffer;
        void ProgramStarted()
        {
            Debug.Print("Program Started");

            //  this is for use with the rs232-Module ************************************************************
            try
            {
                rs232.Initialize(38400, GT.Interfaces.Serial.SerialParity.None, GT.Interfaces.Serial.SerialStopBits.One, 8, GT.Interfaces.Serial.HardwareFlowControl.NotRequired);
            }
            catch (Exception e)
            {
                Debug.Print(e.ToString());
            }
            if (!rs232.serialPort.IsOpen)
            {
                rs232.serialPort.Open();
            }
            rs232.serialPort.DiscardInBuffer();
            rs232.serialPort.DataReceived +=new GT.Interfaces.Serial.DataReceivedEventHandler(serialPort_DataReceived);
            //****************************************************************************************************


            //  this is for use with the usbSerial-Module  ************************************************************
            /*  
            try
            {
                usbSerial.Configure(38400,GT.Interfaces.Serial.SerialParity.None, GT.Interfaces.Serial.SerialStopBits.One, 8); 
            }
            catch (Exception e)
            {
                Debug.Print(e.ToString());
            }
            if (!usbSerial.SerialLine.IsOpen)
            {
                usbSerial.SerialLine.Open();
            }
            usbSerial.SerialLine.DataReceived += new GT.Interfaces.Serial.DataReceivedEventHandler(SerialLine_DataReceived);
            */
            //****************************************************************************************************

            mySerialBuffer = new SerialBuffer(256);
        }

        //  this is for use with the rs232-Module ************************************************************
        void serialPort_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
        {
            byte[] myBuffer = new byte[sender.BytesToRead];
            sender.Read(myBuffer, 0, myBuffer.Length);
            mySerialBuffer.LoadSerial(myBuffer, 0, myBuffer.Length);
            string dataLine = "";
            while ((dataLine = mySerialBuffer.ReadLine()) != null)
            {
                Debug.Print(dataLine);
            }
            //Debug.Print("arrived");
        }
       
        //****************************************************************************************************

        
        //  this is for use with the usbSerial-Module  ************************************************************

        void SerialLine_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
        {
            byte[] myBuffer = new byte[usbSerial.SerialLine.BytesToRead];
            usbSerial.SerialLine.Read(myBuffer, 0, myBuffer.Length);
            mySerialBuffer.LoadSerial(myBuffer, 0, myBuffer.Length);
            string dataLine = "";
            while ((dataLine = mySerialBuffer.ReadLine()) != null)
            {
                Debug.Print(dataLine);
            }
            //Debug.Print("arrived");
        }
        //****************************************************************************************************
    }
}

[quote=“rayhvh”]
thanks that works!! 8)