Author Topic: Can Arduino carry data over USB?  (Read 2790 times)

0 Members and 1 Guest are viewing this topic.

Offline Ben321Topic starter

  • Frequent Contributor
  • **
  • Posts: 919
Can Arduino carry data over USB?
« on: November 07, 2019, 09:30:59 pm »
From what I've ready about it, the Arduino USB port seems to just be used to update your program, so you write the program on the PC and then upload it over USB to the Arduino. I'm wondering something though. Can that same port be used to transfer data to an Arduino running a program? Let's say I want to use an Arduino as interface between an external sensor, and my PC. The data pins on the Arduino can obviously collect digital data from the external sensor's analog-to-digital converter, and then I want to send that data from the Arduino over USB to my PC, which will be running software that will collect that data and log it (save the data to a file on the harddrive). Is this possible? Or do I need another board (shield board) that adds another USB port to the Arduino, rather than using its main USB port?
 

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 4034
  • Country: us
Re: Can Arduino carry data over USB?
« Reply #1 on: November 07, 2019, 09:39:25 pm »
Yes.  The USB port acts as a regular serial port.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9964
  • Country: us
Re: Can Arduino carry data over USB?
« Reply #2 on: November 08, 2019, 12:19:44 am »
Tools->Serial Monitor is the console device in the IDE that uses the USB COM port of the Arduino.
How you format the data is up to you.  It is best if the data can be displayed as plain text on the Serial Monitor but it isn't required.
 

Offline NiHaoMike

  • Super Contributor
  • ***
  • Posts: 9321
  • Country: us
  • "Don't turn it on - Take it apart!"
    • Facebook Page
Re: Can Arduino carry data over USB?
« Reply #3 on: November 08, 2019, 02:18:14 am »
How you format the data is up to you.  It is best if the data can be displayed as plain text on the Serial Monitor but it isn't required.
That's actually a very inefficient way to pack the data. It works if the data rate is low, but it's worth learning some better encoding schemes.
https://en.wikipedia.org/wiki/Binary-to-text_encoding
For a beginner, I suggest encoding the data as hex. It's straightforward to understand and leaves a lot of symbols left over for out of band communication. For example, you can use a newline character to denote the end of packet.
Cryptocurrency has taught me to love math and at the same time be baffled by it.

Cryptocurrency lesson 0: Altcoins and Bitcoin are not the same thing.
 

Offline Renate

  • Super Contributor
  • ***
  • Posts: 1460
  • Country: us
Re: Can Arduino carry data over USB?
« Reply #4 on: November 08, 2019, 04:38:11 pm »
Aw, if you're using an AVR8 to connect a sensor over USB there are better ways than using a serial over USB.
It would be better to use an AVR8 with a USB interface, like a ATMega32u4 (Leonardo in Arduino-speak, I think).

Using serial over USB gets into problems of framing/waiting for the 0x0a (newline or whatever).
Use the USB HID sensor protocol.
Ok, you don't have to get too serious about the descriptors, just make send things in regular HID reports.
Neat, clean, you never get half a report, like with serial over USB.

The one exception to all this is if you want/had to use a serial optoisolator, in which case serial out of the AVR8 to isolator to FTDI would work.
You could also use a USB optoisolator too.
 

Online iMo

  • Super Contributor
  • ***
  • Posts: 5570
  • Country: va
Re: Can Arduino carry data over USB?
« Reply #5 on: November 08, 2019, 04:57:58 pm »
Even an atmega8 can transfer data via USB..
Readers discretion is advised..
 

Offline alsetalokin4017

  • Super Contributor
  • ***
  • Posts: 2055
  • Country: us
The easiest person to fool is yourself. -- Richard Feynman
 

Offline Ben321Topic starter

  • Frequent Contributor
  • **
  • Posts: 919
Re: Can Arduino carry data over USB?
« Reply #7 on: November 12, 2019, 08:34:21 am »
Why not just send binary data over serial? Serial ports support 8-bit bytes, and rates up to a couple megabits-per-second (at least when using a modern USB to serial converter). Of course, I don't know if Arduino supports 8-bit bytes, or if it is limited to 7-bit bytes (text mode only). And I'm not sure what its data rate limit is.
 

Offline Ben321Topic starter

  • Frequent Contributor
  • **
  • Posts: 919
Re: Can Arduino carry data over USB?
« Reply #8 on: November 12, 2019, 08:36:36 am »
Ok, you don't have to get too serious about the descriptors, just make send things in regular HID reports.

Programming USB is hard, unless you are actually programming for a serial port, and the USB just functions as a USB-to-serial converter. I think I'll stick with serial programming, if that's what it comes down to. What you can do in a few lines of code with serial (including serial over USB), takes at least a hundred lines of code to do in pure USB.
 

Offline NiHaoMike

  • Super Contributor
  • ***
  • Posts: 9321
  • Country: us
  • "Don't turn it on - Take it apart!"
    • Facebook Page
