440Hz を 0.5秒 (500mSec)
デモなので 0.5 秒と短い時間ですが、楽器のチューニングするときには、もっと時間を長めに。winsound のマニュアルには、もっといろいろなことが書いてあります。
# Win32(XP)
# Python 2.5
import winsound
winsound.Beep(440,500)
Electronic hobby and a lot of rude programs! とはいえ。。。もぉちょっと なんとかならんか?
# $Id$
# Win32
# Python 2.5 (ctypes)
# MBT.dll by Wago (Modbus over TCP)
# copy MBT.dll to c:\windws\system32
# SYSTEM 750-841 Wago PLC
#
# data = int (16 bit) only
#
from ctypes import *
PLCAddress = '192.168.1.2' # PLC adress or name
Port = 502 # Modbus TCP port, need to be enabled
BaseAddress = 0x3000 # PLC, IEC 61131-3 Addresses %MW0..
length = 1 # data length
socket = c_long() # handler
ReadData = c_long() # pointer
WriteData = c_long() # pointer
def connect():
state_a = windll.MBT.MBTInit()
state_b = windll.MBT.MBTConnect(PLCAddress, Port, 1, 3000, byref(socket))
print ('PLC address = ',PLCAddress)
print ('Socket = ',socket.value)
print ('MBT Init state = ',state_a)
print ('MBT Connect state = ',state_b)
def disconnect():
state_a = windll.MBT.MBTDisconnect(socket)
state_b = windll.MBT.MBTExit()
print ('MBT Disconnect state = ',state_a)
print ('MBT Exit state = ',state_b)
def read(OffsetAddress, c=None):
ReadAddress = BaseAddress + OffsetAddress
state_a = windll.MBT.MBTReadRegisters(socket,4,ReadAddress,length,byref(ReadData),0,0)
if state_a != 0 :
print (' **** PLC read error')
c = windll.MBT.MBTSwapDWord(ReadData.value) >> 16
print ('MBT Read resisters state = ', state_a )
print ('Read address = ', ReadAddress )
print ('Read data = ',c )
return c
def write(OffsetAddress, data, c=None):
WriteAddress = BaseAddress + OffsetAddress
WriteData.value = windll.MBT.MBTSwapDWord(data << 16)
state_a = windll.MBT.MBTWriteRegisters(socket,WriteAddress,length,byref(WriteData),0,0)
if state_a != 0 :
print (' **** PLC write error')
print ('MBT Write resisters state = ', state_a )
print ('Write add = ', WriteAddress )
print ('Write data = ', WriteData.value )
## read back
c = read(OffsetAddress)
return c
connect()
write(0,1)
data = read(0)
print(data)
disconnect()
## -----------------------------------------------
## 'VB source
##Public Const MODBUSTCP_TABLE_OUTPUT_REGISTER = 4
##Public Const MODBUSTCP_TABLE_INPUT_REGISTER = 3
##Public Const MODBUSTCP_TABLE_OUTPUT_COIL = 0
##Public Const MODBUSTCP_TABLE_INPUT_COIL = 1
##Public Const MODBUSTCP_TABLE_EXCEPTION_STATUS = 7
##
##Public Declare Function MBTInit Lib "MBT" () As Long
##Public Declare Function MBTExit Lib "MBT" () As Long
##Public Declare Function MBTConnect Lib "MBT" (ByVal szHostAddress As String, ByVal port As Integer, ByVal useTCPorUDP As Long, ByVal requestTimeout As Long, hSocket As Long) As Long
##Public Declare Function MBTDisconnect Lib "MBT" (ByVal hSocket As Long) As Long
##Public Declare Function MBTReadRegisters Lib "MBT" (ByVal hSocket As Long, ByVal tableType As Byte, ByVal dataStartAddress As Integer, ByVal numWords As Integer, pReadBuffer As Any, ByVal fpReadCompletedCallback As Long, ByVal callbackContext As Long) As Long
##Public Declare Function MBTReadCoils Lib "MBT" (ByVal hSocket As Long, ByVal tableType As Byte, ByVal dataStartAddress As Integer, ByVal numBits As Integer, pReadBuffer As Any, ByVal fpReadCompletedCallback As Long, ByVal callbackContext As Long) As Long
##Public Declare Function MBTReadExceptionStatus Lib "MBT" (ByVal hSocket As Long, pExceptionStatus As Byte, ByVal fpReadCompletedCallback As Long, ByVal callbackContext As Long) As Long
##Public Declare Function MBTWriteRegisters Lib "MBT" (ByVal hSocket As Long, ByVal dataStartAddress As Integer, ByVal numWords As Integer, pWriteBuffer As Any, ByVal fpWriteCompletedCallback As Long, ByVal callbackContext As Long) As Long
##Public Declare Function MBTWriteCoils Lib "MBT" (ByVal hSocket As Long, ByVal dataStartAddress As Integer, ByVal numBits As Integer, pWriteBuffer As Any, ByVal fpWriteCompletedCallback As Long, ByVal callbackContext As Long) As Long
##Public Declare Function MBTSwapWord Lib "MBT" (ByVal wData As Integer) As Integer
##Public Declare Function MBTSwapDWord Lib "MBT" (ByVal dwData As Long) As Long
##
##Private Socket As Long
## $Id$
## win32
## Python2.5
## csv file io (for numeric data)
import csv
def s2f(x): return float(x)
datastr = []
datanum = []
datasample = [[1.0,2.0],[3.0,4.0]]
fw0 = open('sample.csv','wb')
writer0 = csv.writer(fw0)
writer0.writerows(datasample)
fw0.close()
fr0 = open('sample.csv', 'rb')
reader0 = csv.reader(fr0)
for row in reader0:
datastr.append(row)
datanum.append(map(s2f,row))
fr0.close()
print (reader0.line_num)
r = 1
c = 1
print ('string data [',r,'][',c,'] = ',datastr[r][c])
print ('numeric data [',r,'][',c,'] = ',datanum[r][c])
print(row) # data for row write test, form last row
fw1 = open('csvout1.csv','wb')
writer1 = csv.writer(fw1)
writer1.writerow(row)
writer1.writerow(row)
writer1.writerow(row)
writer1.writerow(row)
fw1.close()
fw2 = open('csvout2.csv','wb')
writer2 = csv.writer(fw2)
writer2.writerows(datanum)
fw2.close()
fw3 = open('csvout3.csv','wb')
writer3 = csv.writer(fw3)
writer3.writerows(datastr)
fw3.close()