Last active
June 27, 2023 06:44
-
-
Save TheCrazyT/364ff5d6e893905af9d950f70daa2f29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Modified code by CrazyT | |
# | |
# For orginal view notes below: | |
# | |
# Proof-of-concept code for reading data from a Wifi microscope. | |
# See https://www.chzsoft.de/site/hardware/reverse-engineering-a-wifi-microscope/. | |
# Copyright (c) 2020, Christian Zietz <[email protected]> | |
# | |
# 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. | |
# | |
# 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 OWNER 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. | |
import time | |
import socket | |
import msvcrt | |
import cv2 | |
import numpy as np | |
from io import BytesIO | |
HOST = "192.168.29.1" # Microscope hard-wired IP address | |
SPORT = 20000 # Microscope command port | |
RPORT = 10900 # Receive port for JPEG frames | |
o = None | |
STORE_FRAMES = False | |
def receiveBytes(socket,bufsize): | |
data = b'' | |
while len(data) < bufsize: | |
try: | |
data += socket.recv(bufsize-len(data)) | |
except Exception as e: | |
break | |
if bufsize-len(data)<=0: | |
break | |
#print("[%d]" % len(data),end="", flush=True) | |
return data | |
# Open command socket for sending | |
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: | |
# s.sendto(b"JHCMD\xd0\x00", (HOST, SPORT)) | |
# Send commands like naInit_Re() would do | |
s.sendto(b"JHCMD\x10\x00", (HOST, SPORT)) | |
s.sendto(b"JHCMD\x20\x00", (HOST, SPORT)) | |
# Start data command? | |
s.sendto(b"JHCMD\xd0\x01", (HOST, SPORT)) | |
s.sendto(b"JHCMD\xd0\x01", (HOST, SPORT)) | |
nTime1_pre = time.time() * 1000 | |
lastFramecount = 0 | |
# Open receive socket and bind to receive port | |
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as r: | |
r.bind(("", RPORT)) | |
#r.setblocking(0) | |
r.settimeout(5.0) | |
print("Starting") | |
print("#",end="", flush=True) | |
# Note: Modify this line if not running under Windows | |
while not msvcrt.kbhit(): | |
try: | |
if(time.time() * 1000-nTime1_pre>1400*2): | |
print(",",end="", flush=True) | |
s.sendto(b"JHCMD\xd0\x01", (HOST, SPORT)) #F_SentRTPHeartBeep] | |
nTime1_pre = time.time()* 1000 | |
#data = receiveBytes(r,1450) | |
data = r.recv(1450) | |
print("#",end="", flush=True) | |
if len(data) > 8: | |
# Header | |
framecount = data[0] + data[1]*256 | |
if(framecount != lastFramecount): | |
print(".",end="", flush=True) | |
lastFramecount = framecount | |
packetcount = data[3] | |
#print("Frame %d, packet %d" % (framecount, packetcount)) | |
# Data | |
if packetcount==0: | |
#pass | |
# A new frame has started, open a new file | |
# Only save 100 frames to avoid filling up the disk | |
if STORE_FRAMES: | |
if o is not None: | |
o.close() | |
o = open("frame_%d.jpg" % (framecount%100), "wb") | |
else: | |
if o is not None: | |
#print("upd img") | |
o.seek(0) | |
buf = o.read() | |
if (buf is not None) and (len(buf)>0): | |
print("*",end="", flush=True) | |
#print(buf) | |
nparr = np.frombuffer(buf, dtype="uint8") | |
#print(nparr.shape) | |
if nparr.shape[0] != 0 : | |
img = cv2.imdecode(nparr,1) | |
o.close() | |
#print(img) | |
if img is not None: | |
cv2.imshow('frame',img) | |
cv2.waitKey(25) | |
o = BytesIO() | |
o.write(data[8:]) | |
except Exception as e: | |
#raise e | |
#print(e) | |
time.sleep(0.01) | |
# Stop data command, like in naStop() | |
s.sendto(b"JHCMD\xd0\x02", (HOST, SPORT)) | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment