-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSender_Receiver.java
More file actions
148 lines (126 loc) · 3.16 KB
/
Sender_Receiver.java
File metadata and controls
148 lines (126 loc) · 3.16 KB
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
import java.util.Date;
public class Sender_Receiver
{
private static final long STANDARD=500;
private static final long LONG=2000;
private static final long VERYLONG=10000;
SerialConnection mySerial =null;
static final private char m_cCntrlZ=(char)26;
String m_sIn, m_sOut;
private long m_lDelay=STANDARD;
String m_sPduMsg=null;
int m_iLength=0;
String m_sPort_Id;
public String m_sTextMsg;
public String m_sPhoneNum;
// the message center
//private SerialParameters defaultParameters= new SerialParameters (m_sPort_Id,9600,0,0,8,1,0);
public int m_iStep;
public int m_iStatus=-1;
public long m_lMessageNo=-1;
pdu_conversion convert=new pdu_conversion();
/**
* connect to the port and start the dialogue thread
*/
public int openPort (String m_sPort_Id) throws Exception
{
this.m_sPort_Id=m_sPort_Id;
SerialParameters params = new SerialParameters (m_sPort_Id,9600,0,0,8,1,0);
mySerial =new SerialConnection (params);
mySerial.openConnection();
//log("start");
return 0;
}
/**
* implement the dialogue thread,
* message / response via steps,
* handle time out
*/
public boolean setMode(String m_sCommand)
{
//log ("m_iStep:"+m_iStep);
mySerial.send(m_sCommand);
String result= mySerial.getIncommingString() ;
System.out.println("result :"+result);
int l_iExpectedResult=-1;
l_iExpectedResult=result.indexOf("OK");
if(l_iExpectedResult!=-1)
return true;
else
return false;
}
public boolean send(String m_sPhoneNum,String m_sSecured)
{
try
{
m_sPduMsg=convert.pdu_msg_former(m_sPhoneNum,m_sSecured);
}
catch(Exception ex)
{
System.out.println(ex.toString());
}
m_iLength=(m_sPduMsg.length())/2-1;
//System.out.println("sender");
mySerial.send("at+cmgs="+m_iLength+"\n");
//log("length :"+m_iLength);
String result= mySerial.getIncommingString() ;
int l_iExpectedResult=-1;
l_iExpectedResult=result.indexOf(">");
if(l_iExpectedResult!=-1)
{
mySerial.send(m_sPduMsg+m_cCntrlZ);
result= mySerial.getIncommingString() ;
//l_iExpectedResult=result.indexOf("OK");
if(l_iExpectedResult!=-1)
{
//log(" message sent\n");
return true;
}
else
{
//log ("sending failed\n");
return false;
}
}
else
{
//log ("sending failed\n");
return false;
}
}
public return_format receive(String l_sCommand)
{
String return_text="";
return_format ret_obj=new return_format();
try
{
mySerial.send(l_sCommand);
//log(l_sCommand);
String l_sReceivedMsg=mySerial.getIncommingString();
int l_iOffset=l_sReceivedMsg.indexOf("0791");
String l_sDecodableMsg=l_sReceivedMsg.substring(l_iOffset);
//log("received string :"+l_sDecodableMsg);
pdu_decode obj=new pdu_decode();
ret_obj=obj.decode(l_sDecodableMsg);
//log(return_text);
}
catch(Exception e)
{
log(e.toString());
}
return ret_obj;
}
public String receive_length(String l_sCommand)
{
mySerial.send(l_sCommand);
//log(l_sCommand);
return (mySerial.getIncommingString() );
}
/**
* logging function, includes date and class name
*/
private void log(String s)
{
System.out.println (new java.util.Date()+":"+this.getClass().getName()+":"+s);
}
}