Jython read/write serial port
James Kelly <[email protected]> Sun, 25 Oct 2020 17:56:52 -0500
| Newsgroups | gmane.comp.lang.jython.user |
|---|---|
| Message-ID | <CANio7e8NE9kwJS3joWXXUN+O9fYoiPY+cT63ZaqpFLwRCY8Vpg@mail.gmail.com> |
--===============0430486272968757551==
Content-Type: multipart/alternative; boundary="0000000000007e769705b286bb8c"
--0000000000007e769705b286bb8c
Content-Type: text/plain; charset="UTF-8"
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
--0000000000007e769705b286bb8c
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div class=3D"gmail_default" style=3D"font-size:large">I w=
ant to bring ASCII into WIN-10 running Jython 2.7 on a com port from my Ard=
uino. I send *Axxxx to the Arduino and it replies with -Axxxx. This code wo=
rks solid when tested with the Arduino IDE serial monitor.</div><div class=
=3D"gmail_default" style=3D"font-size:large"><br></div><div class=3D"gmail_=
default" style=3D"font-size:large">Jim K.</div><div class=3D"gmail_default"=
style=3D"font-size:large">Houston TX</div><div class=3D"gmail_default" sty=
le=3D"font-size:large"><br></div><div class=3D"gmail_default" style=3D"font=
-size:large">Using Jython code (below) written in Netbeans IDE -- I send &q=
uot;*ATest/n" out the port and get back the correct decimal values tha=
t represent the ASCII table.</div><div class=3D"gmail_default" style=3D"fon=
t-size:large"><br></div><div class=3D"gmail_default" style=3D"font-size:lar=
ge">Question 1 -- I have to use the time.sleep(2.0) statements or the outSt=
ream won't be sent. Why? & how to fix it correctly.</div><div class=
=3D"gmail_default" style=3D"font-size:large"><br></div><div class=3D"gmail_=
default" style=3D"font-size:large">Question 2 -- I tried to use readline() =
but the compiler=C2=A0says it doesn't exist.</div><div class=3D"gmail_d=
efault" style=3D"font-size:large"><br></div><div class=3D"gmail_default" st=
yle=3D"font-size:large">Question 3 -- how do I properly convert the read() =
byte data to ASCII? & how can I get the SIZE of inStream</div><div clas=
s=3D"gmail_default" style=3D"font-size:large"><br></div><div class=3D"gmail=
_default" style=3D"font-size:large">///////////// code ////////////////////=
///////</div><div class=3D"gmail_default" style=3D"font-size:large">import =
sys, socket, <a href=3D"http://gnu.io">gnu.io</a><br>import gnu.io.RXTXPort=
as RXTX<br>import time<br><br>__author__ =3D "jwkel"<br>__date__=
=3D "$Oct 20, 2020 3:16:32 PM$"<br><br>if __name__ =3D=3D "=
__main__":<br>=C2=A0 =C2=A0 print "Hello World"<br>=C2=A0 =
=C2=A0 # Here's a listing to demonstrate serial port connection<br>=C2=
=A0 =C2=A0 # with Jython and the RXTX module.<br>=C2=A0 =C2=A0 # Much more =
needs to be done to make it a useful<br>=C2=A0 =C2=A0 # program but it demo=
nstrates the key points<br>=C2=A0 =C2=A0 # to make a connection!<br>=C2=A0 =
=C2=A0 # Hope this helps anyone just starting out on a similar project.<br>=
<br>=C2=A0 =C2=A0 # Graham<br><br>=C2=A0 =C2=A0 #Jython.py Serial Port conn=
ection with RXTX<br>=C2=A0 =C2=A0 print "inport"<br>=C2=A0 =C2=A0=
#Open port COM5<br>=C2=A0 =C2=A0 portId=3Dgnu.io.CommPortIdentifier.getPor=
tIdentifier("COM5")<br>=C2=A0 =C2=A0 serialPort=3DportId.open(&qu=
ot;Demo1",2000) =C2=A0 =C2=A0 =C2=A0 =C2=A0#(Name,delay)<br><br>=C2=A0=
=C2=A0 #Set port parameters<br>=C2=A0 =C2=A0 serialPort.setSerialPortParam=
s(9600,RXTX.DATABITS_8,RXTX.STOPBITS_1,RXTX.PARITY_NONE)<br>=C2=A0 =C2=A0 p=
rint "set port"<br>=C2=A0 =C2=A0 #Open input & output streams=
<br>=C2=A0 =C2=A0 outStream=3DserialPort.getOutputStream()<br>=C2=A0 =C2=A0=
inStream=3DserialPort.getInputStream()<br>=C2=A0 =C2=A0 time.sleep(2.0)<br=
>=C2=A0 =C2=A0 #Define some output and write it to the serial port<br>=C2=
=A0 =C2=A0 toOutput=3D"*ATest\n" =C2=A0 <br>=C2=A0 =C2=A0 outStre=
am.write(toOutput)<br>=C2=A0 =C2=A0 outStream.flush()<br>=C2=A0 =C2=A0 time=
.sleep(2.0)<br><br>=C2=A0 =C2=A0 s =3D''<br>=C2=A0 =C2=A0 for num i=
n range(7):<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 s =3D inStream.read()<br>=C2=A0 =
=C2=A0 =C2=A0 =C2=A0 print s<br>=C2=A0 =C2=A0 #s =3D inStream.read()<br>=C2=
=A0 =C2=A0 #print s<br>=C2=A0 =C2=A0 <br>=C2=A0 =C2=A0 #Close inStream, out=
Stream before closing the port itself<br>=C2=A0 =C2=A0 outStream.close()<br=
>=C2=A0 =C2=A0 inStream.close()<br>=C2=A0 =C2=A0 serialPort.close()<br>=C2=
=A0 =C2=A0 print "end Hello World"<br>=C2=A0 =C2=A0<br>//////////=
/////// Netbeans output window //////////////////</div><div class=3D"gmail_=
default" style=3D"font-size:large">Hello World<br>inport<br>set port<br>45 =
(-) I added the ASCII chr for reference<br>65 (A)<br>84 (T)<br>101 (e)<br>1=
15 (s)<br>116 (t)<br>10 (/n)<br>end Hello World<br><br></div><div class=3D"=
gmail_default" style=3D"font-size:large"><br></div><div class=3D"gmail_defa=
ult" style=3D"font-size:large"><br></div><div class=3D"gmail_default" style=
=3D"font-size:large"><br></div></div>
--0000000000007e769705b286bb8c--
--===============0430486272968757551==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
--===============0430486272968757551==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Jython-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jython-users
--===============0430486272968757551==--