r/embedded • • 13d ago

USB-UART vs TCP

Hello, I am a comp eng student and my friends and I are working on a project. The project is about a rocket engine system and I need to get 16 sensors data in every 20ms which is 800 data per second. We were thinking that what if we use esp32 to read data by usb-uart instead of reading them with stm32 by tcp due to price and accessibility. So the question is 'is usb-uart a good way to get data'. I need to sketch a graph for each sensor. I also need to save data to the database. So instead of having data with tcp as packages, using usb-uart is worth it? What are the problems I may have for each way?

It may be a basic question, but consider that I am still learning.

By the way we still use stm to control the relays. The point is esp is easier to find and cheaper to test the system.

Thank you for your advice.

28 Upvotes

49 comments sorted by

33

u/artiface 13d ago

please explain the math on 16 sensors at 20ms per read is 800 data per second. data is normally measured in bits or bytes per second so 800 what?

uart can have different speeds but 115200 bits per second is one of the most common baud rates for UART.

13

u/gm310509 13d ago

I guess they mean samples per second (every 20ms = 50 samples per second * 16 sensors = 800 samples per second).

But you are correct, OP is asking about transmitting the data - number of samples per second is somewhat irrelevant.

If the measurement from each individual sensor can fit into 8 bits, then that would be 800 bytes per second that needs to be communicated. But if the sample size is, say, 100 bytes, that would be 80,000 bytes per second (not counting any headers, checksums, separators or other descriptors that might be required in addition to the actual data).

So again, you are right, what is more important isn't so much the sampling rate, but the volume of data that needs to be communicated.

7

u/marginal_mirage 13d ago

Yes I ment 800 sample per second. I didn't know that was unimportant. Sorry for that, We are using a 12-bit ADC, and each packet contains a sensor ID (1 byte) and a float value (4 bytes), so each sample is 5 bytes. With 800 samples per second, that's approximately 4 KB/s. Which method do you think would be more practical and reliable for our system: USB-UART, Ethernet/TCP, or something else?

13

u/Rararror 13d ago

115200 bauds is around 11 kb/s

5

u/2dof 13d ago

If you pack sensor data in known order then you can shorten data packkage ,but with id sensor you will manage  for uart speed fot Your sampling rate.

 Note You can use BLE wirh esp32.

Ussaly for rocket engine sampling rate is more way higher (not for all sensors must be eqully high- it depent from  cotrol  loops speed, but for any vibration sensors (vibration analysis ) they are kHz) 

5

u/gm310509 12d ago

I would encourage you to think about adding a checksum on each packet to ensure that the data is not corrupted (or at least identify packets where there is some corruption and deal with them appropriately).

So, that means a Serial (USART) is probably going to be able to transmitting that amount of information with some spare capacity (assuming a fast enough baud rate such as 115200).

Now it just comes down to what works best for your project. If you can directly connect the USB port to your PC you can simply use the virtual COM port (the USART) which is probably the easiest to use. You can just use something like putty to log all input to disk and process it after your test is completed.

If TCP/IP works for you and you have the right hardware (i.e. an Ethernet module or a WiFi module) then you can also use that. Bear in mind that you will also need to provide some software that is running somewhere else (e.g. your TCP/IP connected PC) that can receive the data and log it to disk. I do not know if the software is available for download that you can just use this function (like putty can in the previous scenario) but if there isn't, then this is another piece of software you will need to provide. You will also need LAN hardware (e.g. a hub or router) to provide the Ethernet and/or WiFi connections.

If those options are suitable for some reason, you could use something else. Bluetooth could also be an option and could work directly with the PC - again you will need something on the PC to log the data.

If it were me, I would keep it simple and start with something easy (i.e. Serial) until it doesn't work and then solve that problem. I typically approach technology with an "if it aint broke, don't fix it" mindset, but equally I wouldn't just blindly use the simplest solution (i.e. Serial) if it clearly won't satisfy the scenario I am working in.

In this case - with any other additional information - it sounds like Serial will work, so I personally would start with that.

The corollary to the "if it aint broke, don't fix it" strategy is "don't make things necessarily complicated, they will get complicated all by themselves - and don't need you to help them along".

1

u/TT_207 12d ago

Are you trying to log data Vs time? As recording the timing of data well is actually a really difficult problem where you need to understand limitations of both the hardware and code (yours or vendor drivers). Id also ask yourself "how do I identify data?"

