Click here to Skip to main content
15,885,309 members

How do I convert Python script to java

maleksafadi asked:

Open original thread
Python
#!/usr/bin/python
import serial
import sys
import tty
import termios
import select


fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
tty.setraw(sys.stdin.fileno())

if len(sys.argv) <= 1:
	termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
	print("provide interface as arguments (e.g. /dev/ttyACM0)")
        
	sys.exit()

ser = serial.Serial(sys.argv[1], 115200, timeout = 0.5)


def readSerial():
    try:
        readOut = ser.readline().rstrip()
        if readOut:
            print('\r'+readOut)
    except:
        pass
    


    
def printmenu():
    print ("\r\n1-Version 2-subscribe 3-start 4-vend0.5 5-vend0 6-stop 0 to stop\r")              

printmenu()
while True:
    rlist, wlist, elist = select.select([sys.stdin,ser], [], [],0.55555)
    if sys.stdin in rlist:
        
        c = sys.stdin.read(1)
        if c=="1":
            print("--> version")
            ser.write(b'V\n')                                                                                                                                                                          
                                                                                                                                                             
                                                                                                                                                                         
        elif c=="2":                                                                                                                                                                       
            print("\r\n--> start emulation")
            ser.write(b'C,SETCONF,mdb-addr=0x10\n')
            ser.write(b'C,SETCONF,mdb-currency-code=0x1756\n')                                                                                                                                                               
            ser.write(b'C,1\n')                                                                                                                                                               
                                                                                                                                                                                                                                                                                                                                                 
        elif c=="3":                                                                                                                                                                      
            print("\r\n--> start with 10")                                                                                                                                                          
            ser.write(b'C,START,10.0\n')                                                                                                                                                         
        elif c=="4":                                                                                                                                                                      
            print("\r\n--> vend 0.5")                                                                                                                                                               
            ser.write(b'C,VEND,0.5\n')                                                                                                                                                            
        elif c=="5":                                                                                                                                                                       
            print("\r\n--> vend 0")                                                                                                                                                                 
            ser.write(b'C,VEND,0\n')                                                                                                                                                              
        elif c=="6": 
            print("\r\n--> stop")                                                                                                                                                                   
            ser.write(b'C,0\n')
        elif c=="0":
            break                                                                                                                                                               
        else:                                                                                                                                                                                    
            printmenu()
    elif ser in rlist:
        readSerial()                                                                    

termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)



What I have tried:

Someone how can convert this python small script to Java
Tags: Java, Python

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900