Main Site Documentation

Rs232 module: cant sent data!


#1

I have the Hydra mainboard with an rs232 module from GHI. I am just trying to get communication running between realterm and the rs232 module. I can’t get any data out over the line. With a null modem on the serial cable, i can write and read to the computer’s serial port just fine. The module is new and has never been proven to work. I am curious if someone could tell me if the following code is solid.

namespace GELFrEE
{
    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            //start a timer to send data at regular intervals
            GT.Timer time = new GT.Timer(1000);

            Debug.Print("Program Started");

            //initialize the serial port and add event handler
            rS232.Initialize(9600, GT.Interfaces.Serial.SerialParity.None, GT.Interfaces.Serial.SerialStopBits.One, 8, GT.Interfaces.Serial.HardwareFlowControl.NotRequired);
            rS232.serialPort.DataReceived += new GT.Interfaces.Serial.DataReceivedEventHandler(serialPort_DataReceived);
            rS232.serialPort.Open();

            time.Tick += new GT.Timer.TickEventHandler(time_Tick);
            time.Start();
        }

        void time_Tick(GT.Timer timer)
        {
            if (!rS232.serialPort.IsOpen)
                rS232.serialPort.Open();
            //output "sent" every second
            rS232.serialPort.WriteLine("sent");
            Debug.Print("sent a line");
        }

        void serialPort_DataReceived(GT.Interfaces.Serial sender, System.IO.Ports.SerialData data)
        {
            byte [] inbuffer = null;
            if (!rS232.serialPort.IsOpen)
                rS232.serialPort.Open();
            //Try to read anything in buffer
            rS232.serialPort.Read(inbuffer, 0, rS232.serialPort.BytesToRead);
            Debug.Print(inbuffer.ToString());
        }
    }
}

The settings specified in the port initialization are identical to those used in realterm. Nothing is sent nor received on the line. Even when I send something from realterm, it doesn’t fire the datareceived event. Am I missing something?

Thanks!


#2

You need to open port first and then subscribe for event.


#3

Hi Architect,
Thanks for the reply. I wish it had been that simple.

Rearranging the code to:

 rS232.Initialize(9600, GT.Interfaces.Serial.SerialParity.None, GT.Interfaces.Serial.SerialStopBits.One, 8, GT.Interfaces.Serial.HardwareFlowControl.NotRequired);
            rS232.serialPort.Open();
            rS232.serialPort.LineReceived += new GT.Interfaces.Serial.LineReceivedEventHandler(serialPort_LineReceived);

doesn’t solve the issue. Even with a null modem on the device and sending data during the tick event, the datareceived event still never fires. I dont think the data is ever being sent, eventhough the writeline is executing. Any other ideas? I do have the rs232 module plugged into socket 5, and this is where the device is plugged in the designer, just in case you wanted to go down that road. I have looked around for the better part of 2 days for examples where people used this module. If anyone can point me to some, it would be much appreciated.

Thanks again.


#4

The code you posted would result in a null exception when you try to perform a read. You never allocated the input buffer.

*** I just checked the source code for the GHI RS232 driver ,which I got from http://gadgeteer.codeplex.com/SourceControl/list/changesets

The initialize method does the open for you. You do not need to open after the initialize. Second open may be causing a problem.

I find it useful, maybe necessary, to check the Gadgeteer source code for a module when I first start to work with it. It always saved me lots of time.


#5

Gentleman,
Thanks for your help. I finally figured out what was happening here. I checked out the rs232 module source and it is fairly simple. You were right, the Open is called by the initialize method, but it doesn’t seem to hurt anything if called again. I switched out the datareceived code to just print a sting saying it saw something. After doing this, I could get it to trigger the event 1 time at program startup and then it was silent from then on. Turns out, the write was still transferring data from the buffer when it hit the datareceived event the first time, but i couldn’t see it because i wasnt converting the byte [] to a string. Once I did this, I saw that the write was placing new characters in the buffer, as I was reading out the data that were already transferred when the program hit that code. Placing a Thread.Sleep(250) after the write gives enough time for all of the data to be sent prior to the read. The read then successfully empties the input buffer so that the event can trigger again once new data arrives.

Thanks again!