# Serial module error failed to open port

**URL:** <https://forum.inductiveautomation.com/t/serial-module-error-failed-to-open-port/63712>\
**Category:** Ignition\
**Tags:** ignition81, scripting, perspective\
**Created:** [September 2, 2022, 1:30pm UTC](https://forum.inductiveautomation.com/t/serial-module-error-failed-to-open-port/63712 "2022-09-02T13:30:57Z")\
**Posts on this page:** 11\
**Page:** 1

<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:** [September 2, 2022, 1:30pm UTC](https://forum.inductiveautomation.com/t/serial-module-error-failed-to-open-port/63712/1 "2022-09-02T13:30:57Z")

</div>

Hi everyone!

I’ve been getting an error when executing a script from the gateway but not when I do it from the console.

I’m also able to call the function in the gateway from the script console and it works. So, I’m not sure why the gateway is unable to open the port. “java.io.IOException: failed to open port: COM8”

> **Java error**
>
> com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File “”, line 7, in File “”, line 165, in TimedMessages File “”, line 32, in sendManyMsgManyNumbers at com.inductiveautomation.ignition.modules.serial.scripting.SerialScriptModule.openSerialPort(SerialScriptModule.java:258) at com.inductiveautomation.ignition.modules.serial.scripting.SerialScriptModule$PortManager.(SerialScriptModule.java:209) at com.inductiveautomation.ignition.modules.serial.scripting.SerialScriptModule.port(SerialScriptModule.java:201) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) java.io.IOException: java.io.IOException: failed to open port: COM8
> 
> at org.python.core.Py.JavaError(Py.java:547)
> 
> at org.python.core.Py.JavaError(Py.java:538)
> 
> at org.python.core.PyReflectedFunction. **call** (PyReflectedFunction.java:192)
> 
> at org.python.core.PyReflectedFunction. **call** (PyReflectedFunction.java:208)
> 
> at org.python.core.PyObject. **call** (PyObject.java:400)
> 
> at org.python.pycode.\_pyx511.sendManyMsgManyNumbers$3(:42)
> 
> at org.python.pycode.\_pyx511.call\_function()
> 
> at org.python.core.PyTableCode.call(PyTableCode.java:173)
> 
> at org.python.core.PyBaseCode.call(PyBaseCode.java:168)
> 
> at org.python.core.PyFunction. **call** (PyFunction.java:437)
> 
> at org.python.pycode.\_pyx508.TimedMessages$8(:166)
> 
> at org.python.pycode.\_pyx508.call\_function()
> 
> at org.python.core.PyTableCode.call(PyTableCode.java:173)
> 
> at org.python.core.PyBaseCode.call(PyBaseCode.java:134)
> 
> at org.python.core.PyFunction. **call** (PyFunction.java:416)
> 
> at org.python.pycode.\_pyx510.f$0(:10)
> 
> at org.python.pycode.\_pyx510.call\_function()
> 
> at org.python.core.PyTableCode.call(PyTableCode.java:173)
> 
> at org.python.core.PyCode.call(PyCode.java:18)
> 
> at org.python.core.Py.runCode(Py.java:1687)
> 
> at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:796)
> 
> at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runCode(ProjectScriptLifecycle.java:819)
> 
> at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:748)
> 
> at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runCode(ProjectScriptLifecycle.java:800)
> 
> at com.inductiveautomation.ignition.common.script.TimerScriptTask.run(TimerScriptTask.java:90)
> 
> at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
> 
> at java.base/java.util.concurrent.FutureTask.runAndReset(Unknown Source)
> 
> at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
> 
> at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
> 
> at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
> 
> at java.base/java.lang.Thread.run(Unknown Source)
> 
> Caused by: org.python.core.PyException: java.io.IOException: java.io.IOException: failed to open port: COM8
> 
> … 31 common frames omitted
> 
> Caused by: java.io.IOException: failed to open port: COM8
> 
> at com.inductiveautomation.ignition.modules.serial.scripting.SerialScriptModule.openSerialPort(SerialScriptModule.java:258)
> 
> at com.inductiveautomation.ignition.modules.serial.scripting.SerialScriptModule$PortManager.(SerialScriptModule.java:209)
> 
> at com.inductiveautomation.ignition.modules.serial.scripting.SerialScriptModule.port(SerialScriptModule.java:201)
> 
> at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 
> at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
> 
> at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
> 
> at java.base/java.lang.reflect.Method.invoke(Unknown Source)
> 
> at org.python.core.PyReflectedFunction. **call** (PyReflectedFunction.java:190)

Does the Script Hint Scope have something to do with it?  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/e/8/e8d03cea3dbbe20d522b8b127c16db3155810117.png)

---

<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:** [September 2, 2022, 2:59pm UTC](https://forum.inductiveautomation.com/t/serial-module-error-failed-to-open-port/63712/2 "2022-09-02T14:59:30Z")

</div>

When you run it in the script console, it opens the com port on the designer machine. When run in a gateway event, it opens a com port on the gateway machine.

The hInt scope doesn’t actually change the scope you are running, just what help you see.

---

<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:** [September 2, 2022, 3:20pm UTC](https://forum.inductiveautomation.com/t/serial-module-error-failed-to-open-port/63712/3 "2022-09-02T15:20:43Z")

</div>

Script console and designer are on the same machine.

Some weeks ago I did notice that there is a difference on available resources when using local/remote gateway vs local/local gateway. So, in this case my designer and my gateway are on a local machine.

Edit #1:  
I’m using TeamViewer to connect to that server but I don’t think that changes anything as I’m running designer and Script Console on that machine. If I were to run Script Console on my computer using a VPN to the Gateway, that would be different scenario. It would be a “local computer to a remote gateway” case.

---

<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:** [September 2, 2022, 3:26pm UTC](https://forum.inductiveautomation.com/t/serial-module-error-failed-to-open-port/63712/4 "2022-09-02T15:26:35Z")

</div>

The same machine, but not necessarily running as the same user according to the OS. The gateway will, unless you have changed things, be authenticating (assuming Windows) as the LOCAL\_SYSTEM account, which has various limitations. One of those might be COM port access; I don’t know offhand.

---

<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:** [September 2, 2022, 5:20pm UTC](https://forum.inductiveautomation.com/t/serial-module-error-failed-to-open-port/63712/5 "2022-09-02T17:20:18Z")

</div>

Maybe Windows doesn’t recognize me as a local user but I can point out that when I’m calling the function from the script console it works but when another function in the gateway calls the function it doesn’t. Functions in the gateway won’t check who is logged in as its supposed to work on its own.

I will make a dummy function in the gateway that calls the other function that uses the serial port. Maybe that helps to nail the error

---

<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:** [September 2, 2022, 5:38pm UTC](https://forum.inductiveautomation.com/t/serial-module-error-failed-to-open-port/63712/6 "2022-09-02T17:38:23Z")

</div>

Ignition _isn’t_ checking anything. Ignition is just making the attempt from whatever OS process is executing your script. In the script console, that’s the local designer instance, which is running “as you” (per the OS). When you execute a script in Perspective, or from a tag, or anywhere else _on the gateway_, the COM port invocation is happening from the gateway’s process. Your OS is rejecting the attempt to open the COM port when the request is coming from the gateway process.

---

<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:** [September 2, 2022, 6:59pm UTC](https://forum.inductiveautomation.com/t/serial-module-error-failed-to-open-port/63712/7 "2022-09-02T18:59:50Z")

</div>

> [@tupanaster](#):
>
> Functions in the gateway won’t check who is logged in as its supposed to work on its own.

This is simply not correct. The gateway service runs as a specific user, too. `LOCAL_SYSTEM` by default for Windows.

---

<div class="post-metadata">

**Author:** ![kcollins](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kcollins/32/19646_2.png) [@kcollins](https://forum.inductiveautomation.com/u/kcollins)\
**Post date:** [September 2, 2022, 7:10pm UTC](https://forum.inductiveautomation.com/t/serial-module-error-failed-to-open-port/63712/8 "2022-09-02T19:10:44Z")

</div>

I’ll supplement one other thought here as well, for your consideration. You could also experience a failure to acquire a given port if it is in use by another process (i.e. if it is locked by designer).

---

<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:** [September 5, 2022, 7:36am UTC](https://forum.inductiveautomation.com/t/serial-module-error-failed-to-open-port/63712/9 "2022-09-05T07:36:43Z")

</div>

> [@paul-griffith](#):
>
> The gateway will, unless you have changed things, be authenticating (assuming Windows) as the LOCAL\_SYSTEM account, which has various limitations. One of those might be COM port access; I don’t know offhand.

That is good to know. This is an old server so all the configuration over the years was done by several other people.

> [@paul-griffith](#):
>
> In the script console, that’s the local designer instance, which is running “as you” (per the OS). When you execute a script in Perspective, or from a tag, or anywhere else _on the gateway_, the COM port invocation is happening from the gateway’s process.

Ok, so Script Console runs as a local designer instance and all the rest on the gateway's process.

> [@pturmel](#):
>
> This is simply not correct. The gateway service runs as a specific user, too. `LOCAL_SYSTEM` by default for Windows.

That is not something I think of when I'm scripting in the gateway. I guess I should consider that now since I'm using a device connected to the computer.

My engineering PC has Windows 10 and it just worked, this old server is 2012 R2. Some security/permission settings may be different.

> [@kcollins](#):
>
> I’ll supplement one other thought here as well, for your consideration. You could also experience a failure to acquire a given port if it is in use by another process (i.e. if it is locked by designer).

I would drop that as a possibility since it's a new device that only Ignition is accessing. Also, I'm using `with` functions which will open and close the port automatically.

Edit #1:  
Maybe a custom and permanent filter in Windows Event log will help me here. Has anyone created a good filter to debug every case on Ignition?

---

<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:** [September 5, 2022, 1:43pm UTC](https://forum.inductiveautomation.com/t/serial-module-error-failed-to-open-port/63712/10 "2022-09-05T13:43:36Z")

</div>

> [@tupanaster](#):
>
> Also, I’m using `with` functions which will open and close the port automatically.

Actually, the `with` handler for the serial functions is/was broken. I don't recall what version fixed it, if fixed yet at all. Note that re-opening a port imposes a substantial delay (~100ms) where you can lose characters.

Meanwhile, consider not running the designer on the same computer as the gateway. If you are writing code for gateway scope, test it with a message handler that you can call from the script console (via `system.util.sendRequest`), don't run it directly in the script console.

Catch your exceptions in the message handler and stringify them (with backtrace) to return to the caller.

---

<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:** [September 8, 2022, 2:38pm UTC](https://forum.inductiveautomation.com/t/serial-module-error-failed-to-open-port/63712/11 "2022-09-08T14:38:58Z")

</div>

I did some testing today.

In Gateway Message Handlers I added a “Test” function:

```auto
def handleMessage(payload):
	import traceback
	import java.lang.Exception
	print 'Error test'
	errorDict = {}
	try:
		AT.OpenComPort('COM8')
		
	except Exception, e:
		errorDict['jython'] = str(e)
	except java.lang.Exception, e:
		errorDict['java'] = str(e)
		errorDict['javaCause'] = str(e.getCause())
	return errorDict

```

Using the script console in another computer:

```auto
returnValue = system.util.sendRequest(project='TestProject', messageHandler='Test', payload={'hoursOn':15})
print returnValue

```

My results are:

```auto
{'javaCause': 'None', 'java': 'java.io.IOException: failed to open port: COM8'}

```

Basically the same error but at the same time I think it’s a confirmation that java is being blocked when trying to open a port.
