File: SimpleSnuV1.java

package info (click to toggle)
rxtx 2.2pre2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 6,960 kB
  • sloc: ansic: 14,367; sh: 10,742; java: 7,629; cpp: 2,717; makefile: 143
file content (178 lines) | stat: -rw-r--r-- 4,677 bytes parent folder | download | duplicates (8)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* This class is one end of a test pair of programs
* SimpleRead.class is the other end.
* To run the test requires two com ports
* and a null modem cable.
* Start SimpleSnuV1 and then run SimpleRead
* on the other machine/com port.
* This code is supplied for demonstration purposes only.
* You are free to use it as you please.  
*/


import java.io.*;
import java.util.*;
import gnu.io.*;

public class SimpleSnuV1 implements Runnable, SerialPortEventListener {

static CommPortIdentifier portId;
static Enumeration portList;

InputStream inputStream;
OutputStream outputStream;
SerialPort serialPort;
Thread readThread;

int numBytes = 0;
String num = "021891383";
String rst = "atz";
String dial ="atd";
String outData = "";
String outDataBuff = "";
boolean running = true;
boolean process = true;
boolean waitForInput = true;

	public static void main(String[] args) {
		if (args.length < 1) {
			System.out.print("SimpleSnuV1.class /dev/ttyxx\n");
			System.exit(-1);
		}
        		portList = CommPortIdentifier.getPortIdentifiers();
		while (portList.hasMoreElements()) {
            		portId = (CommPortIdentifier) portList.nextElement();
           			if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                			if (portId.getName().equals(args[0])) {
                    				SimpleSnuV1 reader = new SimpleSnuV1();
                			} 
            		}
		}
	}

	public SimpleSnuV1() {
		try {
			serialPort = (SerialPort) portId.open("SimpleSnu", 2000);
		} catch (PortInUseException e) {}
		try {
			inputStream = serialPort.getInputStream();
			outputStream = serialPort.getOutputStream();
		} catch (IOException e) {}
		try {
serialPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
		} catch (UnsupportedCommOperationException e) {}
		byte[] readBuffer = new byte[20];
		try {
			serialPort.addEventListener(this);
		} catch (TooManyListenersException e) {
			System.out.print("To Many Event Listeners\n");
			System.exit(-1);
		}
		serialPort.notifyOnDataAvailable(true);
		readThread = new Thread(this);
		readThread.start();
	}

	public void run() {
		int resetCount = 0;
		int numBytes = 0;
		byte[] readBuffer = new byte[20];
		String sReadBuff = "";
		boolean connected = false;
		while (running) {
			if (!connected) {
				try {
					while (inputStream.available() > 0) {
						numBytes = inputStream.read(readBuffer);
						String tmpR = new String(readBuffer);
						sReadBuff += tmpR.substring(0, numBytes); 
					}
				} catch (IOException e) {
					System.exit(1);
				}
				if (!sReadBuff.equals("")) {
					System.out.print(sReadBuff + "\n");
				} else {
					
				}
				int pos = 0;
				if ((pos = sReadBuff.indexOf("atz")) != -1) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {}
					try {
						outputStream.write(new String("OK").getBytes());
						outputStream.write((byte)0x0D);
						outputStream.write((byte)0x0A);
						System.out.print("OK Sent\n");		
					} catch (IOException e) {
						System.exit(1);					
					}
					sReadBuff = "";
				} else if ((pos = sReadBuff.indexOf("atd")) != -1) {
					sReadBuff = "";
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {}
					try {
						outputStream.write(new String("CONNECT 9600").getBytes());
						outputStream.write((byte)0x0D);
						outputStream.write((byte)0x0A);
//						connected = true;
					} catch (IOException e) {
						System.exit(1);
					}
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {}
				}
			} else {

			}
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {}
		}
					
		System.out.print("Normal Exit...\n");
    	}
	public void serialEvent(SerialPortEvent event) {
//		try {
//			Thread.sleep(500);
//		} catch (InterruptedException e) {}
		switch(event.getEventType()) {
			case SerialPortEvent.BI:
				System.out.print("BI\n");				
				break;
			case SerialPortEvent.OE:
				System.out.print("OE\n");
				break;
			case SerialPortEvent.FE:
				System.out.print("FE\n");
				break;
			case SerialPortEvent.PE:
				System.out.print("PE\n");
				break;
			case SerialPortEvent.CD:
				System.out.print("CD\n");
				break;
			case SerialPortEvent.CTS:
				System.out.print("CTS\n");
				break;
			case SerialPortEvent.DSR:
				System.out.print("DSR\n");
				break;
			case SerialPortEvent.RI:
				System.out.print("RI\n");
				break;
			case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
				System.out.print("Out Buff Empty\n");
				break;
			case SerialPortEvent.DATA_AVAILABLE:
//				waitForInput = false;
				System.out.print("Data Available\n");
            			break;
        		}
    	}
}