`
learnmore
  • 浏览: 588017 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

linux下的串口开发小记

阅读更多
1.了解一下什么是串口.
  串口叫做串行接口,也称串行通信接口,按电气标准及协议来分包括RS-232-C、RS-422、RS485、USB等。 RS-232-C、RS-422与RS-485标准只对接口的电气特性做出规定,不涉及接插件、电缆或协议。USB是近几年发展起来的新型接口标准,主要应用于高速数据传输领域。

   而我需要记录的也是RS232标准串口

  RS-232-C:也称标准串口,是目前最常用的一种串行通讯接口。它是在1970年由美国电子工业协会(EIA)联合贝尔系统、 调制解调器厂家及计算机终端生产厂家共同制定的用于串行通讯的标 准。它的全名是“数据终端设备(DTE)和数据通讯设备(DCE)之间 串行二进制数据交换接口技术标准”。传统的RS-232-C接口标准有22根线,采用标准25芯D型插头座。后来的PC上使用简化了的9芯D型插座。现在应用中25芯插头座已很少采用。现在的台式电脑一般有两个串行口:COM1和COM2,从设备管理器的端口列表中就可以看到。硬件表现为计算机后面的9针D形接口,由于其形状和针脚数量的原因,其接头又被称为DB9接头。现在有很多手机数据线或者物流接收器都采用COM口与计算机相连,很多投影机,液晶电视等设备都具有了此接口,厂家也常常会提供控制协议,便于在控制方面实现编程受控,现在越来越多的智能会议室和家居建设都采用了中央控制设备对多种受控设备的串口控制方式。

2.如果你是用笔记本开发串口程序,可以使用pcmicia转串口设备.

3.关于在linux下开发基于串口的java程序

下载jar包http://java.sun.com/products/javacomm/
解压,把其中so文件复制到/usr/lib

把javax.comm.properties复制到jre目录下

把comm.jar放到classpath目录下

串口连接:

CommPortIdentifier portID;
String owner = new String("modem");
int keeptime = 5000;
Enumeration portList;
portList = CommPortIdentifier.getPortIdentifiers();

// String driverName = "com.sun.comm.Win32Driver";
String driverName = "com.sun.comm.LinuxDriver";
CommDriver driver = null;

try {
// System.loadLibrary("win32com");

driver = (CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
System.out.println("Win32Driver Initialized");
} catch (Exception e) {
e.printStackTrace();
}
// 如果有多个端口
while (portList.hasMoreElements()) {
portID = (CommPortIdentifier) 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();
}

其中注释掉的是windows下的连接方法

全部代码
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 (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<S.LENGTH();I++)
{
String c = Integer.toHexString(s.charAt(i) & 0xffff);
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<MOBILE.LENGTH();I=I+2)
{
sMobile.append((i+1<MOBILE.LENGTH())?MOBILE.CHARAT(I+1):"F");
</MOBILE.LENGTH())?MOBILE.CHARAT(I+1):"F");
sMobile.append(mobile.charAt(i));
}
return sMobile.toString();
}

private void init()
{
CommPortIdentifier portID;
String owner = new String("modem");
int keeptime = 5000;
Enumeration portList;
portList = CommPortIdentifier.getPortIdentifiers();

// String driverName = "com.sun.comm.Win32Driver";
String driverName = "com.sun.comm.LinuxDriver";
CommDriver driver = null;

try {
// System.loadLibrary("win32com");
// System.out.println("Win32Com Library Loaded");

driver = (CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
System.out.println("Win32Driver Initialized");
} catch (Exception e) {
e.printStackTrace();
}
// 如果有多个端口
while (portList.hasMoreElements()) {
portID = (CommPortIdentifier) portList.nextElement();
System.out.println("COM:" + portID.getName());
// if (portID.getName().equals("COM1"))
if (portID.getName().equals("/dev/ttyS0"))
try {
// System.out.println("try to open the /dev/ttyS0");
sPort = (SerialPort) portID.open(owner, keeptime);
// System.out.println("try to open the /dev/ttyS0 OK");
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();
}
}


/**
System.out.print("set PDU mode :");
out.write("at+cmgf=0\r".getBytes());
out.flush();
// len = in.read(b);
for(int i=0;i<3;i++)
{
recvMsg = readThread.readComMsg();
if(!"".equals(recvMsg))break;
Thread.sleep(1000);
System.out.println("Sleep 1001.");
}

if(recvMsg.indexOf("OK")>=0)
{
System.out.println("OK");
}else
{
System.out.println("ERROR");
}
*/

}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<CONTENT.LENGTH();I=I+70)
{
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);

// content = (j+70>uc.length()/2)?uc.substring(j):uc.substring(j,j+140);
// System.out.println(j + ":" + content);
cmd = "at+cmgs=" + Integer.toString(15+content.length()/2) + "\r";
out.write(cmd.getBytes());
out.flush();
System.out.println("Send:" + cmd);

for(int i=0;i<5;i++)
{
if(recvMsg.indexOf(">")>=0)
{
recvMsg = recvMsg.substring(recvMsg.indexOf(">")+1);
break;
}else
{
try
{
Thread.sleep(1000);
}catch(Exception e)
{
}
}
}

cmd = Integer.toHexString(content.length()/2 & 0xffff);
if(cmd.length()<2)cmd="0"+cmd;
cmd = "0011000D91" + getSMobile(recv) + "000800" + cmd + content;
cmd = cmd.toUpperCase();
out.write(cmd.getBytes());
out.write(26);
out.flush();
System.out.println("Send:" + cmd);

for(int i=0;i<5;i++)
{
if(recvMsg.indexOf("OK")>=0)
{
recvMsg = recvMsg.substring(recvMsg.indexOf("OK")+2);
sendok = 1;
break;
}else
{
try
{
Thread.sleep(1000);
}catch(Exception e)
{
}
}
}

}catch(Exception e)
{
sendok = -1;
e.printStackTrace();
}
return sendok;
}

public static void main(String args[])
{
String c = ("16日" );
ATSms at = new ATSms();
at.sendSms("13999999999",c);
at.close();
}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics