RS232/RS485

RS232/RS485

RS232 initialization

In order to use RS232/RS485 communication initialization function have to be used. As you can see Init() function called with desired parameters at the start of the program in part 1:

Example part 1
new Send_Packet[50] =''A value: xx C Value: xx''; new Read_Packet[50]; new Flag_Data; new leng; main(){ Init(RS232, 9600, WORDLENGTH_8, STOPBITS_1, PARITY_NONE);
  • Speed (baud rate) is serial port data transfer speed in bit/s. For example: 9600 bit/s

  • wordLength (data bits) is data size in bits. For example: WORDLENGTH_7 - 7 bit long data packet.

  • stopBits (stop bits) is number of bits that shows end of a single packet.

  • parity is error detection bit, which show that data is made out of even or odd number of ones.

For more information check Init(RS232,...); function or Init(RS485,...)

Checking if new data is available

Let's say we want to read information with RS232/RS485 communication line. So one option would be wait for the data by checking buffer length. if length not zero then read RS232 buffer as is done in part 2:

Example part 2
while(Flag_Data!=1){ write_buf(RS232, 16 ,''waiting for data''); leng = buf_len(RS232); // Read RS232 buffer length if(leng != 0){ // If length not equal 0 then read RS232 buffer read_buf(RS232, leng, Read_Packet); // Read RS232 buffer into rs array (read 4 bytes length array) Flag_Data=1; } Delay(1000); }

 

Sending Data

After reading information from buffer, you can work with your new information. In case you want to send some kind of data via RS232/RS485 - write_buf() function have to be used as is done in part 4:

Example part 4
memset(Send_Packet[9],Read_Packet[2],1); memset(Send_Packet[10],Read_Packet[3],1); memset(Send_Packet[21],Read_Packet[12],1); memset(Send_Packet[22],Read_Packet[13],1); write_buf(RS232, strlen(Send_Packet) ,Send_Packet); // this function sends full text