cancel
Showing results for 
Search instead for 
Did you mean: 

Receive high speed data from stm32 via com port

LSamc.1
Associate II

Hello. I am looking for an example or help, how i can receive data from stm32 at 100kHz with Python via com port ? ADC samples at around 100Khz and streams the data i am not sure how to receive it correctly at this high speed. Thanks  a lot 

9 REPLIES 9
TDK
Super User

Save data to a buffer and send out data in that buffer as it fills up. With USB, you will need to be able to handle random delays of 50ms or so.

In Python, the pyserial module can be used to connect to a serial port and read from it.

Start with sending a few bytes and ensure that works, then try more data once that is working.

If you feel a post has answered your question, please click "Accept as Solution".

Thank you for the reply. In my case, someone wrote a code that transmits data from stm32 at 100kHz, I don't know how to receive these data correctly via usb with python? how to unsure that the data is valid and what is actual speed at which i receive data?

thank you

If you're looking for help on building the PC-side application, this isn't anything STM32-specific. It's just a serial port at that point. As I said, try using the pyserial module.

If you feel a post has answered your question, please click "Accept as Solution".
Ozone
Principal II

> ... how i can receive data from stm32 at 100kHz ... via com port ?
> ...ADC samples at around 100Khz  ...

I think this might not work out.
Even with only one channel and 12-bit samples, the throughput of your UART connection would have to be at least 100.000 x 2 x 10 bits per second. And this would be binary data, without any redundancy for error detection / correction. You need two bytes per sample, and at least 10 bits per byte (incl. start, stop, parity).

yes, I agree, according to my calculation i can only get 2880 samples per seconds.

I am not sure if this transfer is only for test/debug purposes, or the normal operation mode of the application.

If you don't need a constant stream, you might get away with storing a buffer locally, and transfer it.

Both the STM32 and PCs support RS232 baudrates in the MBit range, but this will get tricky. First, will it load the core significantly, you would most probably need a DMA feed for the transmission.
The PC side might cause problems as well. Not every USB-serial adapter (or real RS232) will work correctly with such high baud rates, resulting in a lot of corrupted bytes or drop-outs. Which in turn requires redundancy in the data to detect those (e.g. a checksum).
Not to mention, the cabling would need proper attention.

Perhaps you should consider other options, like USB or ethernet.

TDK
Super User

Is this a UART that passes through a UART to USB adapter? Or a straight USB CDC serial port? The latter should have no issues with bandwidth.

If you feel a post has answered your question, please click "Accept as Solution".

Its VCP

With 100kHz and 16bit data you have to transfer at least 1.6Mbit data per second, therefore baudrate have to be higher then 1.6M/8*10=2Mbaud. This is not particularly difficult. We usualy reaching transfer speeds in range of lower units of Mbytes/s with FT232H/4232H. You have to use hardware flow control (RTS/CTS) and some care needs to be taken when setting the baudrate (FTDI have limited options like 2,3,6,8,12Mbaud, STM32 have also limited options in that high range depending on your clock settings and you probably have to switch to "8 oversampling mode"). But it is not much complicated.

In last aplication i've measured effective speed about 4.61MB/s with Python script running on PC ... in VCP mode.