This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaudTest.java
72 lines (62 loc) · 1.99 KB
/
BaudTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Hayden Walker's Baudrate Simulator
* 2021-04-27 23:06
* Usage: BaudTest [file] [baudrate]
*/
import java.util.*;
import java.io.*;
public class BaudTest {
public static void main(String[] args) throws InterruptedException {
// If the user provides no or too many arguments, throw an error
if((args.length == 0) || (args.length > 2)){
inputError();
}
try {
// Get input file from arguments
File inputFile = new File(args[0]);
// Create a scanner object to read from the input file
Scanner fileReader = new Scanner(inputFile);
// Get baudrate from arguments, catching any formatting issues
long waitTime = 1000; // Dummy value before attempting to read args
try {
// Convert baudrate to bytes/second (8 bits * 1 second / bytes/second)
waitTime = 8000 / Integer.parseInt(args[1]);
} catch(NumberFormatException e) {
// If there was a formatting error, tell the user
inputError();
}
// Read through each line in the file
while(fileReader.hasNextLine()) {
// Break current line into characters
String thisLine = fileReader.nextLine();
char[] thisLineChars = thisLine.toCharArray();
if(thisLineChars.length != 0) {
// If the line is not blank, iterate through each character and print it, with a pause in between to simulate baudrate
for(char i : thisLineChars) {
Thread.sleep(waitTime);
System.out.print(i);
}
// Then make a new line
Thread.sleep(waitTime);
System.out.println();
} else {
// If the line is blank, only make a new line
Thread.sleep(waitTime);
System.out.println();
}
}
// Close the file reader
fileReader.close();
} catch(FileNotFoundException e) {
// If there was an error reading the file, tell the user
inputError();
}
}
static void inputError() {
/*
* Catch any errors, and tell the user how to use the program
*/
System.out.println("Invalid arguments\nUsage: BaudTest [file] [baudrate]");
System.exit(1);
}
}