Skip to content
bitbegin edited this page Apr 7, 2019 · 11 revisions

预计支持2类usb port!, usb://hid://

usb port!

厂商自定义usb设备类

  • url: usb://
  • 设备描述:class为0xFF

hid设备

  • url: hid://
  • 设备描述:usb hid类型

query

获取usb设备

Notes: 假设pc上接有usb://vid-pid1/serialnum1usb://vid-pid1/serialnum2usb://vid-pid2/serialnum3

获取所有usb设备:

devices: query usb://
= [
    make object! [
        port: usb://vid-pid1/serialnum1
        discriptions: ...
    ]
    make object! [
        port: usb://vid-pid1/serialnum2
        discriptions: ...
    ]
    make object! [
        port: usb://vid-pid2/serialnum3
        discriptions: ...
    ]
    ; other devices ...
]

获取vid-pid1的所有usb设备:

devices: query usb://vid-pid1
= [
    make object! [
        port: usb://vid-pid1/serialnum1
        discriptions: ...
    ]
    make object! [
        port: usb://vid-pid1/serialnum2
        discriptions: ...
    ]
]

获取vid-pid1/serialnum1的所有usb设备:

devices: query usb://vid-pid1/serialnum1
= [
    make object! [
        port: usb://vid-pid1/serialnum1
        discriptions: ...
    ]
]

获取hid设备

Notes: 假设pc上接有hid://vid-pid1/serialnum1hid://vid-pid1/serialnum2hid://vid-pid2/serialnum3

获取所有hid设备:

devices: query hid://
= [
    make object! [
        port: hid://vid-pid1/serialnum1
        discriptions: ... ;(include report description)
    ]
    make object! [
        port: hid://vid-pid2/serialnum3
        discriptions: ...  ;(include report description)
    ]
    ; other hid devices ...
]

获取vid-pid1的所有hid设备:

devices: query hid://vid-pid1
= [
    make object! [
        port: hid://vid-pid1/serialnum1
        discriptions: ... ;(include report description)
    ]
]

获取vid-pid1/serialnum1的所有hid设备:

devices: query hid://vid-pid1/serialnum1
= [
    make object! [
        port: hid://vid-pid1/serialnum1
        discriptions: ... ;(include report description)
    ]
]

open

打开usb://vid-pid1/serialnum1

Notes: 设备端口0控制传输,如果serialnum1不存在,使用找到的第一个设备

等价为: usb://vid-pid1/serialnum1/transfer=Control

port: open usb://vid-pid1/serialnum1
= make port! [
    spec: make object! [
        scheme: 'usb
        user-info: none
        host: "vid-pid1"
        port: none
        path: %/
        target: %serialnum1
        query: none
        fragment: none
        ref: usb://vid-pid1/serialnum1
    ]
    select-pipo: func [port [port!]] [
        port/state/rpipo: 0
        port/state/wpipo: 0
        port/state/trasfer: 'Control
    ]
    scheme: make object! [
        open: func [port [port!] /local spec][
            probe "open port"
            port/state/info: get-usb-info vid-pid1 none
            select-pipo port
            port/state/closed?: false
            usb-open port
        ]
        insert: func [
            port [port!]
            data [binary! string!]
        ][
            unless port/state/closed? [
                usb-write port data
            ]
        ]
        copy: func [port [port!]][
            unless port/state/closed? [
                usb-read port
            ]
        ]
        close: func [port [port!]][
            unless port/state/closed? [
                usb-close port
                port/state/closed?: yes
                port/state/info: none
                port/state/sub: none
            ]
        ]
    ]
    actor: none
    awake: none
    state: make object! [
        state: 'init
        sub: none
        error: none
        closed?: false
        info: none
        trasfer: none
        rpipo: none
        wpipo: none
    ]
    data: none
]

打开usb://vid-pid1/serialnum1/port=0102 (01或02为16进制端口编号)

Notes: 端口1为read端口,端口2为write端口,默认中断传输

等价为: usb://vid-pid1/serialnum1/in=01&out=02&transfer=Interrupt

port: open usb://vid-pid1/serialnum1/port=0102
= make port! [
    spec: make object! [
        scheme: 'usb
        user-info: none
        host: "vid-pid1"
        port: none
        path: %/serialnum1/
        target: %port=0102
        query: none
        fragment: none
        ref: usb://vid-pid1/serialnum1/port=0102
    ]
    select-pipo: func [port [port!]] [
        port/state/rpipo: 1
        port/state/wpipo: 2
        port/state/trasfer: 'Interrupt
    ]
    scheme: make object! [
        open: func [port [port!] /local spec][
            probe "open port"
            port/state/info: get-usb-info vid-pid1 none
            select-pipo port
            port/state/closed?: false
            usb-open port
        ]
        insert: func [
            port [port!]
            data [binary! string!]
        ][
            unless port/state/closed? [
                usb-write port data
            ]
        ]
        copy: func [port [port!]][
            unless port/state/closed? [
                usb-read port
            ]
        ]
        close: func [port [port!]][
            unless port/state/closed? [
                usb-close port
                port/state/closed?: yes
                port/state/info: none
                port/state/sub: none
            ]
        ]
    ]
    actor: none
    awake: none
    state: make object! [
        state: 'init
        sub: none
        error: none
        closed?: false
        info: none
        trasfer: none
        rpipo: none
        wpipo: none
    ]
    data: none
]

自定义usb的pipo操作方式

port: open usb://vid-pid1/serialnum1
port/select-pipo: func [port [port!]][
    port/state/rpipo: 2
    port/state/wpipo: 3
    port/state/trasfer: 'Interrupt
]

打开hid://vid-pid1/serialnum1/port=0102 (只支持中断传输)

Notes: 端口1为read端口,端口2为write端口,中断传输 等价为: hid://vid-pid1/serialnum1/in=01&out=02 port!信息和上面的类似

自定义hid的pipo操作方式

port: open hid://vid-pid1/serialnum1
port/select-pipo: func [port [port!]][
    //parse report descriptors (port/state/info)
    port/state/rpipo: rpipo
    port/state/wpipo: wpipo
    port/state/trasfer: 'Interrupt
]