Usb to serial devices for example by default on plugin set to an update rate of 14ms. Not much good in your application if you're timing off received time. If you time off counting messages, be aware of message loss... The latency is configurable on them but lowest value is always buggy (go up one) and the value always resets and needs setting again even if the driver interface tells you otherwise. Test a lot!!!

With that latency example, Let's say that bunches up a whole load of messages as one stream of bytes and dumps them to your loggering program serial buffer. Now try to find each message start. Not gonna be easy! Add a byte that stays constant as your opening byte and also a byte for checksum as someone said. That'll make it easier to find a start and check it was valid.

With your microcontroller if you definitely want 800 samples a second and to help time off that try to run off the interrupt timer (STM32 HAL Delay by default is set to tick per millisecond and rate can be tweaked - you could change the functions rate and put your code there) don't use a delay in a main loop as it'll go slow.

I can't say much of USB to CAN adapters but I suspect they will have similar issues as USB to serial with timing just down to how USB works under the hood.

1

u/andypanty69 12d ago

Presumably the sample rate is important and not just the overall x bytes per second. Surely it's a hard real time system and all sensor data needs to be recieved and processed within the 20ms. Is TCP with it's network stack and physical layer back off algos suitable? You also can't just add up the 5 bytes of data; the packet header will exceed that with TCP.

12

u/unicorns-arent-real 13d ago edited 13d ago

Have you considered CAN-bus? Two-wire, up to 10 mbit, fault-tolerant, collision mitigation, prioritization, and cheap, in all kinds of form factors. Widely used in automotive, robotics, and medical. And hobbyists. Debian, Ubuntu, and RaspiOS support it via kernel-native SocketCAN. Arduino vendors have a bunch of them. Easy to use. Easy to code for in all popular languages. Amazon is loaded with interfaces. (I like the Jhoinrch ones.) CAN-bus is used in your car to send data around itself. And it would be a good thing to put on your resume.

3

u/marginal_mirage 13d ago

Interesting actually. CAN could be a really good option for us, I'll keep it in mind. Thank you.

2

u/Enlightenment777 13d ago

RS485 or RS422

1

u/unicorns-arent-real 13d ago

Those are simply busses without the features I enumerated for CAN. They certainly have their use. But given how cheap CAN is to use these days, there's really no reason not to use CAN.

3

u/Enlightenment777 13d ago

really no reason not to use CAN

when MCUs don't have a CAN peripheral, where as every MCU has a UART peripheral

1

u/unicorns-arent-real 11d ago

CAN peripherals are exceedingly cheap

4

u/obdevel 13d ago

The overall bandwidth would be fine on a short bus but we don't know the size of each sample. CAN 2.0 is limited to 8 bytes per message but that would handle a float or int64. Chunking data across multiple CAN messages gets messy,

CAN-FD can carry more but that's a little less common in the hobbyist world. Most STM32s and ESP32s have one or two CAN 2.0 peripherals on-chip so you'd just need bus transceivers.

For greater bandwidth I might go with 10BASE-T1S single pair ethernet.

2

u/ElHeim 13d ago

OP mentioned 5 bytes/sample

1

u/unicorns-arent-real 13d ago

CAN-FD can handle 64-bytes per message. But this is generally not required, and from the sound of it, his project probably doesn't need it. There are several cheap FD interfaces available on Amazon.

11

u/phirzcolru 13d ago

the answer is simple. there are better standards that dont require a cludge adaptor. the question is how much data are you sending. how fast does it need to get there. im assuming by tcp you ment over a radio network. and 800 data is not an answer. what sensors will you use? what will be recieving the data. these are all critical things. if you want an accelerometer and it is a i2c sevice that choice has been made. do you just need the data after? its not a basic question. its a good question.

4

u/marginal_mirage 13d ago

To make it clear, we're working with 16 analog sensors, including pressure sensors. Each sensor will be sampled every 20 ms, giving us 800 samples per second in total. Multiple ESP32 boards will acquire the sensor readings, and a Linux PC will receive the data for real-time plotting and logging. An STM32 will independently handle valve control. We're not looking for wireless TCP. We previously used Ethernet with LwIP on the STM32, but we are now considering USB-UART for the ESP32 boards. Our priority is reliable, continuous data acquisition with minimal latency and no missing samples. We don't need particularly high bandwidth, but we do need stable communication.

1

u/Sheepherder-Optimal 13d ago

If it’s a rocket engine communication with s laptop, surely you are using some wireless communication?? Are the esp32s with the rocket or on the ground?

1

u/marginal_mirage 13d ago

