Python 零散脚本

本文最后更新于 2026年4月3日 下午

test

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 生成正弦波 wav 250
import wave
import math
import array

# 音频参数
sample_rate = 44100 # 采样频率
duration = 60*10 # 音频时长(秒)

# 创建一个新的WAV文件
output_file = wave.open('output.wav', 'w')
output_file.setnchannels(1) # 单声道
output_file.setsampwidth(2) # 2字节,16位
output_file.setframerate(sample_rate)

# 生成250Hz的音频数据
samples = array.array('h', [0] * (sample_rate * duration))
amplitude = 2 ** 15 - 1 # 振幅

for i in range(len(samples)):
t = float(i) / sample_rate # 当前时间(秒)
samples[i] = int(amplitude * math.sin(2 * math.pi * 250 * t))

# 将音频数据写入WAV文件
output_file.writeframes(samples)

# 关闭WAV文件
output_file.close()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 生成指定的bin
import os
import binascii
import random

file_path = 'example.bin'
# file_size = 0x3150
file_size = 1024*8

with open(file_path, 'wb') as f:
remaining_bytes = file_size
buffer_size = 1024 # 每次写入的字节数
while remaining_bytes > 0:
buffer = bytearray(random.getrandbits(8) for _ in range(min(buffer_size, remaining_bytes)))
f.write(buffer)
remaining_bytes -= buffer_size
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#! /usr/bin/env python
#coding=utf-8
import os
import time
from ctypes import *
import numpy as np

class USBI2C():
ch341 = windll.LoadLibrary("C:/WCH.CN/CH341PAR/CH341DLLA64.dll")
def __init__(self, usb_dev = 0, i2c_dev = 0):
self.usb_id = usb_dev
self.dev_addr = i2c_dev
if USBI2C.ch341.CH341OpenDevice(self.usb_id) != -1:
USBI2C.ch341.CH341SetStream(self.usb_id, 0x80)
USBI2C.ch341.CH341CloseDevice(self.usb_id)
else:
print("USB CH341 Open Failed!")

def read(self, addr,len):
if USBI2C.ch341.CH341OpenDevice(self.usb_id) != -1:
obuf = (c_byte * 2)()
ibuf = (c_byte * len)()
obuf[0] = self.dev_addr
obuf[1] = addr
USBI2C.ch341.CH341StreamI2C(self.usb_id, 2, obuf, len, ibuf)
USBI2C.ch341.CH341CloseDevice(self.usb_id)
# return ibuf[0] & 0xff
return ibuf
else:
print("USB CH341 Open Failed!")
return 0

def write(self, addr, dat):
if USBI2C.ch341.CH341OpenDevice(self.usb_id) != -1:
obuf = (c_byte * 3)()
ibuf = (c_byte * 1)()
obuf[0] = self.dev_addr
obuf[1] = addr
obuf[2] = dat & 0xff
USBI2C.ch341.CH341StreamI2C(self.usb_id, 3, obuf, 0, ibuf)
USBI2C.ch341.CH341CloseDevice(self.usb_id)
else:
print("USB CH341 Open Failed!")

if __name__ == '__main__':

regs_add = [0x0b,0x0f,0x24,0x25,0x26,0x2e,0x2f]
for i in range(len(regs_add)):
i2cDevice = USBI2C(0,0x32)
buf = i2cDevice.read(regs_add[i],1)
print(hex(regs_add[i]),hex(c_uint8(buf[0]).value))
1

1

1

1

1

1

1

1

1

1

1

1

1

1

1


Python 零散脚本
https://blog.zimablue.fun/Language/Python_TinyTools/
作者
zimablue1996
发布于
2026年4月3日
许可协议