TCP Driver Writable tag issue

I’m using the TCP driver to connect to a device that sends out data. The device requires an “ACK” message so I’m using the “Writable” tag that is created with the TCP driver. Be default this tag is a String. The last line in the code below is where I’m having my issue. The device is expecting a byte to be written back. In this case I’m trying to write a number 1 or 16x01. What the system.write function is sending is actually 16x31 which is an ASCII 1. I tried changing the “Writable” tag from a string to and Int without success. How can I write a nonASCII value to the “Writable” tag?

from java.lang import *
from org.python.core.util import StringUtil
#Read the tag that stores the data from the TCP driver opject
data = system.tag.read("[]Group2/4103/Message")
#Convert string into a byte array
bArr = StringUtil.toBytes(data.value) #Read string into byte array
#Parse header data
packetID = bArr[0]
msgCnt = ((bArr[2] & 0xff) << 8) | bArr[3] & 0xff #Combine two bytes into UInt
qStat = ((bArr[4] & 0xff) << 8) | bArr[5] & 0xff #Combine two bytes into UInt
#Send PacketID back to device to increment message
[color=#0000FF]system.tag.write("[]Group2/4103/Writable", bArr[0]) #Sends as ASCII vs byte[/color]

Did you try wrapping in an int() to force it to an integer value? Maybe Python is just converting to a string value on it’s own.

system.tag.write("[]_Group2_/4103/Writable", int(bArr[0])) #Sends as ASCII vs byte

I tried what you suggested and it is still writing an ASCII 1 (16x31) instead of 16x01. I appreciate the suggestion!

Hmmm… that’s weird. I’ll have to play around with it a little bit and see why it would do that…

You know what… thinking about it a bit more. This is probably a problem with the TCP driver, not writing the correct value to the tag. Just because you’re writing a integer 1 to the tag doesn’t mean that when the TCP driver sends it out that it will send it as an integer. I don’t know much about the TCP driver, as I don’t use it, but I would start looking to see if there is a way to force the TCP driver to send in a specific format.

I would think it would respect the tag data type but you never know…

I created my own TCP sender code for sending messages to Marquees and other junk, worst case is you can edit/implement this and send what you need on your own and just bypass the TCP driver…

service is the port number. Change that to whatever port number you need or edit the function to be able to pass in a list of port number. Whatever you want.

[code]def sendToMarquee(ipList, message):

import socket
from org.apache.log4j import Logger
log = Logger.getLogger("app.Marquees.sendToMarquee")
status = False
statusList = []

for host in ipList:
	service = 49999
	family = socket.AF_UNSPEC
	socktype = socket.SOCK_STREAM
	protocol = 0
	flags = socket.AI_PASSIVE
	
	result = socket.getaddrinfo(host, service, family, socktype, protocol, flags)
		
	for family, socktype, protocol, canonname, sockaddr in result:
		try:
			socket.setdefaulttimeout(.5)
			sock = socket.socket(family, socktype, protocol)
		except socket.error:
			log.error("Problem creating communication socket for marquee: %s." %(host))
			continue
		try:
			sock.connect(sockaddr)
			sock.sendall(message)
			statusList.append(True)
							
		except socket.error:
			sock.close()
			sock = None
			log.error("Communication error or timeout for marquee: %s. Network communication could be down." %(host))
			statusList.append(False)
			continue
		break
		
	if sock != None:
		sock.close()
	
if False in statusList:
	return False
else:
	return True[/code]

I was hoping to use the TCP driver to manage the connections as I’ll have up 15 connections that will be active all the time. I will start looking into your code and perform direct socket connections. Great code by the way and I appreciate you sharing!!!

If you want to write 0x01 to the tag, try using chr(1) as the value in your write call.

Writing chr(1) DOES WORK. I had to change the driver’s “Writable” tag back to a string which is what it is by default.

In summary I had to wrap my chr(bARR[0]) to send nonASCII values down the TCP driver.

Thank you everyone for your help