Main Site Documentation

USB interface


#1

Hi there,

I would like to connect another micro controller to G120 via USB and send serial data to it and get back response from it. Can anyone suggest how could I do that ?

I am using USBHost code as follow:
When I debug this code then it never goes in usbSerial_DataReceived function, but it does goes to Controller_UsbSerialConnected function when I connect micro controller to G120. So , How could I communicate via USB interface ?

using GHI.Usb.Host;
using Microsoft.SPOT;
using System.Threading;

public class Program
{
    public static void Main()
    {
        Controller.UsbSerialConnected += Controller_UsbSerialConnected;

        Controller.Start();

        Thread.Sleep(-1);
    }

    private static void Controller_UsbSerialConnected(object sender, UsbSerial usbSerial)
    {
        Debug.Print("UsbSerial connected.");

        usbSerial.DataReceived += usbSerial_DataReceived;
    }

    private static void usbSerial_DataReceived(UsbSerial sender, UsbSerial.DataReceivedEventArgs e)
    {
        for (int i = 0; i < e.Data.Length; i++)
            Debug.Print(e.Data[i].ToString());

        sender.Write(e.Data);
    }
}

Thanks


#2

Your other micro supports cdc? What are you using?


#3

I am using BLE112 Development Board. I am not sure about CDC, but it can communicate through “realterm” software.

Here is link of development board
http://www.digikey.com/catalog/en/partgroup/ble112-development-kit/40582


#4

So your app assumes you are never initiating any sending of the data. When you just plug the board into your PC and use realterm, without sending any data to the BLE device do you get comms back from it ?

What about if in your connected handler you start a timer and then send whatever command you want so you get a response; that should prove you have comms working.


#5

In real term, I do not get any commas back , if I send something , then I get back a reply.

What code should I use to read and write via USB.

I tried usbSerial.write() and usbSerial.read(), but it doesn’t read anything from it.


#6

great, then your program is probably working fine, since you’re never sending anything you’ll never get a response back ! :slight_smile:

Untested, untried, and no doco to point me in this direction, but here’s my suggestion. If you look at the “unknown” device scenario in https://www.ghielectronics.com/docs/36/usb-host you can see there’s a USBserial device defined, you could do a similar thing but simply capture the UsbSerial object that comes in via the connected event, and then reuse it. (actually I just saw some other posts doing this, it should be good to do!)


#7

If I use unknown device scenario code, then it doesn’t even go to Controller_UnknownDeviceConnected function.

If I use serial device scenario , then it does go to Controller_UsbSerialConnected function, but then I don’t know that how can I send and receive data.


#8

I didn’t say write the handler for that. Look at how it grabs the UsbSerial object that exists outside the handler.

Inside the scope of your program, create an object:
private static UsbSerial mySerial;

Inside your handler, grab the object
mySerial = usbSerial; // second parameter from handler

Inside your timer, use mySerial.(stuff)


#9

currently, I do not have any timer. so without using timer shouldn’t I send data or not ?

private static UsbSerial mySerial;

public static void Main()
        {
              Controller.UsbSerialConnected += Controller_UsbSerialConnected;

                Controller.Start();

                Thread.Sleep(-1);
          }


private static void Controller_UsbSerialConnected(object sender, UsbSerial usbSerial)
        {
            byte[] BR_data = new byte[50];
            byte[] BX_data = new byte[5];

            mySerial = usbSerial;

            BX_data[0] = 0x00;
            BX_data[1] = 0x01;
            BX_data[2] = 0x06;
            BX_data[3] = 0x02;
            BX_data[4] = 0x01;

            mySerial.Write(BX_data, 0, BX_data.Length);
            Thread.Sleep(100);
            Debug.Print("BLuetooth Device connected.");

            
            mySerial.DataReceived += usbSerial_DataReceived; // it get to this point 

           
            mySerial.Read(BR_data, 0, BR_data.Length);


        }

//but it doesn't go in this function 
 private static void usbSerial_DataReceived(UsbSerial sender, UsbSerial.DataReceivedEventArgs e)
        {
            for (int i = 0; i < e.Data.Length; i++)    
                Debug.Print(e.Data[i].ToString());

            sender.Write(e.Data);
        }

So , I am trying to send and received data but it doesn’t work. so where am I going wrong ?


#10

all we’re trying to do here is show you that you can send and receive data to your BLE board, right ? So what you want to do is to have some simple way to send something to the BLE board, to which you expect it to respond, and you’ll then be able to see the communications go through.

So the suggestion about using a timer is important because it allows the handlers and events to finish and only after a set time will you then try to send data.

Your UsbSerialConnected handler just needs to capture the usbSerial device like you do and attach the DataReceived handler, but none of the other stuff you do. It also needs to start the timer (I suggest just before the handler exits)

