Login Page - Create Account

DTC Protocol Discussion Forum


Date/Time: Fri, 29 Nov 2024 14:52:49 +0000



Post From: Python interface for DTC protocol library

[2017-04-17 07:53:32]
Queeq - Posts: 42
Thanks for the hint. I tried to test it, but I can't make it work.

What I did is I compiled the proto3 file, taken from my SC installation, using latest google.protobuf. It compiled successfully and I am able to access messages from Python. I wrote the following script as a test:


""" Data retrieval from SierraChart services """

import socket
import struct

from DTC import DTCProtocol_pb2 as Dtc


def construct_message(m, message_type):
""" Prepends message size and type, appends ProtocolType and packs it to binary

Args:
m (DTCProtocol): DTC protobuf message
message_type (int): DTC message type

Returns:
bytes: Binary string
"""
try:
# Try to set ProtocolVersion as most messages contain this field
m.ProtocolVersion = Dtc.CURRENT_VERSION
except AttributeError:
pass
m.ProtocolType = "DTC" # ProtocolType is always the same
total_len = 4 + m.ByteSize() # 2 bytes Size + 2 bytes Type
header = struct.pack('<HH', total_len, message_type) # Prepare 4-byte little-endian header
binary_message = m.SerializeToString()
print(binary_message) # <-- This print outputs b'\x08\x07\x1a\x03DTC', which is wrong
return header + binary_message

SC_INTRADAY = ("127.0.0.1", 11100)
SC_HISTORICAL = ("127.0.0.1", 11101)
ENCODING = Dtc.BINARY_ENCODING

# Create encoding request
message = Dtc.EncodingRequest()
message.Encoding = ENCODING
data = construct_message(message, Dtc.ENCODING_REQUEST)

# Connect
s = socket.socket()
s.connect(SC_INTRADAY)

# Send encoding request
s.send(data)
# Receive encoding response
response = s.recv(1024)
# 2 bytes with offset of 2 must indicate this is encoding response message type
assert struct.unpack_from('<H', response, 2)[0] == Dtc.ENCODING_RESPONSE
encoding_response = Dtc.EncodingResponse()
print(encoding_response.ParseFromString(response[4:]).Encoding)

For some reason, EncodingRequest message doesn't get correctly serialized: it gives b'\x08\x07\x1a\x03DTC'. As a result, SC logs "Unexpected DTC encoding request: 4412484".

I'm pretty stuck here and would appreciate some help.

Thanks!