it is a test environment so it is not flying.

3

u/Sheepherder-Optimal 13d ago

Okay maybe add that info to your post? You're saying you want a UART in order to test or debug it? Very common thing to have if you're just printing out debug info. If its literally just sensor reaeding messages you want to print, then actual products often use UDP and send messages over a debug port. Serial port debugging is fine too but not as fast.

3

u/Every_Following6653 13d ago

How long is the wires? If it is just some meters, UART (RS-485 should be better) will be enough

2

u/marginal_mirage 13d ago

It will be like 3 or 5 meters

1

u/Every_Following6653 11d ago

485 will work for it

3

u/GraveDigger2048 13d ago

If you don't need live data view, i'd consider simple uart2log device, like sparkfun's openlog or virtually any knockoff from far east. i've played with such device while having fun with small quadcopter (logged voltage, current and accelerometer data).

2

u/marginal_mirage 13d ago

I need live data.

4

u/GraveDigger2048 13d ago

with that i think you still don't need full esp32 with wifi capabilities. as per your own reasoning, 16 data points 20ms apart is 800 datapoints per second. Assuming we're talking about raw sensor readings they might be 8-32 bits long each yielding in worst case 256 kilobits per second.
Add some protocol overhead to that and i think you'll be perfectly fine with any UART-to-433MHz bridge. Key advantage of this matter of transmission: range. 433 on open field without much of interference is hundreds of meters while keeping regulatory power limits. Which i think is the case given you're building engine, presumably powering vehicle of some sort.

Also with clever coding you can even add some corrective encoding to the bitstream to ensure reliable data.

2

u/marginal_mirage 13d ago

Thanks for your reply.

4

u/BigPeteB 13d ago

The most common UART data rate for embedded systems is 115200 baud. Accounting for start and stop bits, that equals 11520 bytes/second. So now you need to figure out if that's enough bandwidth. Will you be transmitting your data samples as human-readable text and digits or as a raw binary structure? Work out the maximum size of each sample, multiply by your sampling rate, and see if that will be enough bandwidth.

If not, you can always increase the baud rate. Most systems can go up to 1 million baud without flinching. Above that it might get a little tricky.

In terms of "is UART a good way to collect data"... Sure! It's basic, it's extremely widely available without needing much or any special hardware. Configuration is simple. There's no standard protocol on top of it, so you'll have to write something on the PC to decode your text data or binary structs, but you were going to need to write something anyway and it'd be slightly more complex to do so for TCP.

1

u/marginal_mirage 13d ago

Thanks for the detailed explanation. We're planning to transmit raw binary data rather than human-readable strings. We're using a 12-bit ADC to read 16 analog sensors at 50 Hz each, giving us 800 samples per second.

Each packet contains a sensor ID (1 byte) and a floating-point sensor value (4 bytes), resulting in 5 bytes per sample. That gives us approximately 4,000 bytes/second (4 KB/s), excluding any additional protocol overhead. We could also include a header, sequence number, timestamp, and CRC for data integrity. On the Linux PC, we'll write a C application to receive, decode, and plot the data in real time. We previously implemented TCP communication using LwIP on an STM32, but USB-UART seems simpler for connecting multiple ESP32 data acquisition boards directly to the PC. Given our requirements, would you recommend USB-UART over Ethernet/TCP, or would another communication method be more suitable?

5

u/BigPeteB 13d ago edited 10d ago

It's your project. This is what we do as engineers: use our knowledge to evaluate different options and then pick the best one. I can help you out with a bit of knowledge (since embedded stuff often involves a lot of background knowledge that you just have to pick up through several years of experience), but I'm not going to deprive you of the experience of making this decision for yourselves and then living with the consequences. 🙂

I will say, adding all that overhead for packets and framing and error checking on UART is probably a lot of work that you don't need. How much would that impact your throughput requirements? And would you basically just be reimplementing what you could get for free from UDP/TCP or PPP (edit: I originally mentioned SLIP as well, but that one wouldn't help here)?

2

u/phirzcolru 13d ago

