RXTX Listner is one of Infairy broadcast channel which connect the COMM port and bundle.
Developer can listen RXTX broadcast channel to get data packet from who sending by implement "EventRXTXDNA" class. It's simple and easy, please follow the step as below:
First, import package as follows:
Developer can listen RXTX broadcast channel to get data packet from who sending by implement "EventRXTXDNA" class. It's simple and easy, please follow the step as below:
import org.osgi.service.event.Event; import org.osgi.service.event.EventHandler; import com.infairy.cocina.SDK.gene.EventRXTXDNA;then, implement "EventHandler" in your bundle class:
package yourBundle; public class CommData implements BundleActivator, EventHandler{ .... }Third, implement HandleEvent() method,
public void handleEvent(Event evnt) { ... }Last, do not forget listen RXTX broadcast channel in start() method,
package yourBundle; public class CommData implements BundleActivator, EventHandler{ .... public void start(BundleContext context) throws Exception { ... device.ListenBroadcast(context, this, device.BROADCAST_CHANNEL_RXTX); } }after done the process as above, you can get the data from all RXTX channel, but not every data is yours, so you have to filter the data by "RXTX_Device_Name", and then, check the data checksum value if it is necessary.
public void handleEvent(Event evnt) { //cast EventRXTXDNA from event property "BROADCAST_RXTX_EVENTOBJECT" EventRXTXDNA erxtx=(EventRXTXDNA)evnt.getProperty(device.BROADCAST_RXTX_EVENTOBJECT); if(erxtx==null) return; //get chip name String CHIPName = erxtx.RXTX_Device_Name; //get rxtx channle id String RXTXID = erxtx.RXTX_ID; if (RXTXID == null || RXTXID.equals("")) return; //check if the chip name is CH34X, the chip name please refer to com.infairy.cocina.SDK.device.DevicePool#RXTX_CHIP_xxx if (!CHIPName.equals(device.RXTX_CHIP_CH34X)) return; //there are 5 RXTX_CONNECT_TYPE , replease refer to com.infairy.cocina.SDK.device.DevicePool#RXTX_CONNECT_TYPE_xxx //RXTX_Data is data byte array from COMM port. if(erxtx.RXTX_Connect_Type.equals(device.RXTX_CONNECT_TYPE_DEVICE_CONNTECTED)){ checkSum(erxtx.RXTX_Data); } }Ok, you can get the data from RXTX and check the data is correct or not.
Now, if you want the send data back to the COMM port, you can coding as below:
Fisrt, get the "serial" service:
package yourBundle; public class CommData implements BundleActivator, EventHandler{ Serial serial; public void start(BundleContext context) throws Exception { serial = (Serial) Tools.getService(context, Serial.class.getName(), "(IO=COMM)"); } }Don't forget to import "serial" class:
import com.infairy.cocina.bundle.COMM.Serial;finally, two methods to send data back:
// RXTXID is COMM port ID, the data byte array will send back from this ID // cmd is data byte array which you want to send back serial.sendByte(RXTXID, cmd); //delayTimeMillis means, when to call the sendByte and send the data back, Infairy will send data after waiting for "delayTimeMillis" milliseconds. serial.sendByte(RXTXID, cmd, delayTimeMillis);Finally, if you are a hardware maker, you can send the packet of your hareware devices by implement "EventRXTXDNA".
Broadcast the hardware connected and disconnected status:
EventRXTXDNA erxtx=new EventRXTXDNA(); erxtx.RXTX_ID=String.valueOf(this.toString()); //The connected channel erxtx.RXTX_Device_Name=Conn_Chip_Name; //Chip name of hardware erxtx.RXTX_Device_Mac=Conn_Chip_Mac; //hardware address if(true) erxtx.RXTX_Connect_Type=device.RXTX_CONNECT_TYPE_DEVICE_CONNTECTED; else erxtx.RXTX_Connect_Type=device.RXTX_CONNECT_TYPE_DEVICE_DISCONNTECTED; /* * broadcast process */ device.SensorBroadcast(erxtx);Broadcast thd data packet:
EventRXTXDNA erxtx=new EventRXTXDNA(); erxtx.RXTX_Device_Name=Conn_Chip_Name; erxtx.RXTX_Device_Mac=Conn_Chip_Mac; erxtx.RXTX_ID=String.valueOf(this.toString()); // The connected channel erxtx.RXTX_Data=data; //data packet , byte array erxtx.RXTX_Connect_Type=device.RXTX_CONNECT_TYPE_DATA_COMING; /* * broadcast process */ device.SensorBroadcast(erxtx);following code shows the EventRXTXDNA interface:
/** * RXTX Channel ID */ public String RXTX_ID=""; /** * Device name */ public String RXTX_Device_Name=""; /** * Device Mac Address */ public String RXTX_Device_Mac=""; /** * RXTX Data byte array */ public byte[] RXTX_Data; /** * RXTX Connect type: RXTX_CONNECT_TYPE_DATA_COMING / RXTX_CONNECT_TYPE_DEVICE_CONNTECD */ public String RXTX_Connect_Type="";