「java串行字符」java语言字符串
本篇文章给大家谈谈java串行字符,以及java语言字符串对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、利用JAVA实现串行通信的优点
- 2、Java串行化怎么理解?什么是串行化?谁能通俗地给我讲讲?
- 3、用java绘制DNA字符串输入一个整数且为奇数,表示DNA的串行,再输入一个整数表示?
- 4、java,什么叫串行化?
- 5、java中的序列化和串行化的区别
利用JAVA实现串行通信的优点
利用Java实现串口全双工通讯
内容:
1. SerialBean
2. SerialBuffer
3. ReadSerial
4. SerialExample
一个嵌入式系统通常需要通过串口与其主控系统进行全双工通讯,譬如一个流水线控制系统需要不断的接受从主控系统发送来的查询和控制信息,并将执行结果或查询结果发送回主控系统。本文介绍了一个简单的通过串口实现全双工通讯的Java类库,该类库大大的简化了对串口进行操作的过程。
本类库主要包括:SerialBean.java (与其他应用程序的接口), SerialBuffer.java (用来保存从串口所接收数据的缓冲区), ReadSerial.java (从串口读取数据的程序)。另外本类库还提供了一个例程SerialExample.java 作为示范。在下面的内容中将逐一对这几个部分进行详细介绍。
1. SerialBean
SerialBean是本类库与其他应用程序的接口。该类库中定义了SerialBean的构造方法以及初始化串口,从串口读取数据,往串口写入数据以及关闭串口的函数。具体介绍如下:
public SerialBean(int PortID)
本函数构造一个指向特定串口的SerialBean,该串口由参数PortID所指定。PortID = 1 表示COM1,PortID = 2 表示COM2,由此类推。
public int Initialize()
本函数初始化所指定的串口并返回初始化结果。如果初始化成功返回1,否则返回-1。初始化的结果是该串口被SerialBean独占性使用,其参数被设置为9600, N, 8, 1。如果串口被成功初始化,则打开一个进程读取从串口传入的数据并将其保存在缓冲区中。
public String ReadPort(int Length)
本函数从串口(缓冲区)中读取指定长度的一个字符串。参数Length指定所返回字符串的长度。
public void WritePort(String Msg)
本函数向串口发送一个字符串。参数Msg是需要发送的字符串。
public void ClosePort()
本函数停止串口检测进程并关闭串口。
SerialBean的源代码如下:
package serial;
import java.io.*;
import java.util.*;
import javax.comm.*;
/**
*
* This bean provides some basic functions to implement full dulplex
* information exchange through the srial port.
*
*/
public class SerialBean
{
static String PortName;
CommPortIdentifier portId;
SerialPort serialPort;
static OutputStream out;
static InputStream in;
SerialBuffer SB;
ReadSerial RT;
/**
*
* Constructor
*
* @param PortID the ID of the serial to be used. 1 for COM1,
* 2 for COM2, etc.
*
*/
public SerialBean(int PortID)
{
PortName = "COM" + PortID;
}
/**
*
* This function initialize the serial port for communication. It startss a
* thread which consistently monitors the serial port. Any signal capturred
* from the serial port is stored into a buffer area.
*
*/
public int Initialize()
{
int InitSuccess = 1;
int InitFail = -1;
try
{
portId = CommPortIdentifier.getPortIdentifier(PortName);
try
{
serialPort = (SerialPort)
portId.open("Serial_Communication", 2000);
} catch (PortInUseException e)
{
return InitFail;
}
//Use InputStream in to read from the serial port, and OutputStream
//out to write to the serial port.
try
{
in = serialPort.getInputStream();
out = serialPort.getOutputStream();
} catch (IOException e)
{
return InitFail;
}
//Initialize the communication parameters to 9600, 8, 1, none.
try
{
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e)
{
return InitFail;
}
} catch (NoSuchPortException e)
{
return InitFail;
}
// when successfully open the serial port, create a new serial buffer,
// then create a thread that consistently accepts incoming signals from
// the serial port. Incoming signals are stored in the serial buffer.
SB = new SerialBuffer();
RT = new ReadSerial(SB, in);
RT.start();
// return success information
return InitSuccess;
}
/**
*
* This function returns a string with a certain length from the incomin
* messages.
*
* @param Length The length of the string to be returned.
*
*/
public String ReadPort(int Length)
{
String Msg;
Msg = SB.GetMsg(Length);
return Msg;
}
/**
*
* This function sends a message through the serial port.
*
* @param Msg The string to be sent.
*
*/
public void WritePort(String Msg)
{
int c;
try
{
for (int i = 0; i Msg.length(); i++)
out.write(Msg.charAt(i));
} catch (IOException e) {}
}
/**
*
* This function closes the serial port in use.
*
*/
public void ClosePort()
{
RT.stop();
serialPort.close();
}
}
2. SerialBuffer
SerialBuffer是本类库中所定义的串口缓冲区,它定义了往该缓冲区中写入数据和从该缓冲区中读取数据所需要的函数。
public synchronized String GetMsg(int Length)
本函数从串口(缓冲区)中读取指定长度的一个字符串。参数Length指定所返回字符串的长度。
public synchronized void PutChar(int c)
本函数望串口缓冲区中写入一个字符,参数c 是需要写入的字符。
在往缓冲区写入数据或者是从缓冲区读取数据的时候,必须保证数据的同步,因此GetMsg和PutChar函数均被声明为synchronized并在具体实现中采取措施实现的数据的同步。
SerialBuffer的源代码如下:
package serial;
/**
*
* This class implements the buffer area to store incoming data from the serial
* port.
*
*/
public class SerialBuffer
{
private String Content = "";
private String CurrentMsg, TempContent;
private boolean available = false;
private int LengthNeeded = 1;
/**
*
* This function returns a string with a certain length from the incomin
* messages.
*
* @param Length The length of the string to be returned.
*
*/
public synchronized String GetMsg(int Length)
{
LengthNeeded = Length;
notifyAll();
if (LengthNeeded Content.length())
{
available = false;
while (available == false)
{
try
{
wait();
} catch (InterruptedException e) { }
}
}
CurrentMsg = Content.substring(0, LengthNeeded);
TempContent = Content.substring(LengthNeeded);
Content = TempContent;
LengthNeeded = 1;
notifyAll();
return CurrentMsg;
}
/**
*
* This function stores a character captured from the serial port to the
* buffer area.
*
* @param t The char value of the character to be stored.
*
*/
public synchronized void PutChar(int c)
{
Character d = new Character((char) c);
Content = Content.concat(d.toString());
if (LengthNeeded Content.length())
{
available = true;
}
notifyAll();
}
}
3. ReadSerial
ReadSerial是一个进程,它不断的从指定的串口读取数据并将其存放到缓冲区中。
public ReadSerial(SerialBuffer SB, InputStream Port)
本函数构造一个ReadSerial进程,参数SB指定存放传入数据的缓冲区,参数Port指定从串口所接收的数据流。
public void run()
ReadSerial进程的主函数,它不断的从指定的串口读取数据并将其存放到缓冲区中。
ReadSerial的源代码如下:
package serial;
import java.io.*;
/**
*
* This class reads message from the specific serial port and save
* the message to the serial buffer.
*
*/
public class ReadSerial extends Thread
{
private SerialBuffer ComBuffer;
private InputStream ComPort;
/**
*
* Constructor
*
* @param SB The buffer to save the incoming messages.
* @param Port The InputStream from the specific serial port.
*
*/
public ReadSerial(SerialBuffer SB, InputStream Port)
{
ComBuffer = SB;
ComPort = Port;
}
public void run()
{
int c;
try
{
while (true)
{
c = ComPort.read();
ComBuffer.PutChar(c);
}
} catch (IOException e) {}
}
}
4. SerialExample
SerialExample是本类库所提供的一个例程。它所实现的功能是打开串口COM1,对其进行初始化,从串口读取信息对其进行处理后将处理结果发送到串口。
import serial.*;
import java.io.*;
/**
*
* This is an example of how to use the SerialBean. It opens COM1 and reads
* six messages with different length form the serial port.
*
*/
class SerialExample
{
public static void main(String[] args)
{
//TO DO: Add your JAVA codes here
SerialBean SB = new SerialBean(1);
String Msg;
SB.Initialize();
for (int i = 5; i = 10; i++)
{
Msg = SB.ReadPort(i);
SB.WritePort("Reply: " + Msg);
}
SB.ClosePort();
}
}
5. 编译与调试
本类库中使用了Java Communication API (javax.comm)。这是一个Java扩展类库,并不包括在标准的Java SDK当中。如果你尚未安装这个扩展类库的话,你应该从Sun公司的Java站点下载这个类库并将其安装在你的系统上。在所下载的包里面包括一个安装说明,如果你没有正确安装这个类库及其运行环境的话,运行这个程序的时候你会找不到串口。
正确安装Java Communication API并将上述程序编译通过以后,你可以按如下方法测试这个程序。如果你只有一台机器,你可以利用一条RS-232电缆将COM1和COM2连接起来,在COM1上运行SerialExample,在COM2上运行Windows提供的超级终端程序。如果你有两台机器的话,你可以利用一条RS-232电缆将两台机器的COM1(或者是COM2)连接起来,在一端运行例程,另外一端运行Windows提供的超级终端程序。如果有必要的话,可以对SerialExample中所声明的串口进行相应改动。
本程序在Windows 2000 + Java SDK 1.3环境下编译通过并成功运行。
Java串行化怎么理解?什么是串行化?谁能通俗地给我讲讲?
首先,这个概念的原文是 Serialization,而串行化这个翻译并不是很好,个人倾向于序列化这个翻译,下面我都会用序列化这个名词。
所谓序列化是指把一个对象通过某种规则转化为一串二进制串,字符串就是一种二进制串。但为何要把对象转化为二进制串呢?因为我们需要保存或者在网络上传输它们,而存在于 JVM 内存中的对象并没有使用者可见的二进制形式。虽然内存中的所有东西仍然是二进制的,但 JVM 向我们屏蔽了内存操作相关的信息,我们不一定能确定某个 JVM 实现是如何在内存中存储和组织一个 Java 对象的内容的(C/C++ 就可以直接获取内存块来作为序列化的二进制串)。
当然,光序列化是不够的,我们还需要反序列化,也就是如何从二进制串重新转回对象。这样当我们从文件中读取或者在网络的另一头收到某个对象的二进制串之后,我们才能重新还原回那个对象。
Java 默认实现了自己的序列化,就是使用的内存数据。然而除了 Java 自己的序列化,我们还有很多中序列化方式,例如 hessian。或者说将 Java 对象转成 json、xml 也是一种序列化。
举一个非常简单的例子,例如我们有一个对象 Integer v = 1;。当我们使用 hessian 对其序列化的时候,我们可能会拿到 I1 这样的字串(并不确定 hessian 生成的串是不是真的是这样,但是也差不多),其中 i 表示类型是 Integer,而 1 就是这个变量的值,而 I1 就是序列化后的二进制串(一个字符串)。
用java绘制DNA字符串输入一个整数且为奇数,表示DNA的串行,再输入一个整数表示?
代码如下:
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
if (n % 2 == 0) {
if (n % 4 == 0) {
System.out.println("偶数,可以被4整除。");
} else {
System.out.println("偶数,不可以被4整除。");
}
} else {
if (n % 5 == 0) {
System.out.println("奇数,可以被5整除。");
} else {
System.out.println("奇数,不可以被5整除。");
}
}
}
}
java,什么叫串行化?
把一个对象变成01串,然后在需要的地方重新恢复成对象。比如可以将对象保存在文件,然后再读出。可以将一个对象通过网络传输。
java中的序列化和串行化的区别
假设你想保存一个或多个对象的状态。如果Java不具备串行化能力,就不得不使用某个I/O类写出你想保存的所有对象的实例变量的状态。
串行化简单地说就是“保存这个对象及其所有实例变量Transient,这意味,不会把瞬态变量的值作为对象的串行化状态的一部分包括进来。”。实际上,更有趣的是,显式地将一个变量标识为
将要实现序列化的类必须实现Serializable接口或者是Externalizable接口。
在Serializable接口中没有定义任何方法,而在Externalizable接口中需要实现
public void writeExternal(ObjectOutput out) throws IOException{}以及
public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException{}抽象方法。
一.实现Serializable接口的序列化
package demo;
import java.io.Serializable;
public class Person implements Serializable
{
private String name;
private char sex;
private int age;
public Person(String name,char sex,int age)
{
this.name=name;
this.sex=sex;
this.age=age;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public char getSex()
{
return sex;
}
public void setSex(char sex)
{
this.sex = sex;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String toString()
{
return getName()+", "+getSex()+", "+getAge();
}
}
关于java串行字符和java语言字符串的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-14,除非注明,否则均为
原创文章,转载请注明出处。