|
| 1 | +import java.io.File; // Import the File class |
| 2 | +import java.io.FileWriter; // Import the FileWriter class |
| 3 | +import java.io.IOException; // Import the IOException class to handle errors |
| 4 | + |
| 5 | +public class userApp { |
| 6 | + |
| 7 | + public static String echoRequestCode = "E1092"; |
| 8 | + public static String imageRequestCode = "M2490"; |
| 9 | + public static String imageRequestCodeError = "G0342"; |
| 10 | + public static String gpsRequestCode = "P4642"; |
| 11 | + public static String ackCode = "Q2289"; |
| 12 | + public static String nackCode = "R8766"; |
| 13 | + |
| 14 | + public static void main(String[] param) { |
| 15 | + (new userApp()).demo(); |
| 16 | + } |
| 17 | + |
| 18 | + public Modem initModem(int speed, int timeout){ |
| 19 | + Modem modem; |
| 20 | + modem=new Modem(); |
| 21 | + modem.setSpeed(speed); |
| 22 | + modem.setTimeout(timeout); |
| 23 | + modem.open("ithaki"); |
| 24 | + return modem; |
| 25 | + } |
| 26 | + |
| 27 | + public void listen(Modem modem, String delimiter){ |
| 28 | + int k; |
| 29 | + String rxmessage =""; |
| 30 | + System.out.println("Sent request for message."); |
| 31 | + |
| 32 | + //Endless loop for listening for message |
| 33 | + for (;;) { |
| 34 | + try { |
| 35 | + k=modem.read(); //Read a byte from the modem |
| 36 | + if (k==-1){ |
| 37 | + System.out.println("Connection closed."); |
| 38 | + break; |
| 39 | + } |
| 40 | + //System.out.println((char)k); |
| 41 | + //System.out.print((char)k+" <"+k+"> "); |
| 42 | + rxmessage = rxmessage + (char)k; |
| 43 | + //System.out.println(rxmessage); |
| 44 | + //Break endless loop by catching break sequence. |
| 45 | + if (rxmessage.indexOf(delimiter)>-1){ //If the break sequence is found break |
| 46 | + System.out.println(rxmessage); |
| 47 | + System.out.println("End of listen message."); |
| 48 | + break; |
| 49 | + } |
| 50 | + } catch (Exception x) { |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public void sendAndListen(Modem modem, String txmessage, String delimiter){ |
| 57 | + int k; |
| 58 | + String rxmessage = ""; |
| 59 | + //We create a different variable for the message we will send to Ithaki. The reason for differentiating is because for some reason the terminal couldn't print the |
| 60 | + //txmessage if we sent it along with the txmessage. |
| 61 | + String txmessageToSend = txmessage + "\r"; |
| 62 | + modem.write(txmessageToSend.getBytes()); |
| 63 | + String toPrint = "Sent " + txmessage + " request code."; |
| 64 | + System.out.println(toPrint); |
| 65 | + listen(modem, delimiter); |
| 66 | + } |
| 67 | + |
| 68 | + public void demo() { |
| 69 | + //Initialize modem |
| 70 | + Modem modem = initModem(1000, 2000); |
| 71 | + |
| 72 | + //Listen for welcome message |
| 73 | + listen(modem, "\r\n\n\n"); |
| 74 | + |
| 75 | + //Send a test message and listen for answer |
| 76 | + sendAndListen(modem, "test", "PSTOP\r\n"); |
| 77 | + |
| 78 | + //Create text file to store echo messages |
| 79 | + try { |
| 80 | + File myObj = new File("echopackets.txt"); |
| 81 | + if (myObj.createNewFile()) { |
| 82 | + System.out.println("File created: " + myObj.getName()); |
| 83 | + } else { |
| 84 | + System.out.println("File already exists."); |
| 85 | + } |
| 86 | + } catch (IOException e) { |
| 87 | + System.out.println("An error occurred."); |
| 88 | + e.printStackTrace(); |
| 89 | + } |
| 90 | + //Send echoRequestCodes and listen for answers |
| 91 | + for(int i =0; i<2; i++){ |
| 92 | + sendAndListen(modem, echoRequestCode, "PSTOP"); |
| 93 | + try { |
| 94 | + FileWriter myWriter = new FileWriter("echopackets.txt"); |
| 95 | + myWriter.write("Written "+i+"th line.\r"); |
| 96 | + myWriter.close(); |
| 97 | + System.out.println("Successfully wrote to the file."); |
| 98 | + } catch (IOException e) { |
| 99 | + System.out.println("An error occurred."); |
| 100 | + e.printStackTrace(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // NOTE : Stop program execution when "NO CARRIER" is detected. |
| 105 | + // NOTE : A time-out option will enhance program behavior. |
| 106 | + modem.close(); |
| 107 | + } |
| 108 | +} |
0 commit comments