Tag: 串口

如何从JAVA中的GSM调制解调器读取传入消息

package pack1; import java.io.*; import java.util.*; import javax.comm.*; public class Java_SerialCommTest1 implements Runnable, SerialPortEventListener { public void run() {} static Enumeration portList; static CommPortIdentifier portId; static String messageString = “My Message to be sent”; static char ch = ‘”‘; static String dest = “*********”; // 10 Digit Mobile Number. static InputStream inputStream; static SerialPort serialPort; […]

从Java字节中提取半字节

Hex:[0A][52][08][01][01][01][00][CD][21][02][59] 0 [0A] 1 [52] Packettype = TEMP_HUM 2 [08] subtype = TH8 – 3 [01] Sequence nbr = 1 4/5 [01][01] ID = 257 6/7 [00][CD] Temperature = 20.5 °C 8 [21] Humidity = 33 9 [02] Status = Dry 10 [5] *nibble Signal level = 5 11 [9] *nibble Battery = OK 所以我通过串口获得了11个字节(Hex)。 […]

如何通过CommPortIdentifier查找COM端口

我是整个modbus和串行通信概念的新手,所以即使这是一个真正的菜鸟问题,请耐心等待! 好吧我正在尝试使用modbus协议和RS 232端口读取存储在寄存器中的值。 我写了这段代码,但它找不到串口”COM 4″ 。 我究竟做错了什么? String wantedPortName = “COM 4” ; Enumeration portIdentifiers = CommPortIdentifier.getPortIdentifiers(); CommPortIdentifier portId = null; while (portIdentifiers.hasMoreElements()) { CommPortIdentifier pid = (CommPortIdentifier) portIdentifiers.nextElement(); if (pid.getPortType() == CommPortIdentifier.PORT_SERIAL && pid.getName().equals(wantedPortName)) { portId = pid; break; } } if (portId == null) { System.err.println(“Could not find serial port ” + wantedPortName); […]

Arduino无法发回串行数据

所以我发现了如何将Arduino连接到我的java程序。 但是使用串行连接不会返回任何有用的数据,它的格式错误,或者只是将其作为一个盒子发送。 我已经看过这里早期发布的相关问题,但没有一个提示似乎有所帮助。 那么有谁知道如何使用串口在Arduino和计算机之间发送数据? 这是我正在使用的代码,由此人提供: http : //silveiraneto.net/2009/03/01/arduino-and-java/ package serialtalk; import gnu.io.CommPortIdentifier; import gnu.io.SerialPort; import java.io.InputStream; import java.io.OutputStream; import processing.app.Preferences; public class Main { static InputStream input; static OutputStream output; public static void main(String[] args) throws Exception{ Preferences.init(); System.out.println(“Using port: ” + Preferences.get(“serial.port”)); CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier( Preferences.get(“serial.port”)); SerialPort port = (SerialPort)portId.open(“serial talk”, 4000); input […]

从Java中的串口读取文件

我是java技术的初学者,我必须从端口读取文件。 首先,我将“FLASH”写入输出流,然后我将从目标设备获得响应作为“FLASH_OK”,在获得FLASH_OK作为响应后再次我必须写出我想要的文件名,但问题是它不写文件名为outputstream,下面是我的代码。 请帮帮我。 package writeToPort; import java.awt.Toolkit; import java.io.*; import java.util.*; import javax.comm.*; import javax.swing.JOptionPane; import constants.Constants; public class Flashwriter implements SerialPortEventListener { Enumeration portList; CommPortIdentifier portId; String messageString = “\r\nFLASH\r\n”; SerialPort serialPort; OutputStream outputStream; InputStream inputStream; Thread readThread; String one, two; String test = “ONLINE”; String[] dispArray = new String[1]; int i = 0; […]

Java串口写/发送ASCII数据

我的问题是我需要通过蓝牙在Java中控制移动机器人E-puck,通过发送命令如“D,100,100”来设置速度,“E”来获得速度等等。我有一些代码: String command = “D,100,100”; OutputStream mOutputToPort = serialPort.getOutputStream(); mOutputToPort.write(command.getBytes()); 所以用这种方法write我只能发送byte[]数据,但我的机器人不会理解。 例如,之前我一直在Matlab上使用这样的命令: s = serial(‘COM45′); fopen(s); fprintf(s,’D,100,100′,’async’); 或仅限程序Putty类型: D,100,100 `enter` 附加信息: 我也想通了,Matlab有另一个解决方案。 s = serial(‘COM45’); fopen(s); data=[typecast(int8(‘-D’),’int8′) typecast(int16(500),’int8′) typecast(int16(500),’int8′)]; 在这种情况下: data = [ -68 -12 1 -12 1]; fwrite(s,data,’int8′,’async’); 在Java中不一样吗: byte data[] = new byte[5]; data[0] = -‘D’; data[1] = (byte)(500 & 0xFF); data[2] = […]

如何在不关闭连接的情况下更改波特率?

我想连接到一个连接到串口(COM4)的设备,初始波特率为300,设备可以用命令将其波特率改为9600,我的Java代码将此命令发送到设备并更改设备的波特率,但我不知道如何在不关闭连接的情况下更改程序中的波特率。 连接关闭后,设备将恢复到初始波特率。 在连接打开时有没有办法改变Java中的波特率? 我将“更改波特率”命令发送到设备后,当下面的代码执行设备丢失了连接。 我认为这种方法只是用于初始化,而不是用于改变通信中间的波特率。 port.setSerialPortParams( 9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

当端口存在时,为什么我会收到Java NoSuchPortException?

下面有以下生产代码,我正在使用它作为新驱动程序。 portName是COM4,这个端口存在于PC上(我可以通过超级终端连接到它),那么为什么Javacomm会抛出NoSuchPortException呢? COM4在设备mgr中显示正常。 太 final String portName = getSerialPort(); try { final CommPortIdentifier id = CommPortIdentifier.getPortIdentifier(portName); port = (SerialPort) id.open(getName(), 1000); } catch (NoSuchPortException nspe) { report(SeverityCode.LEVEL2, getName(), “PIN Pad is not connected to ” + portName + ” port, or the port does not exist.”); return; } catch (PortInUseException piue) { report(SeverityCode.LEVEL2, getName(), portName […]

RXTX串行连接 – 阻塞read()的问题

我试图使用RXTX库来阻止Windows上的串行通信(XP和7)。 我已经在两端测试了与Hyperterminal的连接,并且它完美无缺。 我使用以下代码设置连接:(为清楚起见,省略了exception处理和防御性检查) private InputStream inStream; private OutputStream outStream; private BufferedReader inReader; private PrintWriter outWriter; private SerialPort serialPort; private final String serialPortName; public StreamComSerial(String serialPortName) { this.serialPortName = serialPortName; CommPortIdentifier portIdentifier; portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName); CommPort commPort = null; commPort = portIdentifier.open(this.getClass().getName(),500); serialPort = (SerialPort) commPort; serialPort.setSerialPortParams(4800,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE); inStream = serialPort.getInputStream(); outStream = serialPort.getOutputStream(); inReader = new […]

Android蓝牙 – 无法连接

我正在开发一个使用蓝牙连接到设备并发送/接收数据的应用程序。 我正在使用Nexus One手机进行所有测试。 我从来没有能够建立从手机到任何设备的SPP(串行端口)连接。 但是,我已经能够使用相当于PuTTY的Mac从设备(我的笔记本电脑)连接到我的手机(唯一的例外是来自Marketplace的“蓝牙文件传输”应用程序似乎可行,但我不认为使用RFCOM / SPP ……)。 我在LogCat日志中看到这条消息: ERROR/BluetoothService.cpp(78): stopDiscoveryNative: D-Bus error in StopDiscovery: org.bluez.Error.Failed (Invalid discovery session) 以及这些: java.io.IOException: Operation Canceled java.io.IOException: Software caused connection abort 我尝试使用UUID“00001101-0000-1000-8000-00805F9B34FB”,我也试过使用: Method m = device.getClass().getMethod(“createRfcommSocket”, new Class[] { int.class }); sock = (BluetoothSocket) m.invoke(device, Integer.valueOf(1)); 方法而不是device.createRfcommSocketToServiceRecord(UUID); 同样 – 没有运气。 我正在使用BluetoothChat示例和该代码的变体来完成我的所有测试… 解决方案或建议会很棒…或者甚至是我可以在手机上运行的一些测试代码的更好/更简单的例子,或者我可以在计算机上运行以帮助调试的python脚本或其他东西? 谢谢! 我希望这不是Android操作系统的错误,但如果是,我希望找到一个解决方法。 编辑:我还应该注意,大多数设备在蓝牙设置中显示为“已配对但未连接”。 编辑2:解决方案似乎只是禁用任何蓝牙监听。 有关更多信息,请参阅我的回答帖