Communicating with PC

Hey guys, quick question I was hoping you pros could help me with.

Playing with a Spider mainboard at the moment…is there an efficient way I can feed it commands directly from the PC it’s plugged into?

For example, suppose I’ve got a motor plugged in and I want to spin the motor left on F1 keypress and spin the motor right on F2 keypress. Would I be able to achieve this?

welcome to forum.

Quick answer: yes

Find and read document on usb client CDC support.

If you want to debug and use CDC, you will also need to do serial debugging.

Thanks for the quick response Mike.

I found the following example for sending Hello World to the PC

using System.Threading;
using GHI.Usb;
using GHI.Usb.Client;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware.UsbClient;

public class Program
{
    public static void Main()
    {
        // Start Cdc
        Cdc vsp = new Cdc();
        Controller.ActiveDevice = vsp;

        // Send "Hello world!" to PC every second. (Append a new line too)
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes("Hello world!\r\n");
        while (true)
        {
            // Check if connected to PC
            if (Controller.State !=
                UsbController.PortState.Running)
            {
                Debug.Print("Waiting to connect to PC...");
            }
            else
            {
                vsp.Stream.Write(bytes, 0, bytes.Length);
            }
            Thread.Sleep(1000);
        }
    }
}

Seems pretty straightforward…what I’m unsure on is how I would send data from the other end, PC -> board.

it will appear as a serial port on the pc.

as @ Mike says, your device will simply appear as a Serial Port on the PC side, and you will have an app that sends data over that serial port from the PC and you’ll then react appropriately in your code on the spider. You could use simple text commands (like “L” or “R” for spinning motor left or right) or it could be more complex as you need - the good thing is that you get to manage that in your PC code. Text commands are always a good place to start because you don’t need anything more than TeraTerm connecting to a serial port, and you can free type it rather than have to write an app for the PC side to start.

Appreciate the help guys, will fiddle around with it and see what I can do. ;D

1 Like