Java中操作串口的步骤与实现

Java中操作串口的步骤与实现


设置串口API:

1   解压缩javacomm20-win32,  
把omm.jar  
放入   Java-home/jre/lib/ext   目录内
Java-home/jre/lib
Java-home/lib
把   javax.comm.properties文件
放入   Java-home/jre/lib/ext   目录内
Java-home/jre/lib
Java-home/lib


2 win32com.dll   文件放入:  
a) java-home/bin
b) java-home/jre/bin


枚举计算机串口代码:

import   java.util.Enumeration;
import   javax.comm.*;      
public   class   comtest
          {
static   Enumeration   en   =   CommPortIdentifier.getPortIdentifiers();  
static   CommPortIdentifier   portId;
          public   static   void   main(String[]   args)
                  {
        while   (en.hasMoreElements())  
                          {
                    portId   =   (CommPortIdentifier)   en.nextElement();
                    if   (portId.getPortType()   ==   CommPortIdentifier.PORT_SERIAL)
                            {
                        System.out.println(portId.getName());  
                            }
                          }
                  }
          }


实现串口方式与RFID设备通信,并获得版本号

import   java.util.Enumeration;
import   javax.comm.*;
import   java.io.*;
     
public   class   comgetver
          {
    static       Enumeration       portList;        
                    static       CommPortIdentifier       portId;        
                    static       SerialPort       serialPort;        
                    static       OutputStream       outputStream;        
                    static       InputStream       inputStream;        


      public   static   void   main(String[]   args)
                  {
                  byte[]   RecData=   new   byte[256];
                 
                  byte[]   getversion=   new   byte[4]; //获得读写器版本号命令
/*组命令包:40   02   02   bc   :读版本号命令*/
getversion[0]=(byte)(0x40);
getversion[1]=(byte)(0x02);
getversion[2]=(byte)(0x02);
getversion[3]=(byte)(0xbc);
                 
                  try        
                                    {        
                                            CommPortIdentifier       commportidentifier       =       CommPortIdentifier.getPortIdentifier("COM1");        
                                            serialPort       =       (SerialPort)commportidentifier.open("smsapp",       3000);      
                                            outputStream       =       serialPort.getOutputStream();        
                                            inputStream       =       serialPort.getInputStream();        
                                            serialPort.setSerialPortParams(57600,        
                                            SerialPort.DATABITS_8,        
                                            SerialPort.STOPBITS_1,        
                                            SerialPort.PARITY_NONE);      
                                            outputStream.write(getversion);   //发送命令
                                               
                                            for(int   i=0;i <100;i++)  
                                            {  
                                            for(int   j=0;j <100;j++){}  
                                            }  
   
                                            outputStream.flush();        
                                            outputStream.close();    
                                           
                                           
                                           
                                            //接受部分
                                            inputStream.read(RecData);
                                            for(int   i=0;i <100;i++)  
                                            {  
                                            for(int   j=0;j <100;j++){}  
                      }                                                                          
                                           
                                            inputStream.close();
                                           
                                            for(int   i=0;i <8;i++)
{
String   hex=Integer.toHexString(RecData[i]   &   0xFF);
//返回的数据转换为16进制数,取低8位

if   (hex.length()==1)
{
hex="0"+hex; //返回的数据若高位位为“0”,则补“0”
}

System.out.print(hex.toUpperCase()   +"   "); //返回的数据转换为大写,并输出

}
System.out.println("");
                                       
                                           
                                    }        
                                    catch(Exception       e)        
                                    {        
                                    System.out.println(e+"");  
                                    }  
                         
                  }
          }


这是我设置串口,及实现的代码,自己写玩的,里面的命令是针对我们设备的,针对自己的设备做修改即可! 
 
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值