- 浏览: 588017 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
Garlic_90:
ireport分页的话代码写起来有些复杂,我以前试过,比较简单 ...
ireport分页显示 -
feijiing:
nice,problem solved,thanks!
虚拟机安装centos no valid devices were found on which to cereate new file systems -
Jocken:
引用的jar包需要怎么加在命令里面?十多个呢,为什么配在MAN ...
linux 如何运行jar包 -
xiaoqiao800:
看到你的问题,有帮助,我之前都是手动的clear项目下的cla ...
The project cannot be built until build path errors are resolved -
mfkdzhou:
楼主好,我现在也遇到这个问题,可以把源代码发一份不?谢谢了。8 ...
java打印
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();
}
}
串口叫做串行接口,也称串行通信接口,按电气标准及协议来分包括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();
}
}
发表评论
-
linux 操作系统分区
2021-08-20 11:24 449https://www.cnblogs.com/cobcmw/ ... -
ipad白屏并且上面有苹果图标不停的重启无法开机
2020-04-14 16:58 15611.长按开机键5秒,同时按住home建10秒,松开开机键,这个 ... -
shadowsocksr
2018-06-02 17:37 6461、开通Vultr 我当时选的是2.5$/month的套餐, ... -
记录
2014-07-29 08:27 7221.修改httpclient中源码关于cookie头中的过期时 ... -
yum 安装php遇到php53-common conflicts with php-common
2014-01-08 12:06 18681yum 安装php的时候,用命令yum -y install ... -
关于u盘无法引导的可能原因
2013-04-05 17:42 1136前几天在使用u盘引导的时候无法正常引导,首先检查启动u盘是否制 ... -
boot.ini修改导致系统无法正常启动
2013-04-05 17:35 1242前几天由于装双系统,修改了boot.ini,里面的内容都是网上 ... -
bt4在启动时出现filename must be either or block list...
2013-04-05 17:28 1302在启动bt4的时候出现这个报错,经检查是由于menu.lst中 ... -
如何制作BT4 u盘启动盘
2013-04-05 17:21 1600所需要软件:①UltraISO;②grubinst;③grub ... -
windows7中的telnet命令无法识别
2012-04-17 11:40 2014在windows7 cmd命令中,可以正常的使用ping命令, ... -
如何windows远程控制mac电脑
2012-03-15 16:54 18837VNC (Virtual Network Computing) ... -
如何禁用xp的自动播放功能
2012-02-25 13:09 836测试环境:xp professional sp3 在网上搜索地 ... -
不能停用网卡
2011-09-15 23:02 1162以下内容摘自电脑报,如有侵权,请告知! 问题描述: 停用 ... -
linux使用wget请求地址时报错
2011-07-26 14:07 2827需求: 在crontab中使用wget请求某个url地址 ... -
虚拟机安装centos no valid devices were found on which to cereate new file systems
2011-06-30 10:25 3293以下文章是我在安装过 ... -
centos 5.5无线上网
2011-05-17 23:22 1763环境: 无线网卡型号 intel wifi link51 ... -
两台电脑局域网内互访注意事项
2011-03-06 15:08 1886此文章转载自电脑报,如有侵权请即时告知 环境: wind ... -
linux修改ip地址及dns
2011-01-25 12:27 1244linux命令修改ip,网关,dns方法 1.命令行的方式: ... -
linux如何运行后台进程跟当前会话无关
2011-01-05 04:57 1463在linux中放了一个jar包,运行 java -classp ... -
vmware卸载脚本
2010-10-13 17:46 1402%%a %%a %%a %%a %%a %%a %%a %%a ...
相关推荐
Linux系统下用Java开发串口通信程序的类库,从Arduino的IDE中扒出来的,比官方的更加好用,Bug少,支持Arduino设备与Linux之间的串口通信;Linux系统下用Java开发串口通信程序的类库,从Arduino的IDE中扒出来的,比...
嵌入式Linux下串口应用开发嵌入式Linux下串口应嵌入式Linux下串口应用开发用开发
linux下串口程序,实现串口回环自发自收,测试串口
对linux下的串口各项设置,以及读,写。
Linux下串口程序开发.doc
Linux下C++串口通信,支持ubuntu,树莓派等系统。
用QT做的LINUX下串口通信(测试可用),控件为一个LABEL,三个按键,具体看程序里面改名字就可以了
linux下串口程序开发,详细介绍开发的具体过程,并附带一个指纹识别程序
linux下串口工具,比minicom方便很多,内附破解文件
linux下串口编程(读写)linux下串口编程(读写)
先在命令行运行make编译,生成串口读写共享库,然后运行gcc -s -shared -fPIC test.c -lSetupSerialPort -o test.so,...还有Linux下Socket通讯开发例程及Firefox浏览器插件开发例程,有空时会一起整理出来贡献给大家
嵌入式Linux下串口应用开发.pdf
Linux串口转网络,linux下socat命令实现网络转发串口数据
RaspberryPi树莓派处理器使用的是ARM架构,而一般电脑则为x86或x64架构,均不能适用,故只能在系统中重新编译安装RXTX源码,此so文件为在arm架构的树莓派编译制作的文件,直接放在jre目录即可运行
基于ARM9和嵌入式Linux的串口驱动开发.pdf
linux下串口通信程序 gcc编译通过,真实可用
linux UART 串口驱动开发文档 linux UART 串口驱动开发文档
Linux下模拟串口工具,参考于http://blog.163.com/bh_binghu/blog/static/94553512010101585533252/ 下载后使用命令:python virtualCom.py即可,会打印出两个虚拟出的串口,需要停止则<Ctrl>+C。
linux UART串口驱动开发文档linux UART串口驱动开发文档