Main Site Documentation

Can't read from serial ports


#1

I’m almost too embarrassed to post this, but I have spent several days trying to reading from serial ports on Spider and Cerberus CPU modules without luck. Writes to the serial port are working correctly from either NETMF or Gadgeteer.

My “home brewed” Telnet and FTP servers over WiFi are working well on the Spider and RS21 modules.

All of the Spider boards are running version 4.2.10.1 of the TinyCLR firmware and release 4.2.9.0 of TinyBoot. I believe there is a newer release of TinyBoot but I don’t know how to update my hardware to use it. The link in FEZ_Config_V013 explaining how to update TinyBoot was not correct a few days ago.

Here is the code snippet that fails (you should recognize it, it was lifted from this web site with slight modifications to the messages):

using System.IO.Ports;
using System.Threading;
using System.Text;
using Microsoft.SPOT;

// Fez Spider sockets :
// Com1 : 11
// Com2 : 4
// Com3 : 8
// Com4 : 9

namespace Read_Without_Events
{
public class Program
{
public static void Main()
{
SerialPort UART = new SerialPort(“COM3”, 38400, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
int read_count = 0;
byte[] rx_byte = new byte[1];

        UART.Open();
        byte[] msg = Encoding.UTF8.GetBytes("Testing Read without use of events\r\n");
        // send the bytes on the serial port
        UART.Write(msg, 0, msg.Length);

        while (true)
        {
            // read one byte
            read_count = UART.Read(rx_byte, 0, 1);
            if (read_count > 0)// do we have data?
            {
                // create a string
                string counter_string =
                      "You typed: " + rx_byte[0].ToString() + "\r\n";
                // convert the string to bytes
                byte[] buffer = Encoding.UTF8.GetBytes(counter_string);
                // send the bytes on the serial port
                UART.Write(buffer, 0, buffer.Length);
                //wait...
                Thread.Sleep(10);
            }
        }
    }
}

}

I tried the above code on several Spider and and Cereberus CPU modules and with several different RS232 modules. I verified that I could do a “loop back” test on my development host. If I typed characters on the host, they would echo correctly on the terminal emulator running on the host.

The RS232 module was plugged into slot 8 of the CPU spider board. (COM3 maps to slot 8)

I also verified that the start bit, data bits and stop bit were correct on the RX signal (pin 15, IC1) of the RS232 module, when a character was typed on the host. I believe that everything was wired correctly and the terminal emulation software was configured correctly.

I probably have done something stupid. Don’t I need to configure the GPIO pins as UART I/O? (Or, does that happen as part of the SerialPort.Open() method?)

Standing by to eat a large slice of humble pie… :frowning:

Thanks,
Wayne


#2

@ farangutan - Since the RS232 module and the PC are both DTE equipment, you should be using a crossover/modem eliminator cable. Are you?


#3

First, thanks for the “heads up” about posting code.

The comm problem was determined to be a corroded null modem adapter (the ocean is a harsh place), that sometimes worked and sometimes didn’t.

Replaced the flaky null modem adapter and everything worked correctly.

After three plus decades of embedded software work, I thought that I had seen every flavor of bad hardware. An intermittently bad null modem adapter is something that I hadn’t encountered before.

Thanks for the suggestions…
Wayne