Your timer just needs to send something to your device that you expect to get a response to. You can create the timer earlier but just start it in the handler. I’d suggest an interval that is reasonably long so you don’t overload things especially when you’re debugging - 10secs might be a good start.

Your DataReceived handler just needs to display what is received, don’t make it re-send it.


#11

Is there any example of timer that I can refer to it ?


#12

theres an example in the generic code that a new Gadgeteer app creates.


#13

Hi Brett,

thanks for that
Could you please show in my code that How could I use timer ?
Could you please show me that how could I send data with timer ?

private static void Controller_UsbSerialConnected(object sender, UsbSerial usbSerial)
        {
          
             mySerial = usbSerial;

            // start timer here as you said and send data 

             Debug.Print("BLuetooth Device connected.");

            mySerial.DataReceived += usbSerial_DataReceived;
                           
          
        }

      
       private static void usbSerial_DataReceived(UsbSerial sender, UsbSerial.DataReceivedEventArgs e)
        {
             mySerial.Read(e.Data, 0, e.Data.Length);
         }

// timer function 
 {
           byte[] BX_data = new byte[5];

            BX_data[0] = 0x00;
            BX_data[1] = 0x01;
            BX_data[2] = 0x06;
            BX_data[3] = 0x02;
            BX_data[4] = 0x01;

            mySerial.Write(BX_data, 0, BX_data.Length);
            Thread.Sleep(1000);

}

#14

In your Program class, outside ProgramStarted(), define a timer:

    GT.Timer timer = new GT.Timer(10000); // 10 secs

then within ProgramStarted() type
timer.Tick +=
which will define the timer tick handler.

In your USB Connected handler you need to do
timer.Start();
For robustness and completeness you probably should have a USB Disconnected handler that stops the timer.

In your DataReceived handler you need to print what data you got so you can see it.

In your timer, you don’t need to have a thread.sleep.

That’s it. Should be enough to make it work


#15

I tried code as follow, but didn’t work. Is there anything I am doing wrong ?

public class Program
    {
	
		public static GT.Timer timer = new GT.Timer(10000);
		public static UsbSerial mySerial;
		
		public static void Main()
				{

					timer.Tick += timer_Tick;
							
					Controller.UsbSerialConnected += Controller_UsbSerialConnected;

					Controller.Start();

					Thread.Sleep(-1);

				}


		private static void Controller_UsbSerialConnected(object sender, UsbSerial usbSerial)
			{
				 mySerial = usbSerial;
				 timer.Start();
				 Debug.Print("BLuetooth Device connected.");
				 usbSerial.DataReceived += usbSerial_DataReceived;
			}
		 
		private static void usbSerial_DataReceived(UsbSerial sender, UsbSerial.DataReceivedEventArgs e)

			{
				 byte[] BR_data = new byte[50];
            mySerial.Read(BR_data, 0,BR_data.Length);
            Debug.Print("Received Data:" + BR_data);
			}
			
		private	static void timer_Tick(GT.Timer timer)
        {
            byte[] BX_data = new byte[5];
            BX_data[0] = 0x00;
            BX_data[1] = 0x01;
            BX_data[2] = 0x06;
            BX_data[3] = 0x02;
            BX_data[4] = 0x01;

            mySerial.Write(BX_data, 0, BX_data.Length);
            throw new NotImplementedException();
        }
	}

#16

what didn’t work ? What did ?

You have great tools in VS in being able to step into code and debug behaviours. Add a breakpoint and step into code to follow execution.

Other things I can say about this code:

  • You look like you’ve created a standard netmf app, no longer a Gadgeteer app ? Doing that may make using the GT.Timer no longer work. Choose a type and stick with it.
  • the NotImplemented() exception in your timer is pretty pointless now you have code there, get rid of it.

#17

When I debug it, then it does go to controller_UsbSerialConnected function, but it never goes timer_Tick and usbSerial_DataReceived function.

My all other code is in netmf app, I am adding this part into it. so is there any timer function in .netmf instead gadgeteer ?


#18

sure is - in System.Threading namespace in MSCORLIB, look for Timer. Search here is a good option to find real world examples too, but this is a good time to use Object Browser and the netmf references.

Again, you will only hit the DataReceived handler when there is data available - and we’ve established that your BLE module won’t return any data until you send something.


#19

@ Akshay - Instead of reading 50 characters (BR_data.Length) in your DataReceived handler, you should determine how many characters are available and then read that amount. You will be locking in the DataReceived handler until you read 50 characters, and this will stop the timer interrupt from occurring.


#20

@ brett - I have looked for timer function in the object library as you said , but I am not sure how could I use that in my code ?
Could you please help me for it ?
That function do not have timer.start function and timer.tick