Flink combined with HDFS

2023-01-20   ES  

@socket to communicate 485 to the network

This is a method of communicating hardware through sockets, grab data from hardware to input, and transmits real -time data. Put the code directly when I write a blog.

static string responsemsg = ""; 
 private string sendandRes 
     try { 
         // Create the client's DataGramsocket object, no need to pass the address and object 
         client = new datagramsocket (); 
         byte [] sendbytes = hexstrtobytes (msg.replaceall ("", ""); 
         System.out.println (); 
         // Package to send the address of the target 
         Indadress address = Inetaddress.getByname (IP); 
         // The object of the DataGrampacket to be sent to the packaging, because it is to be sent to the destination host, the address and port number should be added 
         DataGrampacket SendPacket = New DataGrampacket (Sendbytes, Sendbytes.length, Address, Port); 

         try { 
             //send data 
             client.send (SendPacket); 
         } Catch (Exception E) { 
             e.printstacktrace (); 
         } 
         System.out.println ("Data sending successful:" + msg); 
        
         byte [] responsebytes = new byte [1024]; 
         // Create the DataGrampacket object of the response information 
         DataGrampacket ResponsePacket = New DataGrampacket (Responsebytes, 1024); 
         try { 
             // Waiting for the response information. Like the server, the client will also block this step until I receive a data packet 
             client.Receive (responSepacket); 
             //Client.send (ResponsePacket); 
         } Catch (Exception E) { 
             e.printstacktrace (); 
         } 
         System.out.println ("Successful data reading:" + msg); 

         // Analysis of the content of the packet 
         responsemsg = bytestohexstring (responsepacket.getdata (), responsePacket.getlength ()); 
     } Catch (Exception E) { 
         e.printstacktrace (); 
     } Finally { 
         // Close the client 
         if (client! = null) { 
             client.close (); 
             client = null; 
         }

Because the sensor on my side only receives hexadecimal, I want to turn it

 private byte[] hexStrToBytes(String hexStr) {
   if(null == hexStr || "".equals(hexStr)) {
  return null;
 }
 if(0 != hexStr.length() % 2) {
  hexStr = "0" + hexStr;
 }
 char[] chars = hexStr.toCharArray();
 int len = chars.length/2;
 byte[] bytes = new byte[len];
 for (int i = 0; i < len; i++) {
  int x = i * 2;
  bytes[i] = (byte)Integer.parseInt(String.valueOf(new char[]{chars[x], chars[x+1]}), 16);
  
 }
 return bytes;
}
private String bytesToHexString(byte[] bytes, int length) {
 if(null == bytes || bytes.length < length) {
  return null;
 }
 StringBuffer sb = new StringBuffer();
 for (int i = 0; i < length; i++) {
  String strHex = Integer.toHexString(bytes[i]);
  if(strHex.length() > 3) {
   sb.append(strHex.substring(6));
  } else {
   if(strHex.length() < 2) {
    sb.append("0" + strHex);
   } else {
    sb.append(strHex);
   }
  }
 }
 return sb.toString();
}

Because I need to send a instruction to the sensor, the sensor will return the data
string msg = “E0 05 07 02 4B” ;, this instruction is given by the merchant

udpclient client = new udpclient (); 
     String msg = "E0 05 07 02 4B"; 
     System.out.println ("Send command:" + msg); 
     String sendandReceive = "as"; 
     SendandReceive = Client.sendandReceive ("192.168.0.189", 6000, msg); 
     System.out.println (SendandReceive); 
   
     //humidity 
     String humidity = responsemsg.substring (6,10); 
     Biginteger bigint = New Biginteger (Humidity, 16); 
     int intvalue = bigint.intvalue (); 
     System.out.println ("humidity:"+intvalue/10.0+"%rh"); 
    
    
     //temperature 
     String temporator = responsemsg.substring (10,14); 
     Biginteger bigint1 = New Biginteger (temporarative, 16); 
     int intvalue1 = bigint1.intvalue (); 
     System.out.println ("Temperature:"+Intvalue1/10.0+"℃");}

I also need to start learning this recently, so I will share it.

source

Related Posts

最大流EK算法模版-POJ1273

Uniapp subtitles 3 seconds automatically broadcast downward (dynamic subtitles, news announcement) DYC

ANT component tool | Configuration information | batch processing command | cloning

Chapter 1 Principles of Computer Storage Information

Flink combined with HDFS

Random Posts

Eclipse JSP page in the web project created in the web project report an error

The BIOS setting hard disk of each brand notebook is compatible mode (IDE) method (transfer)

Spring-IOC container-Keke Teacher

HSSFWORKBOOK export data to Excel file

最大流EK算法模版-POJ1273