Click here to Skip to main content
14,737,856 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
#!/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
Posted
Updated 8-Aug-20 17:35pm

1 solution

Don't. Converting languages never produces good quality code in the target language.

Instead, use that code as a "specification" for the Java version, and design new code that works for Java to do the same function. Python and Java don't share any frameworks, so what works for Pythin won;t work well in Java, and vice versa.
   
Comments
[no name] 18-Jun-19 10:45am
   
thanks!
OriginalGriff 18-Jun-19 10:51am
   
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




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