Code Compatibility

Is it possible to write a code (and make it work) on .NETMF 4.0 platform (having the code run in FEZ) and in turn, to run a device supporting .NET Compact Framework 2.0 ?
If not, what should I do to make this work?
Thanks.

A lot of the code will port across fine but not all of it, can you post a sample of what you are trying to do?

@ Justin - I’m trying to use UART interface to make an RFID reader module to run. However, it’s API only supports .NET Compact Framework v2.0. I’m doing the code in .NETMF v4.0 for FEZ to be able to “operate” the reader module. I am not sure though if I run a .NETMF v4 compiled code, if it can communicate with the reader module with the said supporting environment.

Also, I would like to ask that, since its API is to be referenced (add as reference on code), its not recognized as a .NET dll, it says that its just a File. Although when I’ve added it, add ‘using’, and write code (in which that API alone can understand), it compiled smoothly. Is it really “supported” with that API?

I was afraid I might “destroy” the reader. I was just making sure before I use this.
Thanks.

So are you trying to read a RFID tag with a FEZ?
The reader will just be streaming out some data over TTL which will be easy to decode without an API on either framework.
Or am i missing something?

And you wont “destory” the reader unless you hit it with a hammer or power it incorrectly.

here is the source code for the GHI RFID Gadgeteer module, might help you.


using System;
using Microsoft.SPOT;

using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GTI = Gadgeteer.Interfaces;

namespace Gadgeteer.Modules.GHIElectronics
{

    // -- CHANGE FOR MICRO FRAMEWORK 4.2 --
    // If you want to use Serial, SPI, or DaisyLink (which includes GTI.SoftwareI2C), you must do a few more steps
    // since these have been moved to separate assemblies for NETMF 4.2 (to reduce the minimum memory footprint of Gadgeteer)
    // 1) add a reference to the assembly (named Gadgeteer.[interfacename])
    // 2) in GadgeteerHardware.xml, uncomment the lines under <Assemblies> so that end user apps using this module also add a reference.

    /// <summary>
    /// A RFID module for Microsoft .NET Gadgeteer
    /// </summary>
    public class RFID : GTM.Module
    {
        private GT.Interfaces.Serial serialPort;

        private const int ID_LENGTH = 11;

        // Note: A constructor summary is auto-generated by the doc builder.
        /// <summary></summary>
        /// <param name="socketNumber">The socket that this module is plugged in to.</param>
        public RFID(int socketNumber)
        {
            Socket socket = Socket.GetSocket(socketNumber, true, this, null);

            socket.EnsureTypeIsSupported('U', this);

            serialPort = new GTI.Serial(socket, 9600, GTI.Serial.SerialParity.None, GTI.Serial.SerialStopBits.Two, 8, GTI.Serial.HardwareFlowControl.NotRequired, this);
            serialPort.Open();

            System.Threading.Thread readThread = new System.Threading.Thread(_readThreadStart);

            readThread.Start();
        }

        void _readThreadStart()
        {
            int bytesToRead = 0;
            byte[] readBuffer;

            string cardID = new string(null);

            for (; ; )
            {
                bytesToRead = serialPort.BytesToRead;

                if (bytesToRead > 0)
                {
                    readBuffer = new byte[bytesToRead];

                    serialPort.Read(readBuffer, 0, bytesToRead);

                    for (int i = 0; i < readBuffer.Length; i++)
                    {
                        cardID += (char)readBuffer[i];

                        if (cardID.Length == ID_LENGTH)
                        {
                            OnIDReadyEvent(this, cardID);

                            System.Threading.Thread.Sleep(100);

                            cardID = new string(null);

                            break;
                        }
                    }
                }

                System.Threading.Thread.Sleep(10);
            }
        }

        public delegate void CardIDRecievedEventHandler(RFID sender, String ID);
        public event CardIDRecievedEventHandler CardIDRecieved;
        //private CardIDRecievedEventHandler _CardIDRecieved;

        protected virtual void OnIDReadyEvent(RFID sender, String ID)
        {
            this.CardIDRecieved(sender, ID);
        }
    }
}


