0

Controller Documentation page 124

Serial Sniffing screenshot

The hardware setup should be OK, everything works as it should when I use their sensorTOOL 1.8., also I made it working with similar code for an optoNCDT 1420 laser sensor (which sends the data in strings with fixed lengths). This one is a confocalDT controller with IFS2406-10 sensor. These sensors are used to measure distance.

I know this script is not the right way to do it.

import serial
import sys
import time
import os


try:
    ser = serial.Serial('COM4', 115200)
    print("connected")
except:
    print("no comm")
    

while True:
    try:
            #read 9 Bytes
            byte1 = ser.read()
            byte2 = ser.read()
            byte3 = ser.read()
            byte4 = ser.read()
            byte5 = ser.read()
            byte6 = ser.read()
            byte7 = ser.read()
            byte8 = ser.read()
            byte9 = ser.read()

            #convert bytes to int
            byte1bits = int.from_bytes(byte1, byteorder=sys.byteorder)
            byte2bits = int.from_bytes(byte2, byteorder=sys.byteorder)
            byte3bits = int.from_bytes(byte3, byteorder=sys.byteorder)
            byte4bits = int.from_bytes(byte4, byteorder=sys.byteorder)
            byte5bits = int.from_bytes(byte5, byteorder=sys.byteorder)
            byte6bits = int.from_bytes(byte6, byteorder=sys.byteorder)
            byte7bits = int.from_bytes(byte7, byteorder=sys.byteorder)
            byte8bits = int.from_bytes(byte8, byteorder=sys.byteorder)
            byte9bits = int.from_bytes(byte9, byteorder=sys.byteorder)

            #print(str(byte1bits) + " " + str(byte2bits) + " " + str(byte3bits) + " " + str(byte4bits) + " " + str(byte5bits) + " " + str(byte6bits) + " " + str(byte7bits) + " " + str(byte8bits) + " " + str(byte9bits))
            
            #convert to bits, store as a string
            byte1bits = str(bin(byte1bits)[2:].zfill(8))
            byte2bits = str(bin(byte2bits)[2:].zfill(8))
            byte3bits = str(bin(byte3bits)[2:].zfill(8))
            byte4bits = str(bin(byte4bits)[2:].zfill(8))
            byte5bits = str(bin(byte5bits)[2:].zfill(8))
            byte6bits = str(bin(byte6bits)[2:].zfill(8))
            byte7bits = str(bin(byte7bits)[2:].zfill(8))
            byte8bits = str(bin(byte8bits)[2:].zfill(8))
            byte9bits = str(bin(byte9bits)[2:].zfill(8))
            
            clear = lambda: os.system("cls")
            clear()
            
            print(byte1bits)
            print(byte2bits)
            print(byte3bits)
            print(byte4bits)
            print(byte5bits)
            print(byte6bits)
            print(byte7bits)
            print(byte8bits)
            print(byte9bits)

            LbyteFound = 0
            MbyteFound = 0
            HbyteFound = 0

            #sort L/M/H-Bytes
            if byte1bits.startswith("00") == True and LbyteFound == 0:
                    Lbyte = str(byte1bits)
                    LbyteFound = 1
                    print("Lbyte - byte1bits active")
            if byte2bits.startswith("00") == True and LbyteFound == 0:
                    Lbyte = str(byte2bits)
                    LbyteFound = 1
                    print("Lbyte - byte2bits active")
            if byte3bits.startswith("00") == True and LbyteFound == 0:
                    Lbyte = str(byte3bits)
                    LbyteFound = 1
                    print("Lbyte - byte3bits active")
            if byte4bits.startswith("00") == True and LbyteFound == 0:
                    Lbyte = str(byte4bits)
                    LbyteFound = 1
                    print("Lbyte - byte4bits active")
            if byte5bits.startswith("00") == True and LbyteFound == 0:
                    Lbyte = str(byte5bits)
                    LbyteFound = 1
                    print("Lbyte - byte5bits active")
            if byte6bits.startswith("00") == True and LbyteFound == 0:
                    Lbyte = str(byte6bits)
                    LbyteFound = 1
                    print("Lbyte - byte6bits active")
            if byte7bits.startswith("00") == True and LbyteFound == 0:
                    Lbyte = str(byte7bits)
                    LbyteFound = 1
                    print("Lbyte - byte7bits active")
            if byte8bits.startswith("00") == True and LbyteFound == 0:
                    Lbyte = str(byte8bits)
                    LbyteFound = 1
                    print("Lbyte - byte8bits active")
            if byte9bits.startswith("00") == True and LbyteFound == 0:
                    Lbyte = str(byte9bits)
                    LbyteFound = 1
                    print("Lbyte - byte9bits active")


            if byte1bits.startswith("01") == True and MbyteFound == 0:
                    Mbyte = str(byte1bits)
                    MbyteFound = 1
                    print("Mbyte - byte1bits active")
            if byte2bits.startswith("01") == True and MbyteFound == 0:
                    Mbyte = str(byte2bits)
                    MbyteFound = 1
                    print("Mbyte - byte2bits active")
            if byte3bits.startswith("01") == True and MbyteFound == 0:
                    Mbyte = str(byte3bits)
                    MbyteFound = 1
                    print("Mbyte - byte3bits active")
            if byte4bits.startswith("01") == True and MbyteFound == 0:
                    Mbyte = str(byte4bits)
                    MbyteFound = 1
                    print("Mbyte - byte4bits active")
            if byte5bits.startswith("01") == True and MbyteFound == 0:
                    Mbyte = str(byte5bits)
                    MbyteFound = 1
                    print("Mbyte - byte5bits active")
            if byte6bits.startswith("01") == True and MbyteFound == 0:
                    Mbyte = str(byte6bits)
                    MbyteFound = 1
                    print("Mbyte - byte6bits active")
            if byte7bits.startswith("01") == True and MbyteFound == 0:
                    Mbyte = str(byte7bits)
                    MbyteFound = 1
                    print("Mbyte - byte7bits active")
            if byte8bits.startswith("01") == True and MbyteFound == 0:
                    Mbyte = str(byte8bits)
                    MbyteFound = 1
                    print("Mbyte - byte8bits active")
            if byte9bits.startswith("01") == True and MbyteFound == 0:
                    Mbyte = str(byte9bits)
                    MbyteFound = 1
                    print("Mbyte - byte9bits active")


            if byte1bits.startswith("10") == True and HbyteFound == 0:
                    Hbyte = str(byte1bits)
                    print("Hbyte - byte1bits active")
                    HbyteFound=1
            if byte2bits.startswith("10") == True and HbyteFound == 0:
                    Hbyte = str(byte2bits)
                    print("Hbyte - byte2bits active")
                    HbyteFound=1
            if byte3bits.startswith("10") == True and HbyteFound == 0:
                    Hbyte = str(byte3bits)
                    print("Hbyte - byte3bits active")
                    HbyteFound=1
            if byte4bits.startswith("10") == True and HbyteFound == 0:
                    Hbyte = str(byte4bits)
                    print("Hbyte - byte4bits active")
                    HbyteFound=1
            if byte5bits.startswith("10") == True and HbyteFound == 0:
                    Hbyte = str(byte5bits)
                    print("Hbyte - byte5bits active")
                    HbyteFound=1
            if byte6bits.startswith("10") == True and HbyteFound == 0:
                    Hbyte = str(byte6bits)
                    print("Hbyte - byte6bits active")
                    HbyteFound=1
            if byte7bits.startswith("10") == True and HbyteFound == 0:
                    Hbyte = str(byte7bits)
                    print("Hbyte - byte7bits active")
                    HbyteFound=1
            if byte8bits.startswith("10") == True and HbyteFound == 0:
                    Hbyte = str(byte8bits)
                    print("Hbyte - byte8bits active")
                    HbyteFound=1
            if byte9bits.startswith("10") == True and HbyteFound == 0:
                    Hbyte = str(byte9bits)
                    print("Hbyte - byte9bits active")
                    HbyteFound=1


            #reduce to data (ignore ID bits)
            Lbyte = Lbyte[2:8]
            Mbyte = Mbyte[2:8]
            Hbyte6 = Hbyte[2:8]
            Hbyte = Hbyte[2:8]

            #get 16 bit value
            value16 = str(Hbyte) + str(Mbyte) + str(Lbyte)
            #print(value16)

            #get 18 bit value
            value18 = str(Hbyte6) + str(Mbyte) + str(Lbyte)
            value18 = int(value18, 2)

            #conversion
            measure=int(value16, 2)
            #print(measure)

            #calculate measurement
            try:
                    result=((measure-98232)*10)/65536
            except:
                    print("measurement calculation failed")
            
            #output
            if value18 == 262075:
                    print("Too much data for selected baud rate")
            if value18 == 262076:
                    print("There is no peak present")
            if value18 == 262077:
                    print("Peak is located in front of the measuring range (MR)")
            if value18 == 262078:
                    print("Peak is located behind the measuring range (MR)")
            if value18 <= 262074:
                    NULL=0
                    print(measure)
                    print(result)

            #ser.read(64)

    except:
            #NULL=0
            print("NO DATA")

    time.sleep(0.01)

Script Output:

01001010
10000000
00011010
01000111
11000000
00111001
01000011
11011111
00000100
Lbyte - byte3bits
Mbyte - byte1bits
Hbyte - byte2bits
666
-14.88739013671875

altough the script (sometimes) detects the L/M/H byte I never get any valid calculated measuring value out of it -14.88... instead of 0-10 (millimeters)

ricode
  • 9
  • 3

0 Answers0