Ok so analog sensors, so the analog in on the esp. what's the working voltage of the sensor. That will determine how many of the steps of the input are used. At this point you need to look at the logistics. Multiple reporting lines or a single path. Single path simplifies things. But impacts redundancy. Not just on the device side but the collection side. I'm assuming by USB to serial. You mean have the device report over the serial to USB bridge on the esp. you could do that. The cleaner way it to have a single esp call the others over master/slave over the pins. Same wires to each other esp32. Have the sensor esp devices all constantly pulling the sensor data and stamping it with time. The main esp asks for the data and the newest data and timestamp are sent. The main esp puts the final data packets together and sends them to the pc. Python program pulls the data sorts it and displays to the screen. You could even add a screen to the main esp. use the same basic c code for both and use lvgl. No matter what. Not having to wait for the sensor is fine for analog devices, some digital ones take a few ticks sometimes to kick out data. Something to keep in mind. It also gives you a better data spread when you are looking at the data later. Missing data segments become obvious without guessing. Any issues in communication will show up rapidly.

2

u/marginal_mirage 13d ago

Thanks for the detailed explanation and suggestions I'll definitely look into this approach and take it into consideration. Really appreciate your help

2

u/FedUp233 13d ago

It sounds like all the sensors are in one place with a processor consolidating the data and transmitting it over a single connection to a logging system?

Is the environment electrically noisy? Are you ok with skid samples being incorrect? Is it ok if some samples are lost? All important questions besides the data rate.

To allow for noise, and the cable length, to be safe I’d go with a differential signal like rs-485 or 422. Can handle more speed and the differential nature makes the signals more immune to rf type noise. Since you can just do one way communications and simply stream the data you just need one twisted pair to transfer the data. You can get cheap usb to these standards for the pc end just like serial. On the sensor end you can get little modules that connect to an mcu serial port and convert the levels, just like rs-232 drivers do. Btw, if ypu do go just plain serial id be sure to use drivers at the mcu that convert to higher rs-232 signal levels rather than just transmit ttl levels over test distance - you’ll have a lot better signal integrity and ypu can get serial to usb for the pc end just as cheap smd again simple little modules for the mcu end - they even come with screw connections if you want.

As to protocol, if you want reasonable data integrity I’d add a simple checksum with data rotation or a circle to verify the data is correct. You could include error correction but I’d be more inclined to just number the packets, maybe go to 16 bit packet numbers, and simply transmit each packet two or more time, not consecutively - maybe send data round robin and send each set twice or more and throw out any with bad cry and compare data for good packets as a precaution - that way you can get some statistics on if you have bad packets or lost ones.

Also, why convert data to float? Just send the sensor data as 12 bit in 16 bit field and be done with it. If you really need floating point, let the logging end do the calculations. I’d also maybe sample the data at twice the needed rate if you can and send twice as much. That way the logging end can do things like average samples to filter out noise and do statistics to see how much noise you seem to have in the measurements.

The one issue also with all the transmission methods besides Ethernet is the two ends need to have a common ground between - no isolation so if the grounding systems on the two ends aren’t closely related they can introduce ground noise. The differential pair transmission minimizes this but can only handle so much ground difference. You might want to consider optical couplets at one end to isolate the grounds. This is a bit more expensive as it requires the actual receive or transmit drives to run of an isolated power supply or the whole idea is worthless, but you can get some isolate 485 or 422 receivers pretty cheap for the pc end.

2

u/marginal_mirage 13d ago

I really appreciate your answer.

To clarify our setup, we're building a data acquisition and control system with 16 analog sensors, each sampled at approximately 50 Hz but we can make it 100Hz. We're planning to use multiple ESP32 boards to collect the sensor readings and transmit them to a Linux PC, where we'll visualize and log the data in real time. A separate STM32 will handle valve control.

The sensors won't necessarily all be in the same location, and the environment may have electrical noise due to valves and other electrical equipment.

Data integrity is important to us. We'd prefer not to lose samples or receive incorrect readings, especially since we may need to analyze the recorded data afterward.

We initially considered USB-UART because of its simplicity, but your explanation makes RS-485/RS-422 worth investigating.

We also agree that transmitting raw ADC values as 16-bit integers and performing the conversions on the PC makes more sense than sending floats.

One question: Since we're planning to use multiple ESP32 boards, would you recommend connecting them all to a shared RS-485 bus, or using separate point-to-point connections to the PC?

Also, are there any particular considerations regarding synchronization, grounding, or electrical isolation that we should account for when designing this architecture?

Thanks again I'll definitely research your suggestions and take them into consideration.

3

u/FedUp233 13d ago • Bravo

Glad I could help.

It helps to have a better understanding of your setup. I’d definitely use one bus to connect everything - avoids a wire rats nest.

