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

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 import CommPort, CommPortIdentifier, SerialPort
from 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]

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?

From rxtx.qbang.org/wiki/index.php/In … or_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 (589 KB)

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.

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.

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

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 :smiley:

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 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]

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.