选择你想访问的SIGLENT网站
Choose your SIGLENT site
SIGLENTのウェブサイトを選択してください
当前位置: 首页 / 服务与支持 / 应用文档
编程示例使用Kotlin从XE系列示波器中检索数据
SDS系列示波器均具有远程编程和数据采集功能。 它们可以轻松集成到许多自动测试环境中,以简化测试期间的设置和数据采集。
我们的一位有用的客户开发了一个很好的编程示例,旨在使用Kotlin设置和检索来自SIGLENT SDS1202X-E示波器的数据,Kotlin是一个免费的开源编码环境(此处更多关于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
/**
* 创建一个从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
//将字节翻译成字符。SCPI通常使用US ASCII。
val input = socket.getInputStream()
/**
*开始读取,直到遇到\ n。
*字节数字转换为数字(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())
}
}
}
/**
* 读取字节
* 字节不会转换为字符
*/
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(“length is $length”)
val data = readBytes(length)
readBytes(2)
//最后两个无效字节
println(“V/div = $vDiv; offset = $vOffset; t/div = $tDiv; tOffset = $tOffset”)
return Waveform(vDiv, vOffset, tDiv, tOffset, data)
}
}
}
}
鼎阳科技客服电话
客服电话
400-878-0807
0755-36887876
鼎阳科技电子邮件
电子邮件
market@siglent.com
鼎阳科技公司地址
公司地址
深圳市宝安68区留仙三路
安通达工业园四栋