Skip to content

Instantly share code, notes, and snippets.

@sspathak
Created January 21, 2025 20:03
Show Gist options
  • Save sspathak/f4ceccd63eba9c9b222b20edcc31d344 to your computer and use it in GitHub Desktop.
Save sspathak/f4ceccd63eba9c9b222b20edcc31d344 to your computer and use it in GitHub Desktop.
# 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 numpy as np
# import msvcrt
import os
from io import BytesIO
import cv2
# Windows
if os.name == 'nt':
import msvcrt
# Posix (Linux, OS X)
else:
import sys
import atexit
from select import select
HOST = "192.168.29.1" # Microscope hard-wired IP address
SPORT = 20000 # Microscope command port
# SPORT = 10850 # Microscope command port
RPORT = 10900 # Receive port for JPEG frames
o = None
# 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))
# Heartbeat command, starts the transmission of data from the scope
s.sendto(b"JHCMD\xd0\x01", (HOST, SPORT))
s.sendto(b"JHCMD\xd0\x01", (HOST, SPORT))
s.sendto(b"JHCMD\xd0\x01", (HOST, SPORT))
s.sendto(b"JHCMD\xd0\x01", (HOST, SPORT))
# 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)
# Note: Modify this line if not running under Windows
framecounter = {}
counter = 0
while True:
try:
data = r.recv(1450)
if len(data) > 8:
# Header
framecount = data[0] + data[1]*256
packetcount = data[3]
if framecount not in framecounter.keys():
framecounter[framecount] = []
# print("Frame %d, packet %d" % (framecount, packetcount))
framecounter[framecount].append(packetcount)
# Data
if packetcount == 0:
print(f"Frame {framecount}")
# A new frame has started, open a new file
if o is not None:
o.seek(0)
image_bytes = np.asarray(bytearray(o.read()), dtype=np.uint8)
image = cv2.imdecode(image_bytes, cv2.IMREAD_COLOR)
o.close()
cv2.imshow("image", image)
cv2.waitKey(1)
# Only save 100 frames to avoid filling up the disk
o = BytesIO()
# ignore first 16 bytes (required for compatibility with iowbac microscope)
data = data[16:]
# Send a heartbeat every 50 frames (arbitrary number) to keep data flowing
if framecount%50 == 0:
s.sendto(b"JHCMD\xd0\x01", (HOST, SPORT))
o.write(data[8:])
except KeyboardInterrupt:
break
except:
time.sleep(0.01)
# for each frame print the frame number and the packets received
for k, v in framecounter.items():
print(f"Frame {k}\t: {v}")
# Stop data command, like in naStop()
s.sendto(b"JHCMD\xd0\x02", (HOST, SPORT))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment