最近在做能过JAVA 和传感器交流..网上的串口代码基本上都差不多. 找了一个现成的. 现在遇到的问题是:
1.往传感器里写入数据时. 传感器灯微亮(微亮可能出现的问题是:命令错误,. 可是我用串口软件用这个命令测试的时候是正确的)
2.写入命令: messageString.getBytes() 是不是直接转成Byte数据组发磅
在这里先谢谢各位回贴的大哥啦!传感器的协议是MODBUS RTU 协议.//读串口的例程
import java.io.*;
import java.util.*;
import javax.comm.*;public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier portId;
static Enumeration portList;//枚举类
static String messageString = "01 03 00 10 00 0A C4 08";
static OutputStream outputStream; InputStream inputStream;
SerialPort serialPort;
Thread readThread; public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
//if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
}
}
}
}
public static String turnStringToHex(String input) {
byte[] bytes = null;
String s = " ";
try {
bytes = input.getBytes("iso-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
for(int i = 0; i < bytes.length; i++) {
s += Integer.toHexString(bytes[i]);
}
System.out.println(s.trim());
return s.trim();
}
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {} serialPort.notifyOnDataAvailable(true); try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {} try {
outputStream = serialPort.getOutputStream();
outputStream.write(messageString.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
readThread = new Thread(this);
readThread.start();
} public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
//串口事件
public void serialEvent(SerialPortEvent event) { switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break; case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20]; try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.println("ddd"+new String(readBuffer));
} catch (IOException e) {}
break;
}
}
}
1.往传感器里写入数据时. 传感器灯微亮(微亮可能出现的问题是:命令错误,. 可是我用串口软件用这个命令测试的时候是正确的)
2.写入命令: messageString.getBytes() 是不是直接转成Byte数据组发磅
在这里先谢谢各位回贴的大哥啦!传感器的协议是MODBUS RTU 协议.//读串口的例程
import java.io.*;
import java.util.*;
import javax.comm.*;public class SimpleRead implements Runnable, SerialPortEventListener { static CommPortIdentifier portId;
static Enumeration portList;//枚举类
static String messageString = "01 03 00 10 00 0A C4 08";
static OutputStream outputStream; InputStream inputStream;
SerialPort serialPort;
Thread readThread; public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
//if (portId.getName().equals("/dev/term/a")) {
SimpleRead reader = new SimpleRead();
}
}
}
}
public static String turnStringToHex(String input) {
byte[] bytes = null;
String s = " ";
try {
bytes = input.getBytes("iso-8859-1");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
for(int i = 0; i < bytes.length; i++) {
s += Integer.toHexString(bytes[i]);
}
System.out.println(s.trim());
return s.trim();
}
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {} serialPort.notifyOnDataAvailable(true); try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {} try {
outputStream = serialPort.getOutputStream();
outputStream.write(messageString.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
readThread = new Thread(this);
readThread.start();
} public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
//串口事件
public void serialEvent(SerialPortEvent event) { switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break; case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20]; try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
System.out.println("ddd"+new String(readBuffer));
} catch (IOException e) {}
break;
}
}
}
http://topic.csdn.net/u/20100611/20/8ceab9f5-68b1-4f6c-aec7-05280ae5f190.html
2:确定端口号、波特率等设置与你的串口软件一样。看下我的博客:
http://blog.csdn.net/config_man/archive/2010/06/30/5705264.aspx
如果没有中文的话,是可以的。
messageString = "01 03 00 10 00 0A C4 08";
要发送字节数据,应该定义成
Byte[] bs={0x01, 0x03, 0x00, 0x10, 0x00, (byte)0x0A, (byte)0xC4, 0x08};
这样发送才对
你用messageString.getBytes()输出的组成该字符串的字节,你用System.out.Println()打印一下就知道了。
1号: 82085373
2号: 55191814
3号: 89238748
4号: 56418364
5号: 157181129
6号: 74671714请勿重复加群......