python操作雷赛IO卡(IOC0640)

1.先安装板卡和对应驱动.(测试程序运行起来,没有问题即可.)

(顺便提一句:如果官方 测试程序 | 示例,运行起来找不到IO卡说明DLL版本太新,换旧版DLL即可.)

2.官方例程给的文件打包成(C#的DLL,32 | 64和Windows 相对应;

(厂商给的DLL也是32 | 64 和Windows相对应)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace CSIOC0640
{
    public class Class1
    {
        [DllImport("IOC0640.dll", EntryPoint = "ioc_board_init", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int ioc_board_init();
        [DllImport("IOC0640.dll", EntryPoint = "ioc_board_close", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern void ioc_board_close();

        [DllImport("IOC0640.dll", EntryPoint = "ioc_read_inbit", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int ioc_read_inbit(ushort cardno, ushort bitno);
        [DllImport("IOC0640.dll", EntryPoint = "ioc_read_outbit", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int ioc_read_outbit(ushort cardno, ushort bitno);
        [DllImport("IOC0640.dll", EntryPoint = "ioc_write_outbit", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern uint ioc_write_outbit(ushort cardno, ushort bitno, int on_off);

        [DllImport("IOC0640.dll", EntryPoint = "ioc_read_inport", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int ioc_read_inport(ushort cardno, ushort m_PortNo);
        [DllImport("IOC0640.dll", EntryPoint = "ioc_read_outport", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int ioc_read_outport(ushort cardno, ushort m_PortNo);
        [DllImport("IOC0640.dll", EntryPoint = "ioc_write_outport", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern uint ioc_write_outport(ushort cardno, ushort m_PortNo, uint port_value);

        public delegate uint IOC0640_OPERATE(IntPtr operate_data);
        [DllImport("IOC0640.dll", EntryPoint = "ioc_int_enable", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern uint ioc_int_enable(ushort cardno, IOC0640_OPERATE funcIntHandler, IntPtr operate_data);
        [DllImport("IOC0640.dll", EntryPoint = "ioc_int_disable", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern uint ioc_int_disable(ushort cardno);

        [DllImport("IOC0640.dll", EntryPoint = "ioc_config_intbitmode", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern uint ioc_config_intbitmode(ushort cardno, ushort bitno, ushort enable, ushort logic);
        [DllImport("IOC0640.dll", EntryPoint = "ioc_config_intbitmode", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern uint ioc_config_intbitmode(ushort cardno, ushort bitno, ushort[] enable, ushort[] logic);
        [DllImport("IOC0640.dll", EntryPoint = "ioc_read_intbitstatus", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int ioc_read_intbitstatus(ushort cardno, ushort bitno);

        [DllImport("IOC0640.dll", EntryPoint = "ioc_config_intporten", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern uint ioc_config_intporten(ushort cardno, ushort m_PortNo, uint port_en);
        [DllImport("IOC0640.dll", EntryPoint = "ioc_config_intportlogic", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern uint ioc_config_intportlogic(ushort cardno, ushort m_PortNo, uint port_logic);

        [DllImport("IOC0640.dll", EntryPoint = "ioc_read_intportmode", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern uint ioc_read_intportmode(ushort cardno, ushort m_PortNo, uint[] enable, uint[] logic);
        [DllImport("IOC0640.dll", EntryPoint = "ioc_read_intportstatus", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern int ioc_read_intportstatus(ushort cardno, ushort m_PortNo);

        [DllImport("IOC0640.dll", EntryPoint = "ioc_set_filter", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
        public static extern uint ioc_set_filter(ushort cardno, double filter);

    }
}

3.官方给的DLL还有上面生成的DLL复制到py程序运行目录.

4.不多说python贴上来 .

(import clr 之前 需要install pythonnet ;下来C# | .NET那一套就能直接用了.)

{用winform的定时器读取输入口}

from tkinter import ttk
from tkinter import messagebox
from tkinter import *
import tkinter as tk
import tkinter.messagebox
import os,sys,time
import tkinter.filedialog
def cmd(s="pause"):
    os.system(s)
from ctypes import *
import sys,time
from tkinter import Tk,Frame
import clr
import System
from System import String, Char, Int32,UInt16, Int64, Environment, IntPtr
print(f"{clr.AddReference('System')}")
print(f"{clr.AddReference('CSIOC0640')}")
import CSIOC0640
print("Loading complete");
if __name__=='__main__': 
    i=0
    nCard = CSIOC0640.Class1.ioc_board_init(); #CSIOC0640.Class1.ioc_board_close();    
    if (nCard <= 0):
        System.Windows.Forms.MessageBox.Show(
        "未找到IOC0640控制卡!", "警告!", 
        System.Windows.Forms.MessageBoxButtons.OK, 
        System.Windows.Forms.MessageBoxIcon.Information);
    def IoCardSetOn(x1=1,x2=0):#
        CSIOC0640.Class1.ioc_write_outbit(0,x1,x2); 
    def IoCardSetOff(x1=1,x2=1):
        CSIOC0640.Class1.ioc_write_outbit(0,x1,x2);
    def IoCardGet(x1=1):
        #print(f"type:= {type(CSIOC0640.Class1.ioc_read_inbit(0,x1))}")
        #返回 int类型 0 1;
        return CSIOC0640.Class1.ioc_read_inbit(0,x1)   
    Timer_Flag=False
    def IoCardtim(x1=1,x2=1):
        #无论如何这个函数一定要有两个参数;否则不能正常执行;
        global i;
        global Timer_Flag;
        i=i+1;
        if Timer_Flag==False:
            Timer_Flag=True
            IoCardSetOn()
        else:
            Timer_Flag=False
            IoCardSetOff()
        print(f"定时器事件计次{i}")
        for I,n in enumerate(range(1,32+1,1)):
            print(f"In{I+1}:= {IoCardGet(n)}")
        #遍历输入状态映射到列表 | 算了 先输出看看
        pass
    t = System.Timers.Timer(500);
    t.Elapsed += System.Timers.ElapsedEventHandler(IoCardtim);#到达时间的时候执行事件;
    t.AutoReset = True;#设置是执行一次(false)还是一直执行(true);
    t.Enabled = True;#是否执行System.Timers.Timer.Elapsed事件;
    #*******************************************
    a=Tk()
    a.title('IOC0640')
    screenwidth = a.winfo_screenwidth()  # 屏幕宽度
    screenheight = a.winfo_screenheight()  # 屏幕高度
    width = 1000
    height = 500
    x = int((screenwidth - width) / 2)
    y = int((screenheight - height) / 2)
    a.geometry('{}x{}+{}+{}'.format(width, height, x, y))  # 大小以及位置a.geometry('400x400+100+100')
    ICO="3D.ico"#a.iconbitmap(f'{ICO}')
    cbox001 = ttk.Combobox(a)# 创建
    cbox001.pack()#.place(x=0, y=80)    #绘制
    cbox001['value'] = (
    '1',
    '2',
    '3'
    )#列表 #{str(cbox001.get())} {str(cbox001.current()+1)}
    cbox001.current(0)#设置默认
    def func(event):# 
        result=tkinter.messagebox.askokcancel ("......(确定/取消).",f"名称: !") #下拉菜单索引
        if result==True:
            print(f"result={result};")
        else:
            print(f"result={result};")
        pass
    cbox001.bind("<<ComboboxSelected>>",func)# 
    lb1 = Label(a,text='111', bg='purple', );lb1.place (x=20,y=80)
    lb2 = Label(a,text='222');lb2.place (x=16,y=120);
    def click_button(): 
        print("开");IoCardSetOn(1)        
    button = tk.Button(a,text='开',bg='#8cb5b3',width=10, height=2,command=click_button)
    button.place(x=20, y=20) 
    def click_button_(): 
        print("关");IoCardSetOff(1)
    button_ = tk.Button(a,text='关',bg='#8cb5b3',width=10, height=2,command=click_button_)
    button_.place(x=20, y=80) 
    def click_button___():
        print("全开");
        for n in range(1,32+1,1):  
            IoCardSetOn(n)
    button___ = tk.Button(a,text='全开',bg='#8cb5b3',width=10, height=2,command=click_button___)
    button___.place(x=20, y=140) 
    def click_button____(): 
        print("全关");
        for n in range(1,32+1,1):  
            IoCardSetOff(n)    
    button____ = tk.Button(a,text='全关',bg='#8cb5b3',width=10, height=2,command=click_button____)
    button____.place(x=120, y=140) 
    def QueryWindow():
        result=tkinter.messagebox.askokcancel ("(确定/取消)",f"确定关闭??") #
        if result==True:
            print(f"result={result};")
            try:
                a.destroy()#关闭窗口
                pass 
            except:
                pass
        else:
            print(f"result={result};")
    a.protocol('WM_DELETE_WINDOW', QueryWindow)#注册
    a.mainloop()

5.运行起来.

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: C是点灯开关的意思。点灯开关是一种用来控制灯泡通电或断电的装置。通过按下或拨动开关,可以打开或关闭灯泡。C也可以代表“电流”的缩写。电流是电荷在电路中流动的现象,是导体中自由电子的移动所产生的。电流的大小可以通过测量单位时间内流过的电荷量来确定,通常用安培(A)来表示。电流在电路中起到了传递能量和信号的作用,是电路运行的基础。此外,C还可以代表克氏温度(Celsius),是摄氏温标的单位,常用于衡量温度。以摄氏温度为基准,水的冰点为0℃,沸点为100℃。C还可以是“计算机”的缩写,是一种用于处理和存储信息的机器。计算机能够执行各种任务,包括计算、通信、娱乐等。现代社会离不开计算机的运行和应用,它已经成为我们生活和工作中不可或缺的一部分。 ### 回答2: c是英语字母表中的第三个字母。c的发音是/k/,在大部分单词中表示/k/这个音素。 在英语单词中,c通常与a、o、u这些元音字母连用,表示不同的发音。例如,cat(猫),cot(软垫),cut(切割)。此外,也可以与其他辅音字母连用,例如crab(螃蟹),clam(蛤蜊),clip(夹子)。 c还可以与k或者s字母连用,形成特定的发音。当c和k连用时,通常表示/k/的发音。例如,clock(时钟),back(背部),stick(棍子)。当c和s连用时,通常表示/s/的发音。例如,ceiling(天花板),city(城市),century(世纪)。 此外,c还有一些其他的用法。例如,在罗马数字中,c表示100。在化学元素周期表中,c代表碳(Carbon)。 总之,c是一种字母,它在英语中有着不同的发音和用法。我们可以通过学习和掌握这些规则来正确地读写和使用c。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值