Click here to Skip to main content
14,451,614 members
Rate this:
Please Sign up or sign in to vote.
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 10-Jun-19 9:03am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100