新聞中心

        EEPW首頁 > 測試測量 > 設(shè)計應(yīng)用 > 編程示例使用Kotlin從XE系列示波器中檢索數(shù)據(jù)

        編程示例使用Kotlin從XE系列示波器中檢索數(shù)據(jù)

        作者: 時間:2024-06-18 來源:鼎陽硬件設(shè)計與測試智庫 收藏

        SDS系列均具有遠(yuǎn)程編程和數(shù)據(jù)采集功能。 它們可以輕松集成到許多自動測試環(huán)境中,以簡化測試期間的設(shè)置和數(shù)據(jù)采集。
        我們的一位有用的客戶開發(fā)了一個很好的,旨在使用設(shè)置和檢索來自SIGLENT SDS1202X-E的數(shù)據(jù),是一個免費的開源編碼環(huán)境(此處更多關(guān)于)。
        該代碼使用LAN連接和打開的套接字。
        感謝Chris Welty的代碼!

        代碼如下:
        **
        * License: 3-Clause BSD
        *
        * Copyright 2018 Chris Welty
        *
        * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
        * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
        * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
        * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
        *
        * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
        * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
        * EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
        * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
        * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
        * POSSIBILITY OF SUCH DAMAGE.
        */
        package scope
        import java.io.BufferedWriter
        import java.io.OutputStreamWriter
        import java.io.Serializable
        import java.net.Socket
        /**
        * 創(chuàng)建一個從Siglent 1202X-E下載的波形
        */
        class Waveform(val vDiv: Double, val vOffset: Double, val tDiv: Double, val tOffset: Double, val data: ByteArray) : Serializable {
        val xs: DoubleArray
        get() = DoubleArray(data.size, { i -> i * tDiv * 14 / data.size + tOffset – tDiv * 7 })
        val ys: DoubleArray
        get() = DoubleArray(data.size, { i -> data[i] * vDiv / 25 – vOffset })
        companion object {
        /**
        * 下載在屏幕上顯示的波形
        */
        fun download(): Waveform {
        Socket(“192.168.1.222”, 5025).use { socket ->
        println(“connected to ” + socket.inetAddress)
        val output = BufferedWriter(OutputStreamWriter(socket.getOutputStream(), Charsets.US_ASCII))
        //由于套接字可以返回二進(jìn)制數(shù)據(jù),我們不能使用InputStreamReader
        //將字節(jié)翻譯成字符。SCPI通常使用US ASCII。
        val input = socket.getInputStream()
        /**
        *開始讀取,直到遇到 n。
        *字節(jié)數(shù)字轉(zhuǎn)換為數(shù)字(ASCII)。
        */
        fun readLine(): String {
        val sb = StringBuilder()
        while (true) {
        val c = input.read()
        when (c) {
        -1, ‘n’.toInt() -> return sb.toString()
        else -> sb.append(c.toChar())
        }
        }
        }
        /**
        * 讀取字節(jié)
        * 字節(jié)不會轉(zhuǎn)換為字符
        */
        fun readBytes(n: Int): ByteArray {
        val result = ByteArray(n)
        var i = 0
        while (i < n) {
        i += input.read(result, i, n – i)
        }
        return result
        }
        fun writeLine(string: String) {
        output.write(string)
        output.write(“n”)
        output.flush()
        }
        /**
        * 讀取數(shù)據(jù)響應(yīng)
        * 比如 “C1:VDIV 1.00E+00V”.
        * 此函數(shù)提取“1.00E + 00”,將其轉(zhuǎn)換為double類型,然后返回。
        */
        fun readNumber() = readLine().split(” “)[1].dropLast(1).toDouble()
        writeLine(“*IDN?”)
        println(readLine())
        //將響應(yīng)格式重置為默認(rèn)值,以便readNumber()運(yùn)行
        writeLine(“CHDR SHORT”)
        writeLine(“C1:VDIV?”)
        val vDiv = readNumber()
        writeLine(“C1:OFST?”)
        val vOffset = readNumber()
        writeLine(“TDIV?”)
        val tDiv = readNumber()
        writeLine(“TRDL?”)
        val tOffset = readNumber()
        // 請求所有的波形點
        writeLine(“WFSU SP,0,NP,0,F,0”)
        writeLine(“C1:WF? DAT2”)
        // 解析波形響應(yīng)
        val header = String(readBytes(21))
        println(“header is $header”)
        val length = header.substring(13, 21).toInt()
        println(“l(fā)ength is $length”)
        val data = readBytes(length)
        readBytes(2)
        //最后兩個無效字節(jié)
        println(“V/div = $vDiv; offset = $vOffset; t/div = $tDiv; tOffset = $tOffset”)
        return Waveform(vDiv, vOffset, tDiv, tOffset, data)
        }
        }
        }
        }

        本文引用地址:http://www.104case.com/article/202406/460004.htm


        關(guān)鍵詞: 編程示例 Kotlin XE系列 示波器

        評論


        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉
        主站蜘蛛池模板: 靖宇县| 凤翔县| 璧山县| 姜堰市| 天台县| 扶风县| 青神县| 西青区| 新干县| 宁强县| 通辽市| 富锦市| 溧阳市| 商河县| 梁山县| 内乡县| 涡阳县| 灵丘县| 彰化市| 墨竹工卡县| 阿鲁科尔沁旗| 祁东县| 蒙阴县| 河曲县| 易门县| 永城市| 清徐县| 保康县| 巴马| 监利县| 香港| 清涧县| 龙南县| 藁城市| 嵊州市| 安徽省| 浠水县| 东乡族自治县| 安图县| 商城县| 大邑县|