I want to bring ASCII into WIN-10 running Jython 2.7 on a com port from my
Arduino. I send *Axxxx to the Arduino and it replies with -Axxxx. This code
works solid when tested with the Arduino IDE serial monitor.
Jim K.
Houston TX
Using Jython code (below) written in Netbeans IDE -- I send "*ATest/n" out
the port and get back the correct decimal values that represent the ASCII
table.
Question 1 -- I have to use the time.sleep(2.0) statements or the outStream
won't be sent. Why? & how to fix it correctly.
Question 2 -- I tried to use readline() but the compiler says it doesn't
exist.
Question 3 -- how do I properly convert the read() byte data to ASCII? &
how can I get the SIZE of inStream
///////////// code ///////////////////////////
import sys, socket, gnu.io
import gnu.io.RXTXPort as RXTX
import time
__author__ = "jwkel"
__date__ = "$Oct 20, 2020 3:16:32 PM$"
if __name__ == "__main__":
print "Hello World"
# Here's a listing to demonstrate serial port connection
# with Jython and the RXTX module.
# Much more needs to be done to make it a useful
# program but it demonstrates the key points
# to make a connection!
# Hope this helps anyone just starting out on a similar project.
# Graham
#Jython.py Serial Port connection with RXTX
print "inport"
#Open port COM5
portId=gnu.io.CommPortIdentifier.getPortIdentifier("COM5")
serialPort=portId.open("Demo1",2000) #(Name,delay)
#Set port parameters
serialPort.setSerialPortParams(9600,RXTX.DATABITS_8,RXTX.STOPBITS_1,RXTX.PARITY_NONE)
print "set port"
#Open input & output streams
outStream=serialPort.getOutputStream()
inStream=serialPort.getInputStream()
time.sleep(2.0)
#Define some output and write it to the serial port
toOutput="*ATest\n"
outStream.write(toOutput)
outStream.flush()
time.sleep(2.0)
s =''
for num in range(7):
s = inStream.read()
print s
#s = inStream.read()
#print s
#Close inStream, outStream before closing the port itself
outStream.close()
inStream.close()
serialPort.close()
print "end Hello World"
///////////////// Netbeans output window //////////////////
Hello World
inport
set port
45 (-) I added the ASCII chr for reference
65 (A)
84 (T)
101 (e)
115 (s)
116 (t)
10 (/n)
end Hello World
|