RS232 is the best choice when we have peer_to_peer communication like PLC_HMI or PLC_PC its easy to use and full-duplex and has the ability to control the bus electrical by(CTS RTS).
- Step 1:
Fix 4 buttons to the H1DR0 RS232 sender to determine which output relay has to switch on/off.
/* User Task */
void UserTask(void * argument)
{
//AddPortButton(MOMENTARY_NO,P1);
AddPortButton(MOMENTARY_NO,P4);
AddPortButton(MOMENTARY_NO,P2);
AddPortButton(MOMENTARY_NO,P3);
AddPortButton(MOMENTARY_NO,P5);
//AddPortButton(MOMENTARY_NO,P6);
//////////////////////////////////////////////////on module
SetButtonEvents(P2, 1, 1, 0, 0, 0, 0, 0, 0, 0);/////p2->p3
SetButtonEvents(P4, 1, 1, 0, 0, 0, 0, 0, 0, 0);/////p4->p1
SetButtonEvents(P3, 1, 1, 0, 0, 0, 0, 0, 0, 0);/////p3->p4
SetButtonEvents(P5, 1, 1, 0, 0, 0, 0, 0, 0, 0);/////p5->p5
//SetButtonEvents(P1, 1, 0, 0, 0, 0, 0, 0, 0, 0);///p1->p2
//SetButtonEvents(P6, 1, 0, 0, 0, 0, 0, 0, 0, 0);///p6->p6
/* Infinite loop */
for(;;)
{
}
}
void buttonClickedCallback(uint8_t port)
{
if(port == P4)
{
y0=1;
y1=0;
y2=0;
y3=0;
}
else if(port == P2)
{
y0=0;
y1=1;
y2=0;
y3=0;
}
else if(port == P3)
{
y0=0;
y1=0;
y2=1;
y3=0;
}
else if(port == P5)
{
y0=0;
y1=0;
y2=0;
y3=1;
}
y=y3<<3 | y2<<2 |y1<<1 | y0;
MODBUS_ASCII(0x02, 0x06, 0x10, 0x04, 0x00, y); //writing regester D4 for output ->y0 >> ON
}
void buttonDblClickedCallback(uint8_t port)
{
y0=0;
y1=0;
y2=0;
y3=0;
y=y3<<3 | y2<<2 |y1<<1 | y0;
MODBUS_ASCII(0x02, 0x06, 0x10, 0x04, 0x00, y); //writing regester D4 for output ->y0 >> ON
}
- Step 2:
write the code to the sender module which is Master in Modbus/ASCII and make the messages in frames with LRC to be acceptable in Slave (PLC).
void MODBUS_ASCII(uint8_t address_slave, uint8_t functio_code, uint8_t start_reg_h,uint8_t start_reg_l, uint8_t number_of_reg_h,uint8_t number_of_reg_l)
{
modbus_mes[0]=0x3A; /// :
if(address_slave<=0x0F)
{
modbus_mes[1]=HEX_2_ASCII(0x00);
modbus_mes[2]=HEX_2_ASCII(address_slave);
}
else
{
modbus_mes[1]=HEX_2_ASCII(address_slave/0x10);
modbus_mes[2]=HEX_2_ASCII(address_slave%0x10);
}
modbus_mes[3]=HEX_2_ASCII(0x00);
modbus_mes[4]=HEX_2_ASCII(functio_code);
modbus_mes[5]=HEX_2_ASCII(start_reg_h/0x10);
modbus_mes[6]=HEX_2_ASCII(start_reg_h%0x10);
modbus_mes[7]=HEX_2_ASCII(start_reg_l/0x10);
modbus_mes[8]=HEX_2_ASCII(start_reg_l%0x10);
modbus_mes[9] =HEX_2_ASCII(number_of_reg_h/0x10);
modbus_mes[10]=HEX_2_ASCII(number_of_reg_h%0x10);
modbus_mes[11]=HEX_2_ASCII(number_of_reg_l/0x10);
modbus_mes[12]=HEX_2_ASCII(number_of_reg_l%0x10);
uint8_t nLRC = 0 ; // LRC char initialized
nLRC =address_slave+functio_code+start_reg_h+start_reg_l+number_of_reg_h+number_of_reg_l;
nLRC=0xFF-nLRC;
nLRC++;
modbus_mes[13]=HEX_2_ASCII(nLRC/0x10);
modbus_mes[14]=HEX_2_ASCII(nLRC%0x10);
modbus_mes[15]=0x0D;
modbus_mes[16]=0x0A;
HAL_UART_Transmit(&huart1,modbus_mes,17,100);
}
and to convert between HEX and ASCII
uint8_t HEX_2_ASCII(uint8_t hex)
{
uint8_t result;
switch(hex)
{
case 0x00:
result=0x30;
break;
case 1:
result=0x31;
break;
case 2:
result=0x32;
break;
case 3:
result=0x33;
break;
case 4:
result=0x34;
break;
case 5:
result=0x35;
break;
case 6:
result=0x36;
break;
case 7:
result=0x37;
break;
case 8:
result=0x38;
break;
case 9:
result=0x39;
break;
case 0x0A:
result=0x41;
break;
case 0x0B:
result=0x42;
break;
case 0x0C:
result=0x43;
break;
case 0x0D:
result=0x44;
break;
case 0x0E:
result=0x45;
break;
case 0x0F:
result=0x46;
break;
}
return result;
}
- Step 3:
connect the sender module with the receiver one which will be acting as a bridge to convert the data to the USB H1AR2 module.
This data will transfer to any COM in the computer to monitor the messages and resending to another COM that is connected to PLC USB.
DON'T worry about wiring Hexabitz provides a variety of adapters.
Note: Delta PLC SE series has com port not RS232 as usual.
- Step 4:
Write code for both module H1AR2 and H1DR0 receiver to make them act as a bridge, using bridge instruction makes things easy breezy.
Note: to have more information about the plc code and other things see
Hexabitz H1AR1 Converter with Delta PLC project.
Comments