How to use java to connect to a mobile phone or SMS cat through a serial port in linux to send a Chinese text message
Source: Internet
Author: User
In linux, you can use java to connect to a mobile phone or SMS cat through a serial port to send a Chinese text message-Linux general technology-Linux programming and kernel information. The following is a detailed description. Download the jar package, decompress it, and copy the so file to/usr/lib.
Copy javax. comm. properties to the jre directory.
Driver = (CommDriver) Class. forName (driverName). newInstance ();
Driver. initialize ();
System. out. println ("Win32Driver Initialized ");
} Catch (Exception e ){
E. printStackTrace ();
}
// If multiple ports exist
While (portList. hasMoreElements ()){
PortID = (comatrix ortidentifier) portList. nextElement ();
System. out. println ("COM:" + portID. getName ());
// If (portID. getName (). equals ("COM1 "))
If (portID. getName (). equals ("/dev/ttyS0 "))
Try {
SPort = (SerialPort) portID. open (owner, keeptime );
Break;
} Catch (PortInUseException e ){
E. printStackTrace ();
System. exit (1 );
}
}
Try {
If (sPort! = Null)
{
In = sPort. getInputStream ();
Out = sPort. getOutputStream ();
Try {
SPort. addEventListener (this );
SPort. notifyOnDataAvailable (true );
} Catch (TooManyListenersException e ){
E. printStackTrace ();
}
}
} Catch (Exception e)
{
E. printStackTrace ();
}
Note out the connection method in windows.
All code
Import javax. comm .*;
Import java. util. Enumeration;
Import java. util. TooManyListenersException;
Import java. io .*;
Public class ATSms implements SerialPortEventListener {
Private SerialPort sPort = null;
Private InputStream in = null;
Private OutputStream out = null;
Private byte B [] = new byte [1024];
Private String cmd = "";
Private int len = 0;
Private String recvMsg = "";
Public ATSms ()
{
Init ();
}
Public synchronized void serialEvent (SerialPortEvent serialPortEvent ){
Int c = 0;
// If a serial port event occurs
If (serialPortEvent. getEventType () = SerialPortEvent. DATA_AVAILABLE)
{
Try {
System. out. print ("Recv :");
While (c = in. read ())! =-1 ){
System. out. print (char) c );
RecvMsg + = (char) c );
}
} // Try
Catch (IOException e ){
E. printStackTrace ();
}
}
}
Private static String getUnicodeString (String s)
{
String unicodeStr = "";
For (int I = 0; I {
String c = Integer. toHexString (s. charAt (I) & 0 xffff );
While (c. length () <4)
{
C = "0" + c;
}
UnicodeStr + = c;
}
Return unicodeStr;
}
Private static String getSMobile (String mobile)
{
StringBuffer sMobile = new StringBuffer ("68 ");
For (int I = 0; I {
SMobile. append (I + 1 SMobile. append (mobile. charAt (I ));
}
Return sMobile. toString ();
}
} Catch (Exception e)
{
E. printStackTrace ();
}
}
Public void close ()
{
Try {
Thread. sleep (4000 );
In. close ();
Out. close ();
} Catch (Exception e ){
E. printStackTrace (); // To change body of catch statement use File | Settings | File Templates.
}
SPort. close ();
}
Public int sendSms (String recv, String content)
{
Int sendok = 1;
For (int I = 0; I {
Sendok = sendShortSms (recv, (I + 70 <= content. length ())? Content. substring (I, I + 70): content. substring (I )));
}
Return sendok;
}
Private int sendShortSms (String recv, String content)
{
Int sendok =-1;
Try {
System. out. println ("send sms to:" + recv + "\ ncontent:" + content );
Content = getUnicodeString (content );
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service