# Can write to serial port, can't read from it

**URL:** <https://forum.inductiveautomation.com/t/can-write-to-serial-port-cant-read-from-it/2350>\
**Category:** Ignition\
**Created:** [November 16, 2010, 10:41pm UTC](https://forum.inductiveautomation.com/t/can-write-to-serial-port-cant-read-from-it/2350 "2010-11-16T22:41:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![chazeroo](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@chazeroo](https://forum.inductiveautomation.com/u/chazeroo)\
**Post date:** [November 16, 2010, 10:41pm UTC](https://forum.inductiveautomation.com/t/can-write-to-serial-port-cant-read-from-it/2350/1 "2010-11-16T22:41:33Z")

</div>

Hi,

We need View clients to communicate with scales through the pc serial port. We’re trying to avoid using serial/EtherNet adapters. The following (RXTX-2.1-7) code under #—WRITE— works great, but I can’t get the code under #—READ— to work. No errors, just doesn’t read it. I’m testing with a second pc running HyperTerminal. I’d really appreciate any suggestions - thanks.

[code]from [gnu.io](http://gnu.io) import CommPort, CommPortIdentifier, SerialPort  
from [java.io](http://java.io) import FileDescriptor, IOException, InputStream, OutputStream

portIdentifier = CommPortIdentifier.getPortIdentifier(“COM5”)

if portIdentifier.isCurrentlyOwned():  
print “Error: Port is currently in use”  
else:  
commPort = portIdentifier.open(“IgnView-SerialTest”, 2000)  
if isinstance(commPort,SerialPort):  
serialPort = commPort  
serialPort.setSerialPortParams(9600, SerialPort.DATABITS\_7, SerialPort.STOPBITS\_1, SerialPort.PARITY\_NONE)

```
	#---WRITE---
	outStr = serialPort.getOutputStream()
	outStr.write("msg-out ")

	#---READ---

```

# numBytes = -5

# readBuffer = " "

# inStr = serialPort.getInputStream()

# numBytes = inStr.read(readBuffer)

# print numBytes

# print readBuffer

```
	print "done"

else:
	print "Error: Only serial ports are handled by this example."

commPort.close()[/code]
```

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [November 17, 2010, 5:36pm UTC](https://forum.inductiveautomation.com/t/can-write-to-serial-port-cant-read-from-it/2350/2 "2010-11-17T17:36:47Z")

</div>

I don’t think I can really help you with 3rd party libraries like RXTX, which I have no experience with. I’m curious, how did you get RXTX into our software?

---

<div class="post-metadata">

**Author:** ![chazeroo](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@chazeroo](https://forum.inductiveautomation.com/u/chazeroo)\
**Post date:** [November 17, 2010, 7:32pm UTC](https://forum.inductiveautomation.com/t/can-write-to-serial-port-cant-read-from-it/2350/3 "2010-11-17T19:32:47Z")

</div>

From [rxtx.qbang.org/wiki/index.php/In … or\_Windows](http://rxtx.qbang.org/wiki/index.php/Installation_for_Windows) (files are attached)

[color=#0000FF]If you just want to run RXTX enabled programs, follow this procedure.

Identify your Java Runtime Environment’s folder. For version 1.6.0, this usually is c:\Program Files\Java\jre1.6.0\_01\

Copy rxtxParallel.dll to c:\Program Files\Java\jre1.6.0\_01\bin\  
Copy rxtxSerial.dll to c:\Program Files\Java\jre1.6.0\_01\bin\  
Copy RXTXcomm.jar to c:\Program Files\Java\jre1.6.0\_01\lib\ext[/color]  
[rxtx-2.1-7-bins-r2.jar.gz](https://us1.discourse-cdn.com/inductiveautomation/original/1X/05bf42309368142e3841ed6fc7a18e9a968c1b97.gz) (589 KB)

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [November 17, 2010, 9:28pm UTC](https://forum.inductiveautomation.com/t/can-write-to-serial-port-cant-read-from-it/2350/4 "2010-11-17T21:28:17Z")

</div>

Ok fair enough.

You’re still on your own with RXTX however as we can’t really support something we didn’t provide. But to give you a starting point - I’d say that you can’t use a string as a buffer like that - it doesn’t really make sense because strings are immutable.

---

<div class="post-metadata">

**Author:** ![chazeroo](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@chazeroo](https://forum.inductiveautomation.com/u/chazeroo)\
**Post date:** [November 17, 2010, 9:54pm UTC](https://forum.inductiveautomation.com/t/can-write-to-serial-port-cant-read-from-it/2350/5 "2010-11-17T21:54:05Z")

</div>

InputStream.read is looking for a byte array. I tried

readBuffer = range(20) and readBuffer = zeros(20, ‘b’)

before I tried the string. No luck.

---

<div class="post-metadata">

**Author:** ![Colby.Clegg](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/colby.clegg/32/3282_2.png) [@Colby.Clegg](https://forum.inductiveautomation.com/u/Colby.Clegg)\
**Post date:** [November 18, 2010, 1:49am UTC](https://forum.inductiveautomation.com/t/can-write-to-serial-port-cant-read-from-it/2350/6 "2010-11-18T01:49:11Z")

</div>

Well, I’m just shooting in the dark, but I google’d for creating a byte array in jython and came up with:

```auto
import jarray
bytearray = jarray.zeros(1024,'b')
```

I didn’t go beyond that (how to convert the jarray to different types, etc.), but hopefully that helps you get past your current block 😃

---

<div class="post-metadata">

**Author:** ![chazeroo](https://avatars.discourse-cdn.com/v4/letter/c/df705f/32.png) [@chazeroo](https://forum.inductiveautomation.com/u/chazeroo)\
**Post date:** [January 24, 2011, 10:00pm UTC](https://forum.inductiveautomation.com/t/can-write-to-serial-port-cant-read-from-it/2350/7 "2011-01-24T22:00:39Z")

</div>

I finally had time to play with this some more - got it to work with plain ol’ javax.comm.

My next challenge is to get javax.comm & associated files to automatically deploy to clients, since javax.comm is deprecated. I’ll start digging around the Ignition Module Developers forum and see if I can figure it out. I’d appreciate any pointers to start me in the right direction - thanks!

[code]from javax.comm import CommPort, CommPortIdentifier, SerialPort  
from [java.io](http://java.io) import FileDescriptor, IOException, InputStream, OutputStream

#For this code to work:  
#Copy javax.comm.properties (1KB, 8/12/1999) to c:\Program Files\Java\jre6\lib\  
#Copy win32com.dll (28KB, 8/12/1999) to c:\Program Files\Java\jre6\bin\  
#Copy comm.jar (28KB, 8/12/1999) to C:\Program Files\Java\jre6\lib\ext  
#Add this to system environment variable CLASSPATH: ;C:\Program Files\Java\jre6\lib\ext\comm.jar

portIdentifier = CommPortIdentifier.getPortIdentifier(“COM1”)

if portIdentifier.isCurrentlyOwned():  
print “Error: Port is currently in use”  
else:  
commPort = portIdentifier.open(“IgnView-SerialTest”, 2000)  
if isinstance(commPort,SerialPort):  
serialPort = commPort  
serialPort.setSerialPortParams(9600, SerialPort.DATABITS\_8, SerialPort.STOPBITS\_1, SerialPort.PARITY\_NONE)

```
  #---WRITE---
  outStr = serialPort.getOutputStream()
  outStr.write("msg-out")

  #---READ---
  inStr = serialPort.getInputStream()
  inChr = -1
  buffer = ""
  try:
  	inChr = inStr.read()
    while ((inChr > -1) and (chr(inChr) != '\n')):
  		buffer = buffer + chr(inChr)
    	inChr = inStr.read()

  	print buffer

  except IOException, e:
  	e.printStackTrace()
  	commPort.close()	

```

else:  
print “Error: Only serial ports are handled by this example.”

commPort.close()  
[/code]

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [January 25, 2011, 12:21am UTC](https://forum.inductiveautomation.com/t/can-write-to-serial-port-cant-read-from-it/2350/8 "2011-01-25T00:21:23Z")

</div>

Due to the nature of the java comm extension and how it has to be installed on each client machine, I don’t think you’re going to be able to automatically deploy it through Java Web Start, which is how our clients run.
