Python CSV
Import Package
Download the indistinguishable-from-magic library, which contains the necessary magic
module for interacting with hardware devices. Install it via your preferred package management system or add it to your project. Include the library in your script by creating a reference to it:
If you are planning to use a USB connection, ensure you install the appropriate USB driver for your system. For instance, you can use the Silicon Labs USB to UART driver and follow the installation wizard. This step is recommended during development.
Connecting to Hardware
To connect to a hardware device, initialize the connection with Magic.Device
and specify the port. In this example, the hardware is connected via the COM7
port:
Ensure you specify the correct port name for your system:
Windows USB:
COM#
(e.g.,COM7
)Mac USB:
cu.SLAB_USBtoUART
If using Bluetooth, pair the device via the operating system first. On connection, a directory named MAGIC
will be created in the same directory as your script, if it does not already exist.
Writing Data to CSV
The script automatically creates a timestamped .csv
file in the MAGIC
directory to store data from the device. The filename follows the format MAGIC/YYYYMMDDTHHMMSS.csv
. For example:
Data is continuously read from the device using the d.read()
method, which returns a list of ports. Each port's data is written as a row in the CSV file, with columns dynamically updated as new keys appear.
Data Access
To access and log device data, the script uses a dictionary structure:
Time Elapsed: The script tracks time in seconds (
time_secs
) usingd.time
.Port Data: Each port's data is processed and merged into the results.
The following loop continuously reads data and writes it to the CSV:
To stop data collection, use a Keyboard Interrupt (Ctrl + C
), which will gracefully exit the script.
Disconnecting from Hardware
The script automatically disconnects the device when the program ends or is interrupted. However, you can explicitly disconnect using:
Always disconnect before reloading or exiting to prevent errors.
Full Example
Last updated