# Script console serial port

**URL:** <https://forum.inductiveautomation.com/t/script-console-serial-port/58369>\
**Category:** Ignition\
**Tags:** ignition81, vision\
**Created:** [April 12, 2022, 6:02pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369 "2022-04-12T18:02:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [April 12, 2022, 6:02pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/1 "2022-04-12T18:02:45Z")

</div>

Hello, I have this code but I got this error , can anyOne help me with this please ?  
I have and 0x80 in the address but says that cannot be cast to class

 ![IMG_20220412_125939](https://us1.discourse-cdn.com/inductiveautomation/original/3X/4/1/41c9f1c2e32f09ccd8cf08ae046a7f4a91d3dcbf.jpeg)  
 ![IMG_20220412_125932](https://us1.discourse-cdn.com/inductiveautomation/original/3X/1/a/1abb540d2e68b7c4779c9bac02fa6dfa84b9f1b7.jpeg)

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [April 12, 2022, 6:21pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/2 "2022-04-12T18:21:22Z")

</div>

Please post code and errors as code, not photographs or screenshots. Use the `</>` code formatting button to apply syntax highlighting and preserve indentation. Then we can copy and edit it in our answers.

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [April 12, 2022, 6:23pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/3 "2022-04-12T18:23:43Z")

</div>

I don’t think you are calling writeBytes correctly, and i don’t think you are giving it a java.lang.byte.  
[https://docs.inductiveautomation.com/display/DOC81/system.serial.writeBytes](https://docs.inductiveautomation.com/display/DOC81/system.serial.writeBytes)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 12, 2022, 6:27pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/4 "2022-04-12T18:27:12Z")

</div>

Something is coercing `[adress]` into a Python ‘singleton’, which likely means `True`, `False` or `None`, but I’m not sure why…

---

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [April 12, 2022, 7:04pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/5 "2022-04-12T19:04:18Z")

</div>

```auto
y = 0
portNum = "COM10"
address = 0x80
cmd = 0x04
qty = 0x01
confirm = 0x0B
chkSum3 = adress ^ cmd ^ qty
chkSum2 = adress ^ confirm
chkConfNoError = adress ^ 3 ^ 0 ^ qty
with system.serial.port(
			port = portNum,
			bitRate =system.serial.BIT_RATE_19200,
			dataBits = system.serial.DATA_BITS_8,
			handshake = system.serial.HANDSHAKE_NONE,
			hardwareFlowControl = False,
			parity = system.serial.PARITY_MARK,
			stopBits = system.serial.STOP_BITS_1) as port:
			
	port.writeBytes([adress])

with system.serial.port(
			port = portNum,
			bitRate =system.serial.BIT_RATE_19200,
			dataBits = system.serial.DATA_BITS_8,
			handshake = system.serial.HANDSHAKE_NONE,
			hardwareFlowControl = False,
			parity = system.serial.PARITY_SPACE,
			stopBits = system.serial.STOP_BITS_1) as port2:
			
	port2.writeBytes([cmd, qty, chkSum3])
	#time.sleep(0.001)
	#port2.writeBytes([qty])
	#time.sleep(0.001)
	#port2.writeBytes([chkSum3])
	response = port2.readBytes(5, 5)
	#time.sleep(0.015)
		
with system.serial.port(
			port = portNum,
			bitRate =system.serial.BIT_RATE_19200,
			dataBits = system.serial.DATA_BITS_8,
			handshake = system.serial.HANDSHAKE_NONE,
			hardwareFlowControl = False,
			parity = system.serial.PARITY_MARK,
			stopBits = system.serial.STOP_BITS_1) as port:
	
	port.writeBytes([adress])

with system.serial.port(
			port = portNum,
			bitRate =system.serial.BIT_RATE_19200,
			dataBits = system.serial.DATA_BITS_8,
			handshake = system.serial.HANDSHAKE_NONE,
			hardwareFlowControl = False,
			parity = system.serial.PARITY_SPACE,
			stopBits = system.serial.STOP_BITS_1) as port2:

	while True:
		port2.writeBytes([confirm, chkSum2])
		#time.sleep(0.001)
		#port2.writeBytes([chkSum2])
		res = list(port2.readBytes(5, 5))
		#time.sleep(0.015)
		y = y + 1
		if res == [adress, 3, 0, qty, chkConfNoError]:
			break
			
	print "Dirección " + str(adress)
	print res
	print list(response)
	print str(y)

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 12, 2022, 7:09pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/6 "2022-04-12T19:09:54Z")

</div>

The point of the `system.serial.port` context manager is that you don’t have to open and close the port manually - you just write all your code that has to interact with the port within the indented scope.  
Try this, and see what the output is of the two `print` statements before the first write:

```python
y = 0
portNum = "COM10"
adress = 0x80
cmd = 0x04
qty = 0x01
confirm = 0x0B
chkSum3 = adress ^ cmd ^ qty
chkSum2 = adress ^ confirm
chkConfNoError = adress ^ 3 ^ 0 ^ qty

with system.serial.port(
			port = portNum,
			bitRate =system.serial.BIT_RATE_19200,
			dataBits = system.serial.DATA_BITS_8,
			handshake = system.serial.HANDSHAKE_NONE,
			hardwareFlowControl = False,
			parity = system.serial.PARITY_MARK,
			stopBits = system.serial.STOP_BITS_1) as port:

	toWrite = [adress]
	print toWrite
	print type(toWrite)
	
	port.writeBytes(toWrite)
	port.writeBytes([cmd, qty, chkSum3])
	#time.sleep(0.001)
	#port.writeBytes([qty])
	#time.sleep(0.001)
	#port.writeBytes([chkSum3])
	response = port.readBytes(5, 5)
	#time.sleep(0.015)

	port.writeBytes([adress])

	while True:
		port.writeBytes([confirm, chkSum2])
		#time.sleep(0.001)
		#port.writeBytes([chkSum2])
		res = list(port.readBytes(5, 5))
		#time.sleep(0.015)
		y = y + 1
		if res == [adress, 3, 0, qty, chkConfNoError]:
			break
			
	print "Dirección " + str(adress)
	print res
	print list(response)
	print str(y)

```

---

<div class="post-metadata">

**Author:** ![zacht](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@zacht](https://forum.inductiveautomation.com/u/zacht)\
**Post date:** [April 12, 2022, 7:20pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/7 "2022-04-12T19:20:32Z")

</div>

I’m trying to understand your code (@PGriffith’s code). Is it possible that `port2` isn’t declared? If it is declared somewhere under the hood, where?

---

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [April 12, 2022, 7:21pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/8 "2022-04-12T19:21:39Z")

</div>

The error is right here :

`port.writeBytes([address])`

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 12, 2022, 7:30pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/9 "2022-04-12T19:30:07Z")

</div>

> [@zacht](#):
>
> Is it possible that `port2` isn’t declared? If it is declared somewhere under the hood, where?

That was just a typo from rewriting the original code.

> [@Esme\_Lopez](#):
>
> The error is right here :

Yeah, I want to see the output from the print statements in my modified code:

```auto
	toWrite = [adress]
	print toWrite
	print type(toWrite)
	
	port.writeBytes(toWrite)

```

---

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [April 12, 2022, 7:31pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/10 "2022-04-12T19:31:45Z")

</div>

Yes , I got a type = list and towrite I got [128] , but If I put 127 everything is fine

y = 0  
portNum = “COM10”  
address = 0x80  
cmd = 0x04  
qty = 0x01  
confirm = 0x0B  
chkSum3 = adress ^ cmd ^ qty  
chkSum2 = adress ^ confirm  
chkConfNoError = adress ^ 3 ^ 0 ^ qty  
with system.serial.port(  
port = portNum,  
bitRate =system.serial.BIT\_RATE\_19200,  
dataBits = system.serial.DATA\_BITS\_8,  
handshake = system.serial.HANDSHAKE\_NONE,  
hardwareFlowControl = False,  
parity = system.serial.PARITY\_MARK,  
stopBits = system.serial.STOP\_BITS\_1) as port:

```
port.writeBytes([adress])
```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [April 12, 2022, 7:36pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/11 "2022-04-12T19:36:11Z")

</div>

You might need to work around a Jython bug:

> [@\[IGN-4991, IGN-4996\]system.file.writeFile encoding](https://forum.inductiveautomation.com/t/ign-4991-ign-4996-system-file-writefile-encoding/19878/14):
>
> Once I decided the built in ways were broken, I just built a bigger hammer: def pythonByteString\_to\_jarray(pyString): return jarray.array(((x if x \< 128 else (x - 256)) for x in bytearray(pyString)),'b')

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 12, 2022, 8:33pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/12 "2022-04-12T20:33:18Z")

</div>

It isn’t really a jython bug. Just that java bytes are signed, so java byte arrays must be made of values from -128 to 127.

---

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [April 12, 2022, 8:37pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/13 "2022-04-12T20:37:02Z")

</div>

I get it , but I need to send 128, do you know if is there more options?  
How to send a number without the operator - 128

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [April 12, 2022, 9:15pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/14 "2022-04-12T21:15:19Z")

</div>

The simplest expression to get the proper signed byte is `((b + 128) & 0xff) - 128`. For each byte.

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [April 12, 2022, 9:19pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/15 "2022-04-12T21:19:27Z")

</div>

Seems like we should make these functions easier to call… perhaps by accepting `int[]` as well.

---

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [April 12, 2022, 9:23pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/16 "2022-04-12T21:23:31Z")

</div>

I understand but we need with no sign

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 13, 2022, 6:04pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/17 "2022-04-13T18:04:51Z")

</div>

You actually do, because that is how the underlying Java works. You can use the expression @pturmel mentioned in a helper function to format the bytes as they make sense to you to what is required for the array. Combining with what @paul-griffith mentioned earlier should yield something like:

```python
y = 0
portNum = "COM10"
adress = 0x80
cmd = 0x04
qty = 0x01
confirm = 0x0B
chkSum3 = adress ^ cmd ^ qty
chkSum2 = adress ^ confirm
chkConfNoError = adress ^ 3 ^ 0 ^ qty

def toSignedBytes(bytesIn):
	''' Convert a list of unsigned byte values to signed values.
	    If a single integer value is given, a list is still returned
	'''
	if isinstance(bytesIn, int):
		bytesIn = [bytesIn]
	return [((byte+128) & 255)-128 for byte in bytesIn]

# Initialize the byte lists
cmdList = toSignedBytes([address, cmd, qty, chkSum3])
confirmList = toSignedBytes([address, confirm, chkSum2])

with system.serial.port(
			port = portNum,
			bitRate =system.serial.BIT_RATE_19200,
			dataBits = system.serial.DATA_BITS_8,
			handshake = system.serial.HANDSHAKE_NONE,
			hardwareFlowControl = False,
			parity = system.serial.PARITY_MARK,
			stopBits = system.serial.STOP_BITS_1) as port:
	# Send the command
	print "Dirección: ", str(adress)
	print "Cmd bytes:", cmdList
	port.writeBytes(cmdList)
	# Get the response
	cmdResponse = list(port.readBytes(5, 5))
	print "Cmd response: ", (cmdResponse)	

	print "Confirm bytes: ", confirmList 	
	while True:
		# Send the confirm
		port.writeBytes(confirmList)
		# Get the response
		confirmResponse = list(port.readBytes(5, 5))
		
		y = + 1
		if res == [adress, 3, 0, qty, chkConfNoError]:
			break
			
	print "Confirm Response: ", confirmResponse
	print "Tries: ", str(y) 

```

---

<div class="post-metadata">

**Author:** ![Esme\_Lopez](https://avatars.discourse-cdn.com/v4/letter/e/ecb155/32.png) [@Esme\_Lopez](https://forum.inductiveautomation.com/u/Esme_Lopez)\
**Post date:** [April 14, 2022, 2:04pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/18 "2022-04-14T14:04:57Z")

</div>

Yes but I am looking for the opposite , I mean upside down

---

<div class="post-metadata">

**Author:** ![tupanaster](https://avatars.discourse-cdn.com/v4/letter/t/5daacb/32.png) [@tupanaster](https://forum.inductiveautomation.com/u/tupanaster)\
**Post date:** [May 3, 2022, 10:28am UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/19 "2022-05-03T10:28:09Z")

</div>

> [@paul-griffith](#):
>
> The point of the `system.serial.port` context manager is that you don’t have to open and close the port manually - you just write all your code that has to interact with the port within the indented scope.

Opening and closing ports is not needed. Should we also avoid delays between calls?

I have a while looking up for posts using the Serial Module with delays in between

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [May 3, 2022, 2:28pm UTC](https://forum.inductiveautomation.com/t/script-console-serial-port/58369/20 "2022-05-03T14:28:34Z")

</div>

As a general practice, a lockable external resource like a serial port should only be held by your application while it’s actively being read to or written from. There are exceptions, but I would stick to that as a guideline before doing something else.

[Next page](https://forum.inductiveautomation.com/t/script-console-serial-port/58369.md?page=2)