Re: Can Arduino carry data over USB?
« Reply #9 on: November 12, 2019, 01:29:58 pm »
Why not just send binary data over serial? Serial ports support 8-bit bytes, and rates up to a couple megabits-per-second (at least when using a modern USB to serial converter). Of course, I don't know if Arduino supports 8-bit bytes, or if it is limited to 7-bit bytes (text mode only). And I'm not sure what its data rate limit is.
Framing - knowing when one packet ends and the next begins - can be a problem if you don't have an "out of band" value to use as the frame delimiter. The link I posted earlier describes ways of encoding that leaves room for out of band framing.
Cryptocurrency has taught me to love math and at the same time be baffled by it.

Cryptocurrency lesson 0: Altcoins and Bitcoin are not the same thing.
 

Offline Ben321Topic starter

  • Frequent Contributor
  • **
  • Posts: 919
Re: Can Arduino carry data over USB?
« Reply #10 on: January 03, 2020, 06:01:27 am »
Why not just send binary data over serial? Serial ports support 8-bit bytes, and rates up to a couple megabits-per-second (at least when using a modern USB to serial converter). Of course, I don't know if Arduino supports 8-bit bytes, or if it is limited to 7-bit bytes (text mode only). And I'm not sure what its data rate limit is.
Framing - knowing when one packet ends and the next begins - can be a problem if you don't have an "out of band" value to use as the frame delimiter. The link I posted earlier describes ways of encoding that leaves room for out of band framing.

One thing that is guarantied with a serial port (whether native serial on a mother board, or serial-over-USB) is that every byte sent gets received, unless the physical cable gets disconnected. As for making sure the entire set of bytes in your data set is recognized properly is to prepend your raw data with a 4 byte header. That header is an unsigned 32bit integer that tells the number of bytes following the header will be your data packet. Then encapsulate everything within those simple packets, everything from control signals to data transfer. Basically when using serial, you need to create your own protocol, but fortunately that is simple. You just need to write the software at each end of the connection and program it to use your protocol.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10385
  • Country: nz
Re: Can Arduino carry data over USB?
« Reply #11 on: January 03, 2020, 07:07:36 am »
Even an atmega8 can transfer data via USB..

Technically a ATTiny85 can transfer data over Ethernet  :-DD

Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3382
  • Country: gb
Re: Can Arduino carry data over USB?
« Reply #12 on: January 03, 2020, 12:48:24 pm »
Why not just send binary data over serial? Serial ports support 8-bit bytes, and rates up to a couple megabits-per-second (at least when using a modern USB to serial converter). Of course, I don't know if Arduino supports 8-bit bytes, or if it is limited to 7-bit bytes (text mode only). And I'm not sure what its data rate limit is.
Framing - knowing when one packet ends and the next begins - can be a problem if you don't have an "out of band" value to use as the frame delimiter. The link I posted earlier describes ways of encoding that leaves room for out of band framing.

Fixed length frames makes framing very easy, though can introduce other problems.
 

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 4034
  • Country: us
Re: Can Arduino carry data over USB?
« Reply #13 on: January 03, 2020, 06:08:46 pm »
Why not just send binary data over serial? Serial ports support 8-bit bytes, and rates up to a couple megabits-per-second (at least when using a modern USB to serial converter). Of course, I don't know if Arduino supports 8-bit bytes, or if it is limited to 7-bit bytes (text mode only). And I'm not sure what its data rate limit is.
Framing - knowing when one packet ends and the next begins - can be a problem if you don't have an "out of band" value to use as the frame delimiter. The link I posted earlier describes ways of encoding that leaves room for out of band framing.

One thing that is guarantied with a serial port (whether native serial on a mother board, or serial-over-USB) is that every byte sent gets received, unless the physical cable gets disconnected.

That assumption is a good way to create an unreliable product.  Unless you have an out-of-band way to reset the device into a known state (such as by togging GPIO on the USB->serial adapter like the arduino) your microcontroller doesn't actually know when the "cable is connected."  Also, even after connection, if the device sends data before the host application opens the port the OS will drop the data.  Its really best to use explicit frame synchronization always.  Then if your host and device ever get out of sync they can resynchronize after a single message.  There are plenty of simple ways to do this depending on what data you need to transmit and what overhead is acceptable. 
 
The following users thanked this post: NiHaoMike, Kilrah

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11714
  • Country: my
  • reassessing directives...
Re: Can Arduino carry data over USB?
« Reply #14 on: January 04, 2020, 07:12:08 am »
i've done that binary data transfer in packets (64 bytes per packet for most efficient/speed transfer iirc, nearly 8KB of total data per capture) but sadly the code is in asm and you need to work closely with FTDI API on PC client side to do handshaking before any data is to be transferred reliably http://soasystem.com/eng/amla/index.htm ... provided your arduino board is using FTDI chip, not the CH341 (not sure and never tried on that china serial IC). if you transfer in HEX format, you'll have double (or more depending on what HEX format you use) the size to transfer compared to raw binary. you better just get on with standard serial communication library provided by the arduino if you only need/want to send ASCII (8 bits per byte compared to 16 bits per byte/data/character in HEX format). fwiw...
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf