使用Python進(jìn)行SDS示波器屏幕圖像捕獲
、簡(jiǎn)介
下面是一個(gè)用Python 3.4編寫(xiě)的簡(jiǎn)短代碼示例,它借助套接字,通過(guò)LAN從SIGLENT SDS1000X-E中提取顯示圖像(屏幕截圖),并將其保存到控制計(jì)算機(jī)的本地驅(qū)動(dòng)器中。
注意:此程序?qū)D片/顯示圖像文件保存在同一目錄中的.py文件。它將覆蓋所有具有相同名稱(chēng)的現(xiàn)有文件。
下載Python 3.4,使用以太網(wǎng)電纜將示波器連接到LAN,獲取示波器IP地址,然后運(yùn)行附帶的.PY程序以保存示波器顯示的位圖(BMP)圖像。
您可以在此處下載.PY文件: Python_Socket_SDS_SCDP.zip
2、借助以下軟件和儀器進(jìn)行測(cè)試:
Python 3.4
SDS1202X-E
SDS1104/1204X-E
SDS2000X-E 型號(hào)
SDS5000X 型號(hào)
3、編程示例
1 | #!/usr/bin/env python 3.4.3 #-*- |
2 | coding:utf-8 –*- |
3 | #----------------------------------------------------------------------------- |
4 | #這段短腳本是一個(gè)打開(kāi)套接字的例子, 發(fā)送查詢(xún)來(lái)從作用域返回屏幕轉(zhuǎn)儲(chǔ),將屏幕轉(zhuǎn)儲(chǔ)保存 |
5 | #為python文件中的BMP文件,并關(guān)閉套接字 |
6 | # |
7 | #目前在SDS1000X-E,2000X-E和5000X型號(hào)上進(jìn)行了測(cè)試 |
8 | # |
9 | #不作任何明示或暗示的保證 |
10 | # |
11 | #SIGLENT/JAC 03.2019 |
12 | # |
13 | #----------------------------------------------------------------------------- |
14 | import socket #對(duì)于套接字 |
15 | import sys # 退出 |
16 | import time # 休眠 |
17 | #----------------------------------------------------------------------------- |
18 | |
19 | remote_ip = "192.168.55.100" # 應(yīng)與儀器的IP地址相配 |
20 | port = 5025 #儀器服務(wù)的端口號(hào) |
21 | |
22 | def SocketConnect(): |
23 | try: |
24 | #創(chuàng)建一個(gè)AF_INET, STREAM (TCP) 套接字 |
25 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
26 | except socket.error: |
27 | print ('Failed to create socket.') |
28 | sys.exit(); |
29 | try: |
30 | #連接到遠(yuǎn)程服務(wù)器 |
31 | s.connect((remote_ip , port)) |
32 | s.setblocking(0) # 非阻塞模式,當(dāng)接收器沒(méi)有檢測(cè)到數(shù)據(jù)時(shí)發(fā)生異常 |
33 | #s.settimeout(3) |
34 | except socket.error: |
35 | print ('failed to connect to ip ' + remote_ip) |
36 | return s |
37 | |
38 | def SocketQuery(Sock, cmd): |
39 | try : |
40 | #發(fā)送cmd字符串 |
41 | Sock.sendall(cmd) |
42 | Sock.sendall(b'n') #命令終止 |
43 | time.sleep(1) |
44 | except socket.error: |
45 | #發(fā)送失敗 |
46 | print ('Send failed') |
47 | sys.exit() |
48 | |
49 | data_body = bytes() |
50 | while True: |
51 | try: |
52 | time.sleep(0.01) |
53 | server_replay = Sock.recv(8000) |
54 | #print(len(server_replay)) |
55 | data_body += server_repla |
56 | except BlockingIOError: |
57 | print("data received complete..") |
58 | break |
59 | return data_body |
60 | ''' |
61 | PACK_LEN = 768067#您將收到的數(shù)據(jù)包長(zhǎng)度; |
62 | #SDS5000X是2457659; SDS1000X-E / 2000X-E是768067 |
63 | had_received = 0 |
64 | data_body = bytes() |
65 | while had_received < PACK_LEN: |
66 | part_body= Sock.recv(PACK_LEN - had_received) |
67 | data_body += part_body |
68 | part_body_length = len(part_body) |
69 | #print('part_body_length', part_body_length) |
70 | had_received += part_body_length |
71 | return data_body |
72 | ''' |
73 | |
74 | |
75 | def SocketClose(Sock): |
76 | #關(guān)閉套接口 |
77 | Sock.close() |
78 | time.sleep(5) |
79 | |
80 | def main(): |
81 | global remote_ip |
82 | global port |
83 | global count |
84 | |
85 | #打開(kāi)一個(gè)文件 |
86 | file_name = "SCDP.bmp" |
87 | |
88 | # 正文:打開(kāi)套接字,查詢(xún)屏幕轉(zhuǎn)儲(chǔ),保存并關(guān)閉 |
89 | s = SocketConnect() |
90 | qStr = SocketQuery(s, b'SCDP') #請(qǐng)求屏幕圖像 |
91 | print(len(qStr)) |
92 | f=open(file_name,'wb') |
93 | f.write(qStr) |
94 | f.flush() |
95 | f.close() |
96 | |
97 | SocketClose(s) |
98 | sys.exit |
99 | |
100 | if __name__ == '__main__'; |
101 | proc = main() |
評(píng)論