Python Forum
Multi-processing to communicate with microcontrollers
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multi-processing to communicate with microcontrollers
#2
You can use asyncio.
This code is not tested, but should work.

import asyncio
import struct
import csv


async def connect_and_recv(ip, port):
    reader, writer = await asyncio.open_connection(ip, port)
    data = await reader.read(1024)
    writer.close()
    await writer.wait_closed()
    result = parse(data)
    return result


async def collect_data(addresses):
    tasks = [connect_and_recv(ip, port) for ip, port in addresses]
    return await asyncio.gather(*tasks)


def parse(data):
    integers = len(data) // 4
    st_format = str(integers) + 'I'
    return struct.unpack(st_format, data)


def write_data(data):
    with open('data.csv', 'w') as fd:
        writer = csv.writer(fd)
        writer.writerow(['id', 'data1', 'data2', 'data3', 'data4'])
        for index, row in enumerate(data):
            writer.writerow([index, *row])


STARTING_TCP_PORT=  5099
ips = [
    '192.168.1.4', 
    '192.168.1.3',
    '192.168.1.5',
    '192.168.1.6',
    '192.168.1.7',
    '192.168.1.8',
    '192.168.1.9',
    '192.168.1.10',
    '192.168.1.11',
    '192.168.1.12',
    '192.168.1.13',
    ]
addresses = [(ip, STARTING_TCP_PORT + idx) for idx, ip in enumerate(ips)]
loop = asyncio.get_event_loop()
task = collect_data(addresses)
print('Collecting data')
data = loop.run_until_complete(task)
print('Writing data')
write_data(data)
print('Finished')
You should do something with exception handling.
You can decide if you kill the whole data collection process of data, if only one device fails.
Also it's a bit strange, that each esp is listening on a different port.
To simplify this, you could listem them all on the start port. Then the order does not matter.
If you need to know which esp has sent which data, the esp should send it's own id as unsigned short for example.
Then you don't need to enumerate all esps. In this case, you have to change the fortmat for struct a little bit (H on the beginning).

More ressources: https://docs.python.org/3/library/asynci...ml#streams
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Multi-processing to communicate with microcontrollers - by DeaD_EyE - Mar-01-2019, 08:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to communicate between Electron Framework and python Abdul_Rafey 0 230 Mar-09-2024, 12:18 PM
Last Post: Abdul_Rafey
  Conceptualizing modulus. How to compare & communicate with values in a Dictionary Kaanyrvhok 7 4,034 Mar-15-2021, 05:43 PM
Last Post: Kaanyrvhok
  Updating variables in Multi-processing WiPi 11 10,832 Feb-13-2021, 10:58 AM
Last Post: WiPi
  Multi-processing - problem with running multiple *.py files at the same time Antonio 5 3,813 Sep-12-2018, 01:08 PM
Last Post: volcano63
  multi-processing dR_Garuby 1 2,668 Mar-24-2018, 05:59 PM
Last Post: woooee
  How does multi-processing in python increase the execution time? kadsank 0 2,330 Jan-15-2018, 01:15 PM
Last Post: kadsank
  proc communicate not exiting on python subprocess timeout leoonardoo 0 3,793 Sep-13-2017, 09:54 AM
Last Post: leoonardoo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020