G120 RS232 Module

I’m simply trying to connect to my Terminal on my computer via the Cobra II. I have dug around for code samples and this is what I have come up with as what " I think" is right. The serial port initializes and the loop sits and “counts” to the debug.print. But nothing is showing up on my PC Terminal program. I guess I’m at a loss as to figure out how to debug this…

G120 board, with a RS232 v1.1 module plugged into socket 5 (U X?). Program runs regardless of weather the module is plugged in or not. I’m using a serial cable plugged directly into COM1 on my computer. I know my computer works because I have attached it to other “RS232” sources and I can see info in the terminal.

I’m powering my board via the small USB power port. Could this be the problem? Does GHI sell a power supply for this board? Seems like I have not seen one?

Can anyone point me in a direction???


        private static void TestSerial()
        {


            MainBoard = new GHIElectronics.Gadgeteer.FEZCobra_II();

            Gadgeteer.Modules.GHIElectronics.RS232 sp = new Gadgeteer.Modules.GHIElectronics.RS232(5);

            sp.Initialize(9600, GT.Interfaces.Serial.SerialParity.None, GT.Interfaces.Serial.SerialStopBits.One, 8, GT.Interfaces.Serial.HardwareFlowControl.NotRequired);
         
            int counter = 0;
            sp.serialPort.Open();

            Debug.Print(sp.serialPort.IsOpen.ToString());

            while (true)
            {
                // create a string
                string counter_string = "Count: " + counter.ToString() + "\r\n";
                // convert the string to bytes
                byte[] buffer = Encoding.UTF8.GetBytes(counter_string);
                // send the bytes on the serial port
                sp.serialPort.Write(buffer, 0, buffer.Length);
                // increment the counter;
                counter++;
                //wait...
                Debug.Print(counter.ToString());
                Thread.Sleep(100);
            }
        
        }

A while loop is never recommended in the ProgramStarted method. All sorts of things don’t work.

The program could look like this if you used the designer. It works.

@ Mike - Sorry, you are right of course. It was only a 5 minute test to see if the port works.

is this o.k.?


using System;
using System.Collections;
using System.Threading;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;

namespace TestSerialPort
{
    public partial class Program
    {
        // This method is run when the mainboard is powered up or reset.   
        void ProgramStarted()
        {
            // Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
            Debug.Print("Program Started");
            rs232.Initialize(9600, GT.Interfaces.Serial.SerialParity.None, GT.Interfaces.Serial.SerialStopBits.One, 8, GT.Interfaces.Serial.HardwareFlowControl.NotRequired);
            Thread WriteThread = new Thread(WriteToComPort);
            WriteThread.Start();
        }

        private void WriteToComPort()
        {
            int counter = 0;
            rs232.serialPort.Open();
            Debug.Print(rs232.serialPort.IsOpen.ToString());
            while (true)
            {
                // create a string
                string counter_string = "Count: " + counter.ToString() + "\r\n";
                // convert the string to bytes
                byte[] buffer = Encoding.UTF8.GetBytes(counter_string);
                // send the bytes on the serial port
                rs232.serialPort.Write(buffer, 0, buffer.Length);
                // increment the counter;
                counter++;
                //wait...
                Debug.Print(counter.ToString());
                Thread.Sleep(100);
            }
        }
    }
}



@ Dan Harris, can you tell us about the PC end of this connection? The RS232 module has a DB9 output, are you connecting to a “real” serial port with a DB9, or are you using a USB/RS232 converter? Also, can you confirm you’re using the right baud rate?

Try to cover all of these…

No I’m not using the designer.

I would not use a while loop when I’m done. Just trying to get something to show up on the PC before I get to deep. (Baby steps)

I have a 9 pin serial cable directly to my computer, which has a good old fashion 9 pin Com port on it. I assume it is a straight thru cable, but doesn’t say so directly on package.

Yes on baud rate.

Does it “really” matter if I’m not running this simple example in a thread? I mean all I’m looking for is something to show up in the terminal program.

Thanks for the help so far, I really don’t know how much simpler I could have made this for myself… I must missing something stupid…

OK, so if you have a DB9 on the PC, that’s great. Can you confirm you’ve had devices connected, using that actual cable, to your PC and have been able to send and receive data?

I think you need a cross over cable to connect to a PC. A crossover is needed for host to host connections. DTE to DTE.

Generally, if the connector gender on both devices are the same, you need a cross over cable. The RS-232 has male, which makes it a DTE.

@ Dan Harris - Why do you not use the designer? It is so easy I think. Both examples I posted were ready in a minute and both work, I can see the messages in teraterm.

@ Mike I will delete my first bad example. Perhaps you can delete your post too. I think it’s not necessarry to fill the form with “bad” things.

@ RoSchmi - I do not consider my posting to be a “bad” thing. Connecting two devices with RS-232 has been “fun” area for many many years. All to often the problems are related to the type of cable being used.

Thanks Mike I will see if I can put together a crossover cable. I was thinking it was something like that…

@ Mike - I am sorry, that you did not understand me right. I meant, that my code example was the “bad” thing.