⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serialmain.java

📁 用java实现串口通信 用java实现串口通信
💻 JAVA
字号:
import communication.*;
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 SerialMain
{
	public static void main(String[] args)
	{
		Crc cr=new Crc();
		SerialBean SB = new SerialBean(1);
		SB.Initialize();
		String Msg="";
		String[] effect_Address=new String[10];//存放有效地址,即该地址有模块,假设只有10个模块
		int i_Address=0;
	    double TIME=0;

		//搜索模块
		/*************************/
		String[] order_Search=new String[256];
		for(int i=0;i<16;++i)
			order_Search[i]="$"+"0"+Integer.toHexString(i).toUpperCase()+"M";//地址0~15转换为地址00~0F
		for(int i=16;i<256;++i)
			order_Search[i]="$"+Integer.toHexString(i).toUpperCase()+"M";//地址16~255转换为地址10~FF
		for(int i=0;i<256;++i)
			order_Search[i]=cr.toCrc(order_Search[i]);
		for (int i=0;i<256;++i)
		{
			//System.out.println(order_Search[i+1]);
			SB.WritePort(order_Search[i]+System.getProperty("line.separator"));
			Msg=SB.ReadSearch();
			//搜索到模块则显示---Msg--!AA(Data)[CHK](cr)
			if(Msg.length()!=0)
			{	
				effect_Address[i_Address++]=Msg.substring(1,3);
				System.out.println("Msg: "+Msg);
			}
		}
		/*for(i_Address=0;i_Address<10;++i_Address)
			System.out.println(effect_Address[i_Address]);*/
			/**************************/



		
		//读模块信息,如BaudRate,Address
		/************************
		String[] order_Info=new String[10];
		for(int i_Address=0;i_Address<10;++i_Address)
			order_Info[i_Address]="$"+effect_Address[i_Address++]+"2";
		for(int i_Address=0;i_Address<10;++i_Address)
			order_Info[i_Address]=cr.toCrc(order_Info[i_Address]);
		for (int i_Address=0;i_Address<10;++i_Address)
		{
			SB.WritePort(order_Info[i_Address]);
			Msg=SB.ReadPort();
			if(Msg.charAt(0)=='!')//查询到信息则显示---'!'有效命令头字符
				System.out.println(Msg);
		}
		***************************/



		//信息采集
		//SB.WritePort("#**77"+System.getProperty("line.separator"));//加入硬回车
		//SB.WritePort("$024BA"+System.getProperty("line.separator"));
		//Msg=SB.ReadPort();
		//if(Msg.charAt(0)=='!')//查询到信息则显示---'!'有效命令头字符
		//System.out.println(Msg);


		SB.ClosePort();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -