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);
}
}
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.
great, then your program is probably working fine, since you’re never sending anything you’ll never get a response back !
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!)
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.
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.
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.
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.
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.
@ 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.
@ 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