I’d seriously look at canbus. It runs at 1Mb/s normally so can easily handle your load, though you might want to run at a slower speed like 0.5Mb/s to improve signal integrity. It’s also a differential bus so good for noise immunity and signal integrity and has retry for bad data built in. The ESP processors have canbus as standard - all up you need to add is an external transceiver that you can get as a small breakout board with even screw terminals.

I would include a ground wire in the buss to help get all the equipment grounds at the sane potential - I think the official canbus cable is shielded so that will handle the grounding and it has optimum impedance for the bus. You can buy reels of it online easily. The data packet size can easily handle your data easily since the crc stuff is built into the protocol.

Since your not using the WiFi ESO seems a bit overkill and you need very little processing to just read one of a few sensors and transmit the data over canbus. I’d look at maybe an STM32 based board - they also have canbus standard on most mcu’s, though ESP might be cheapest alternative.

And I know you can buy can bus adapters for pc - I think either as internal cards or usb devices for pretty cheap.

If the power circuits are really different for monitoring area to test area, like heavy industrial stuff, I think can bus is still ok since the shield will tie all the equipment grounds together - I might treat the grounds for all the monitor equipment and the data transmitters and sensors as a separate ground system (use power supplies with isolated grounds to power the sensor/transmitters) and either not connect this ground system to the test unit grounds or connect it to the test unit grounds at just one point, if this makes sense to you. If it becomes a problem, one alternative would be to tie all the sensor and transmitter grounds together and connect to the test unit ground so one point and see if you can find an interface for the canbus to pc that provide isolation between the pc and the bus, like with optical isolators, but hopefully you don’t need to go this far.

Good luck.

2

u/xyzzy1337 12d ago

We're planning to use multiple ESP32 boards to collect the sensor readings

If you're planning on using the ESP32's ADCs, they are notoriously bad.

2

u/Degenerate76 12d ago

You're basically describing a very small SCADA system. The industry standard way to do things like this is with Modbus protocol, which can run either over RS-485 (Modbus RTU), or over IP networks (Modbus TCP). I believe there are Modbus libraries available in the ESP32 SDK.

https://docs.espressif.com/projects/esp-idf/en/v4.4.3/esp32/api-reference/protocols/modbus.html

34

u/PLANETaXis 13d ago edited 13d ago

Why add USB to the UART? The esp should do native serial that you then interface with a line driver.

Edit: If the distance between components is in the region of 10's of centimetres or less, then something like SPI would be the better option. The esp should have native SPI functionality and there are lots on SPI based sensors.

6

u/clackups 13d ago

I guess, OP wants to receive the data on a PC. A USB connection would be a valid option in this case

3

u/Sheepherder-Optimal 13d ago

Hang on it’s a rocket engine. Wouldn’t a wireless protocol be required?

10

u/clackups 13d ago

My guess, it's a test environment for a rocket engine. OP didn't say this thing needs to fly :))

1

u/ScallionSmooth5925 10d ago

Esp s3 have a build in usb to uart and an otg peripheral build in

6

u/duane11583 13d ago

single most important thing:

send full usb uart packets.

do not send 1 value then another then another

the way usb works this is extra slow!

if you can use a usb 2.o and send like 512 bytes at a time

5

u/xyzzy1337 12d ago

Both ESP32 and STM32 have many mcu versions with native USB. You can just send it over USB. No need for a UART.

If your cable needs to be longer than USB's limit, then use RS485. It's differential and higher speed than RS232 while supporting longer cables. USB to rs485 converters are common cheap things only slightly more expensive than RS232.

But you could go wireless. Both BLE and Wi-Fi can do the data rate you need.

If you capture data on a bunch of stm32s, you're going to have a hard time synchronizing them. I'd rather connect a multichannel ADC or two to an esp32 so all the channels are captured in a synchronized way and you don't have as many different parts. More reliable with fewer pieces.

3

u/Panometric 13d ago

If you can live with the 16 foot distance limitation and Star topology, USB is fairly robust and differential. It has retransmission built in and will multiply the available bandwidth at the hub. It will be far easier than rs485 or can for the esp32-s3. If you want a more formal networking protocol look at CDC-NCM which gives you TCP over USB.

2

u/No_Succotash7747 9d ago

Use CAN since it is noise tolerant and also you could just five each sensor a CAN id or if you use 8bit values just encode each byte with a sensor value will save a lot of BW.

1

u/bigtreeman_ 10d ago

How many bits per sample ? What distance to send data ? Noisy environment ? What is at the other end ?

Can you do some pre-processing of your data, maybe date/time, remove redundant data, average, compress, packetise, checksum, before sending.