佚名通过本文主要向大家介绍了javarxtx,c#获取串口数据,获取串口数据,js获取串口数据,matlab获取串口数据等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
问题: java rxtx获取不到串口数据
描述:
import java.io.*;
import java.util.*;
import gnu.io.*;
import java.awt.*;
import java.util.Properties;
import javax.swing.*;
import java.awt.event.*;
public class com extends JFrame implements ActionListener,Runnable,SerialPortEventListener
{
static Enumeration portList;
static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static CommDriver driver=null;
static Properties props = new Properties();
static boolean use=false;
Thread readThread;
JScrollPane p1;
JTextField Text=new JTextField(10);
JButton ok=new JButton("确定");
JTextArea area=new JTextArea(15,10);
JLabel l1=new JLabel("Message:");
com()
{
setTitle("Com");
setVisible(true);
setLayout(null);
p1=new JScrollPane(area);
add(l1);l1.setBounds(35,30,100,25);
add(Text);Text.setBounds(120,30,150,25);
add(ok);ok.setBounds(290,30,80,23);
add(p1);p1.setBounds(35,70,340,250);
ok.addActionListener(this);
setSize(400,400);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
setVisible(true);
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width-400)/2,(screen.height-400)/2);
addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ok)
{
String message1=Text.getText()+"\r";
byte[] message = message1.getBytes();
boolean portFound=false;
String defaultPort="COM3";
portList=CommPortIdentifier.getPortIdentifiers();
System.out.println(portList.hasMoreElements());
while(portList.hasMoreElements())
{
portId=(CommPortIdentifier)portList.nextElement();
System.out.println("portId:"+portId.getName());
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL)
{
if(portId.getName().equals(defaultPort))
{
System.out.println("OK:1");
portFound=true;
if(!use)
{
try
{
serialPort=(SerialPort)portId.open("ReadCommApp",2000);
serialPort.setRTS(false);
System.out.println("OK:2");
}
catch(PortInUseException a){JOptionPane.showMessageDialog(null,"端口正在使用");continue;}
}
try
{
outputStream=serialPort.getOutputStream();
System.out.println("OK:3");
}
catch(IOException a){}
try
{
serialPort.notifyOnOutputEmpty(true);
System.out.println("OK:4");
}
catch(Exception a){}
try
{
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
System.out.println("OK:5");
}
catch(UnsupportedCommOperationException a){}
try
{
outputStream.write(message);//.getBytes());
//outputStream.write("AT\r".getBytes());
area.append("\nsend to port is sucess: "+message+"***");
use=true;
}
catch(IOException a){}
}
}
}
if(!portFound)
{
JOptionPane.showMessageDialog(null,"can't find the port");
}
try
{
inputStream=serialPort.getInputStream();
}
catch(IOException a){}
try
{
serialPort.addEventListener(this);
}
catch(TooManyListenersException a){}
serialPort.notifyOnDataAvailable(true);
readThread=new Thread(this);
readThread.start();
}
}
public void run()
{
try
{
Thread.sleep(2000);
}
&nb
描述:
javarxtxrs-232空获取不到
我用了一条rs-232线,把我的pc机和单片机连接起来。然后用了STC里面自带的串口助手,向串口发送数据,但是pc机接受不到。后来我又专门下载了一个串口助手,发送字符串,还是接受不到。哪位大神帮我看下,是我的操作有问题,还是pc机获取串口数据的代码有问题。import java.io.*;
import java.util.*;
import gnu.io.*;
import java.awt.*;
import java.util.Properties;
import javax.swing.*;
import java.awt.event.*;
public class com extends JFrame implements ActionListener,Runnable,SerialPortEventListener
{
static Enumeration portList;
static CommPortIdentifier portId;
static SerialPort serialPort;
static OutputStream outputStream;
static InputStream inputStream;
static CommDriver driver=null;
static Properties props = new Properties();
static boolean use=false;
Thread readThread;
JScrollPane p1;
JTextField Text=new JTextField(10);
JButton ok=new JButton("确定");
JTextArea area=new JTextArea(15,10);
JLabel l1=new JLabel("Message:");
com()
{
setTitle("Com");
setVisible(true);
setLayout(null);
p1=new JScrollPane(area);
add(l1);l1.setBounds(35,30,100,25);
add(Text);Text.setBounds(120,30,150,25);
add(ok);ok.setBounds(290,30,80,23);
add(p1);p1.setBounds(35,70,340,250);
ok.addActionListener(this);
setSize(400,400);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setResizable(false);
setVisible(true);
Dimension screen=Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screen.width-400)/2,(screen.height-400)/2);
addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
}
);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==ok)
{
String message1=Text.getText()+"\r";
byte[] message = message1.getBytes();
boolean portFound=false;
String defaultPort="COM3";
portList=CommPortIdentifier.getPortIdentifiers();
System.out.println(portList.hasMoreElements());
while(portList.hasMoreElements())
{
portId=(CommPortIdentifier)portList.nextElement();
System.out.println("portId:"+portId.getName());
if(portId.getPortType()==CommPortIdentifier.PORT_SERIAL)
{
if(portId.getName().equals(defaultPort))
{
System.out.println("OK:1");
portFound=true;
if(!use)
{
try
{
serialPort=(SerialPort)portId.open("ReadCommApp",2000);
serialPort.setRTS(false);
System.out.println("OK:2");
}
catch(PortInUseException a){JOptionPane.showMessageDialog(null,"端口正在使用");continue;}
}
try
{
outputStream=serialPort.getOutputStream();
System.out.println("OK:3");
}
catch(IOException a){}
try
{
serialPort.notifyOnOutputEmpty(true);
System.out.println("OK:4");
}
catch(Exception a){}
try
{
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
System.out.println("OK:5");
}
catch(UnsupportedCommOperationException a){}
try
{
outputStream.write(message);//.getBytes());
//outputStream.write("AT\r".getBytes());
area.append("\nsend to port is sucess: "+message+"***");
use=true;
}
catch(IOException a){}
}
}
}
if(!portFound)
{
JOptionPane.showMessageDialog(null,"can't find the port");
}
try
{
inputStream=serialPort.getInputStream();
}
catch(IOException a){}
try
{
serialPort.addEventListener(this);
}
catch(TooManyListenersException a){}
serialPort.notifyOnDataAvailable(true);
readThread=new Thread(this);
readThread.start();
}
}
public void run()
{
try
{
Thread.sleep(2000);
}
&nb