import serial
import shutil
import sys

# the pcap init sequence for little-endian system such as the telosb
pcap_init_seq = bytearray.fromhex(u'D4 C3 B2 A1 02 00 04 00 00 00 00 00 00 00 00 00 FF FF 00 00 C3 00 00 00 00 00 00 00 00 00 00 00 21 00 00 00 21 00 00 00 41 88 01 34 12 00 00 78 00 77 69 72 65 73 68 61 72 6B 20 66 6F 6E 63 74 69 6F 6E 6E 65 20 21 AB 00')

# write the init sequence in a file
pcap_file = open('tmp_telosb-pcap-initseq.pcap', 'wb')
pcap_file.write(pcap_init_seq)
pcap_file.close()

# read from file and copy to stdout
with open("tmp_telosb-pcap-initseq.pcap", "r") as f:
	shutil.copyfileobj(f, sys.stdout)

ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=0)

# flush everything that may have been received on the port to make sure that we start with a clean serial input
ser.flushInput()

while True:
    out = ''
    sys.stdout.write(ser.read(1024))
    sys.stdout.flush()