@ Justin - Yes, something like that.
I’ll be using this as a reference: http://wiki.tinyclr.com/index.php?title=UART_-_PC_Communication

However, I would like to ask:

  1. I dont get this part. [quote]For example, you may have a USB serial port on your PC that maps to COM8 and so you need to open COM8 on your PC, not COM1 but the NETMF program will still use COM1 because it uses UART0 (COM1).[/quote]
 SerialPort UART = new SerialPort("COM1", 115200);

COM1 refers to FEZ?
3. If a device has only one UART TX/RX, is it automatically COM1? :slight_smile:

PS: I’m just taking precaution. :smiley:

Thanks again! :smiley:

What fez are you using

@ Justin - FEZ Cobra :slight_smile:

Then you will have i think 4 Com ports, so we need to know which pins you are connecting the reader up to.

Ports

@ Justin - @ Justin - I see, so it must depend on where I connect to FEZ. But, if I load the code to FEZ, FEZ will need to use the SerialPort UART = new SerialPort(“COM1”, 115200); to connect to reader module,

should I still put COM1? or I’ll revise this such that COM port will depend on reader? If yes, the reader module however, didnt indicate COM ports… just one UART TX/RX pins. :frowning:

You can connect the RX and TX pins from the reader to any one of the 4 COM ports on the Cobra.
So for example if you connect the reader to the RX and TX pins in the middle of the Cobra you will use SerialPort UART = new SerialPort(“COM1”, 115200);
Likewise if you connect to the Com ports on the side it will be SerialPort UART = new SerialPort(“COM2”, 115200); etc.

The reader doesn’t have a com port, it just streams data from it’s RX and TX pins that connects to a COM port on the Cobra.

@ Justin - One more question… :slight_smile:
For now, I will be testing .NETMF v4 code on the reader compiled on PC. (connection is PC to reader module first) Since there’s no COM port for the reader module, what should I write on that part?

Thanks! :slight_smile:

You will need a 3.3v Ftdi USB to ttl cable which will allow you to connect a ttl device to your pc. The pc will see it as a com port so in device manager you will see a new com port like com6 or something similar. So in your code for the pc you will need to use that com port number.

1 Like

@ cluemain - the image posted by Justin shows you the available COM ports on the EMX.

If you connect the RFID reader to f.e. COM2, you need to open COM2 on the FEZ:

SerialPort UART = new SerialPort("COM2", 115200);

Make sure your RFID is outputting RX/TX as TTL level (3V3 of 5V is ok). Then connect TX from RFID to RX of the FEZ COMx port, and connect RX from RFID to TX on FEZ COMx port. Then open COMx port. (replace x with the choosen portnumber). Also make sure you connect GROUND of RFID module and FEZ.

1 Like

Thanks!

I tried running a code now. However, an unhandled exception appeared:

It was on the line UART.Open();

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

using Microsoft.SPOT;

namespace MFConsoleApplication7
{
    public class Program
    {
        public static void Main()
        {
            SerialPort UART = new SerialPort("COM29",115200);
            int read_count = 0;
            byte[] b;
            byte[] rx_data = new byte[50];
            b = new byte[] { 0x0B, 0x1D, 0x04, 0x00, 0xFF };
            var tx_data_boot = new byte[] { 0x0B, 0x1D, 0x04, 0x00, 0xFF };
            UART.ReadTimeout = 0;
            UART.Open();
            UART.Write(b, 0, b.Length);
            UART.Flush();
            Thread.Sleep(100);
            read_count = UART.Read(rx_data, 0, rx_data.Length);
            if (read_count != 27) {
                Debug.Print("Wrong. You received" + read_count.ToString());
            }
            for (int x = 0; x <= read_count; x++)
            {
                Debug.Print("Byte:" + rx_data[x]);
            }
            Thread.Sleep(100);
         
        }

    }
}

Did you mean to open COM 29?

@ mhectorgato - yes. did that. but still same error. :frowning:

Ok, open up the image of the Cobra posted by Justin and show me where COM29 is? :slight_smile:

Choose COM1, COM2, COM3, COM4… and connect the RFID reader to the pins of that port as shown in Justins image.