Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Communicating with Netduino via USB


  • Please log in to reply
11 replies to this topic

#1 J Wilson

J Wilson

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts

Posted 24 February 2012 - 05:43 AM

Seems like it should be possible, since programs are loaded and debugging happens via the USB cable, but is it possible to communicate with the Netduino via the USB cable? If so, are there examples available?

#2 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 1072 posts
  • LocationNorth Yorkshire, UK

Posted 24 February 2012 - 07:08 AM

Seems like it should be possible, since programs are loaded and debugging happens via the USB cable, but is it possible to communicate with the Netduino via the USB cable? If so, are there examples available?

I do not believe that it is possible for a PC to send data to the Netduino over USB but it is possible for the Netduino to send data to the PC.

Omar posted a HID example a while ago.

Hope this helps,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life

Follow @nevynuk on Twitter


#3 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 24 February 2012 - 04:05 PM

This is not possible. You can use an FTDI cable or FTDI card and a USB cable to communicate via serial port with the Netduino's software.

#4 J Wilson

J Wilson

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts

Posted 25 February 2012 - 03:09 AM

This is not possible. You can use an FTDI cable or FTDI card and a USB cable to communicate via serial port with the Netduino's software.


Thanks, Dave.

I ordered up an FTDI card from Sparkfun.

I saw on the other thread* where this was being discussed. It looks like you provided code on the Netduino end. Do you know if the code on the PC application side from inxtremo looks correct? Do you have any C# code you could share that shows sending and receiving from PC side?

*
http://forums.netdui...h__1#entry24068

#5 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 25 February 2012 - 03:17 AM

I'll try to post up some of my code soon. Mine is not 100% yet, but I can send/receive basic data.

#6 J Wilson

J Wilson

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts

Posted 25 February 2012 - 05:42 AM

I'll try to post up some of my code soon. Mine is not 100% yet, but I can send/receive basic data.

Great, thanks!

#7 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 27 February 2012 - 05:51 PM

Great, thanks!


Hey jwilson,

I got my bug fixed today and I am now receiving the correct packet back from Netduino. I'll do a bit more testing and post of some of my sample code in a couple hours.

#8 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 27 February 2012 - 08:09 PM

Ok, so here is what I have working. Keep in mind, I am breaking this code out of my working prototypes so it will need to be tweaked to work for you.

On the Netduino Side:

Serial Port Def
// The serial port
static SerialPort _port;

Init and open the serial port
// Define the port, "COM1" Netduino digital pins D0(RX), D1(TX) at 115200 Baud
_port = new SerialPort("COM1", 115200, Parity.None, 8, StopBits.One);
_port.DataReceived += new SerialDataReceivedEventHandler(ReceivedData);
_port.Open();

Event to receive data/parse data/do something with data
    	private void ReceivedData(object sender, SerialDataReceivedEventArgs e)
    	{

        	try
        	{

            	// Get the number of incoming bytes
            	int bytes = _port.BytesToRead;
            	// Create a new temporary store
            	byte[] data = new byte[bytes];
            	// Read in the bytes
            	_port.Read(data, 0, bytes);

            	// Check to see if this is a new read
            	// if so reset the system and get the
            	// length of the incoming data
            	if (!reading)
            	{
                	reading = true;
                	count = 0;
                	readLength = data[0];
                	inTray = new byte[readLength + 1];
            	}

            	// Populate the inTray with the incoming data
            	for (int i = 0; i < bytes; i++)
            	{
                	inTray[count] = data[i];
                	count++;
            	}

            	// Check to see if we have a full packet
            	ParseRecievedData();
        	}
        	catch (Exception ex)
        	{
            	writeError(ex);
        	}

    	}

    	private void ParseRecievedData()
    	{
        	try
        	{
            	// Check to see if we have a full packet. If so
            	// reconstruct data as text
            	if (count > readLength)
            	{

                	// Remove the first byte as this is the length indicator
                	// not part of the message
                	byte[] outTray = new byte[inTray.Length - 1];
                	Array.Copy(inTray, 1, outTray, 0, outTray.Length);
                	// Convert the bytes back into a string
                	string text = new string(Encoding.UTF8.GetChars(outTray));
                	// Reset the reading state
                	reading = false;
                	// Use our new data to do something
                	DoSomethingWithData(text);
            	}
        	}
        	catch (Exception ex)
        	{
            	writeError(ex);
        	}

    	}

    	private void DoSomethingWithData(string data)
    	{

        	try
        	{

            	// Example somethings
            	switch (data)
            	{
                	case "reboot":
                    	Reboot(false);
                    	break;
                	default:
                    	break;
            	}

        	}
        	catch (Exception ex)
        	{
            	writeError(ex);
        	}

On the Windows App side, the code is the same with the exception of the COM port (configure as needed on your project).

I then use this code to send from Windows --> Netduino or Vice Versa.

        	// Double check the port is open
        	if (!port.IsOpen) { port.Open(); }
        	
        	// Obtain the length of the data to be sent
        	byte[] count = new byte[1] { Convert.ToByte(output_text.Text.Length) };
        	// Obtain the data to be sent
        	byte[] outBound = Encoding.UTF8.GetBytes(output_text.Text);
        	
        	// Construct our data packet
        	byte[] data = new byte[outBound.Length + 1];
        	// Set the first byte to equal the length of the data
        	data[0] = count[0];
        	// Populate the rest of the packet
        	for (int i = 0; i < outBound.Length; i++)
        	{ data[1 + i] = outBound[i]; }

        	// Send the packet to the Netduino
        	port.Write(data, 0, data.Length);

I know the code is just pieces but you should be able to connect the Dots and use the correct name spaces. Best of luck!

#9 J Wilson

J Wilson

    Advanced Member

  • Members
  • PipPipPip
  • 30 posts

Posted 28 February 2012 - 04:55 AM

Dave, This looks good - can't wait for my Sparkfun delivery du jour. On the Windows App side, how did you declare port? Thanks!

#10 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 28 February 2012 - 08:06 AM

Dave,

This looks good - can't wait for my Sparkfun delivery du jour.

On the Windows App side, how did you declare port?

Thanks!


Windows side serial port is:

    	// The serial port
    	static System.IO.Ports.SerialPort _port;

The code is the same, with the exception that my FTDI card is on COM6.

#11 Jeka

Jeka

    Advanced Member

  • Members
  • PipPipPip
  • 38 posts

Posted 03 March 2012 - 12:14 AM

Why not possible? You can deploy and debug your software via UART and use USB to communicate with PC, but you need install 4.2 RC4 firmware and switch deploy/debug to UART via special version of MFDeploy. All this you can find in beta firmware section of forum. But, you need more experience to use this method of communication, and need additional hardware and external power to enable USB connection when Netduino loaded your software.

#12 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 03 March 2012 - 01:09 AM

Why not possible? You can deploy and debug your software via UART and use USB to communicate with PC, but you need install 4.2 RC4 firmware and switch deploy/debug to UART via special version of MFDeploy. All this you can find in beta firmware section of forum. But, you need more experience to use this method of communication, and need additional hardware and external power to enable USB connection when Netduino loaded your software.


The point is that you cannot communicate with the software loaded on the Netduino via USB. This is why you need the UART connection. You will also find that you will run into deployment issues and communication issues if you have your software running and listening on the UART while trying to deploy. Everytime I wanted to communicate via UART with my windows app, the Netduino would think I was deploying and would send debug information back instead of my data. The only correct solution was to keep these ports separate.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.