Click here to Skip to main content
15,612,351 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
See more:
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
Posted
Updated 2-Mar-23 2:22am

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.
 
Share this answer
 
Comments
[no name] 18-Jun-19 10:45am    
thanks!
OriginalGriff 18-Jun-19 10:51am    
You're welcome!
Arman Kazi 10-Sep-21 14:37pm    
you are an idiot
OriginalGriff 10-Sep-21 14:59pm    
You sound like a child, so I'll be polite.
When you come onto a real world site and start being rude to random strangers that you might want help from, that probably a bad move. When you do that on a question / answer that hasn't even been directed to you so sound like a younger, dumber child than you started as. We call those Trolls and they get no respect, no help, no friends.
I would like to give you something to think about. So, remember that your actions today will stay with you pretty much forever: particularly online. So your behaviour, your attitude, your comments while you are a child will probably affect your education, job prospects, and even chances of dating in future. Behave nicely, and that is how people see you, now and in future. Act like a griefer in GTAV and that is how people will see you as well ...

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