Click here to Skip to main content
15,867,308 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 3-Jun-23 15:52pm

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 ...

Here is what Bing chat came up with when I asked it to convert your code. I'm pasting it here exactly as it produced it.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import com.fazecast.jSerialComm.SerialPort;

public class Main {
    public static void main(String[] args) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        if (args.length <= 0) {
            System.out.println("provide interface as arguments (e.g. /dev/ttyACM0)");
            System.exit(0);
        }
        SerialPort ser = SerialPort.getCommPort(args[0]);
        ser.setComPortParameters(115200, 8, 1, 0);
        ser.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 0, 0);
        ser.openPort();
        
        printmenu();
        while (true) {
            try {
                if (System.in.available() > 0) {
                    int c = reader.read();
                    if (c == '1') {
                        System.out.println("--> version");
                        ser.writeBytes("V\n".getBytes(), 2);
                    } else if (c == '2') {
                        System.out.println("\r\n--> start emulation");
                        ser.writeBytes("C,SETCONF,mdb-addr=0x10\n".getBytes(), 23);
                        ser.writeBytes("C,SETCONF,mdb-currency-code=0x1756\n".getBytes(), 34);
                        ser.writeBytes("C,1\n".getBytes(), 4);
                    } else if (c == '3') {
                        System.out.println("\r\n--> start with 10");
                        ser.writeBytes("C,START,10.0\n".getBytes(), 12);
                    } else if (c == '4') {
                        System.out.println("\r\n--> vend 0.5");
                        ser.writeBytes("C,VEND,0.5\n".getBytes(), 10);
                    } else if (c == '5') {
                        System.out.println("\r\n--> vend 0");
                        ser.writeBytes("C,VEND,0\n".getBytes(), 8);
                    } else if (c == '6') {
                        System.out.println("\r\n--> stop");
                        ser.writeBytes("C,0\n".getBytes(), 4);
                    } else if (c == '0') {
                        break;
                    } else {
                        printmenu();
                    }
                }
                while (ser.bytesAvailable() == 0)
                    Thread.sleep(20);

                byte[] readBuffer = new byte[ser.bytesAvailable()];
                int numRead = ser.readBytes(readBuffer, readBuffer.length);
                System.out.print("\r" + new String(readBuffer));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        ser.closePort();
    }

    public static void printmenu() {
        System.out.println(
                "\r\n1-Version 2-subscribe 3-start 4-vend0.5 5-vend0 6-stop 0 to stop\r");
    }
}

This code uses the jSerialComm library to handle serial communication. You can add it to your project by including the following dependency in your pom.xml file:
<dependency>
    <groupId>com.fazecast</groupId>
    <artifactId>jSerialComm</artifactId>
    <version>2.7.1</version>
</dependency>

Or you can download the jar file from the jSerialComm website and add it to your project’s build path.

Please note that this code is just an example and may require further modifications to work correctly in your specific use case.
 
Share this answer
 

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