編程示例使用Kotlin從XE系列示波器中檢索數據
SDS系列示波器均具有遠程編程和數據采集功能。 它們可以輕松集成到許多自動測試環(huán)境中,以簡化測試期間的設置和數據采集。
我們的一位有用的客戶開發(fā)了一個很好的編程示例,旨在使用Kotlin設置和檢索來自SIGLENT SDS1202X-E示波器的數據,Kotlin是一個免費的開源編碼環(huán)境(此處更多關于Kotlin)。
該代碼使用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))
//由于套接字可以返回二進制數據,我們不能使用InputStreamReader
//將字節(jié)翻譯成字符。SCPI通常使用US ASCII。
val input = socket.getInputStream()
/**
*開始讀取,直到遇到 n。
*字節(jié)數字轉換為數字(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é)不會轉換為字符
*/
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()
}
/**
* 讀取數據響應
* 比如 “C1:VDIV 1.00E+00V”.
* 此函數提取“1.00E + 00”,將其轉換為double類型,然后返回。
*/
fun readNumber() = readLine().split(” “)[1].dropLast(1).toDouble()
writeLine(“*IDN?”)
println(readLine())
//將響應格式重置為默認值,以便readNumber()運行
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”)
// 解析波形響應
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)
}
}
}
}
評論