모두버스 통신은 이런식으로 보내진다.

/***********************************************************
 * global variables
 *  - CAUTION : do not declare 'host' as global variable name. already declared.
 ***********************************************************/

/***********************************************************
 * Parse Address(주소 해석)
 *  - Optional
 ***********************************************************/
function parseAddress(addr)
{
	if(addr.length < 1) return null;
	if(addr == "0001"){
		var id = 0x01;
	}else if (addr == "0301" ){
		var id = 0x12D;
	}else if(addr == "0302"){
		var id = 0x12E;
	}else {
		return null;
	}
	return host.createDeviceAddress(addr, addr, id, 0);
}

/***********************************************************
 * Build a packet for 'Read Request'(읽기 요청 패킷 생성)
 *  - Mandatory
 ***********************************************************/
function buildReadRequest(value, output, session)
{	
	var id = value.address.position;
	output.bigEndian=false;
	output.putInt8(0x01);
	output.putInt8(0x03);
	if (id == "0301"){
	output.putInt8(0x01);
	output.putInt8(0x2D);
	}else if( id == "0302"){
	output.putInt8(0x00);
	output.putInt8(0x2E);
	}else if(id == "0001"){
	output.putInt8(0x00);
	output.putInt8(0x01);
	}
	output.putInt8(0x00);
	output.putInt8(0x01);
	

	var crc = output.getCRC16(0,6);
	host.log("crc : "+crc);
	output.putInt16(crc);
	//output.save("c:/mod_output.dat");
	output.commit();
}

/***********************************************************
 * Parse a packet of 'Read Repsonse'(읽기 응답 패킷 해석)
 *  - Mandatory
 ***********************************************************/
function parseReadResponse(input, value, session)
{
	if(input.length <7){
	//아직 데이터를 덜 받았기 때문에 계속 대기해야 하므로 false리턴
	return false;
	}
	input.bigEndian=true;
	//input.save("c:/mod_input.dat");
	input.skip(3);
	data = input.getInt16();
	value.intValue=data;
	input.skip(2);
	input.commit();
	//데이터를 모두 받고 처리가 끝났기 때문에 true 리턴
	return true;
}

/***********************************************************
 * Build a packet for 'Write Request'(쓰기 요청 패킷 생성)
 *  - Optional
 ***********************************************************/
function buildWriteRequest(value, output, session)
{
	var data = value.intValue;
	output.bigEndian = true;
	output.putInt8(0x01);
	output.putInt8(0x06);
	output.putInt8(0x01);
	output.putInt8(0x2D);
	output.putInt16(data);
	output.bigEndian = false;
	var w_crc = output.getCRC16(0,6);
	host.log("crc : "+w_crc);
	output.commit();
	
}

/***********************************************************
 * Parse a packet of 'Read Repsonse'(쓰기 응답 패킷 해석)
 *  - Optional
 ***********************************************************/
function parseWriteResponse(input, value, session)
{
	if(input.length <7){
	return false;
	}
	
	input.bigEndian = true;
	
	input.skip(8);
	input.commit();
	
	return true;
}

 


var h = '0 3'; 
    for (var i = 0; i <h.length; i++)
    {
        var message = "0x" + pad(h[i].charCodeAt(0).toString(16),2);
        console.log(message)
    }

    for (var i = 0; i <h.length; i++)
    {
        var message = h[i].charCodeAt(0);
        console.log(typeof(message))
    }


function pad(n, width) {
  n = n + '';
  return n.length >= width ? n : new Array(width - n.length + 1).join('0') + n;
}
//문자 유니코드 16진수로 변환

블로그 이미지

Or71nH

,