RS485 library received multiple bytes data

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
johnsonpay
Posts: 53
Joined: 27 May 2013 13:48

RS485 library received multiple bytes data

#1 Post by johnsonpay » 14 Oct 2017 02:01

Hi all,


May I know how to use mikroc pro c RS485 library received multiple bytes data? because my device sent out is 7 data bytes, but RS485 library can received 3 bytes data only

User avatar
dusan.poluga
mikroElektronika team
Posts: 780
Joined: 02 Feb 2017 14:21

Re: RS485 library received multiple bytes data

#2 Post by dusan.poluga » 17 Oct 2017 10:36

Hi,

Can you post the part of the code where you handle the RS485 incoming data ?

Regards,
Dusan Poluga.

johnsonpay
Posts: 53
Joined: 27 May 2013 13:48

Re: RS485 library received multiple bytes data

#3 Post by johnsonpay » 18 Oct 2017 05:43

dusan.poluga wrote:Hi,

Can you post the part of the code where you handle the RS485 incoming data ?

Regards,
My device sent out 0xAA 0x11 0x12 0x13 0x14 0x2A 0x3F 0xFF, so how can receive this 8 bytes data, and sent out 8 bytes data to my device ?

User avatar
dusan.poluga
mikroElektronika team
Posts: 780
Joined: 02 Feb 2017 14:21

Re: RS485 library received multiple bytes data

#4 Post by dusan.poluga » 20 Oct 2017 10:52

Hi,

I believe my colleague has explained this to you on the ticket.
Can you tell me what part of his explanation is not clear ?

Best Regards,
Dusan Poluga.

gonca
Posts: 7
Joined: 17 Oct 2017 02:30

Re: RS485 library received multiple bytes data

#5 Post by gonca » 23 Oct 2017 15:57

Hello all,

could you kindly please share the code ?
Tks

Gonçalo

johnsonpay
Posts: 53
Joined: 27 May 2013 13:48

Re: RS485 library received multiple bytes data

#6 Post by johnsonpay » 24 Oct 2017 08:38

dusan.poluga wrote:Hi,

I believe my colleague has explained this to you on the ticket.
Can you tell me what part of his explanation is not clear ?

Best Regards,

below is your colleague reply me, he explained very clear, but I don't want received start and stop bytes, example,master send 0xaa 0x10 0x11 0x12, then
my slave will received 0xaa 0x10 0x11 0x12 so I can create my own protocol.


The library uses up to 3 bytes to send, but that is only a buffer. After you confirm that 3 bytes has been received (by checking other "status" bytes that are sent consequently)

For example, the RS485 Master Receive function: RS485Master_Receive(char *data_buffer);

data_buffer: 7 byte buffer for storing received data, in the following manner:
data[0 - 2]: message content
data_buffer[3]: number of message bytes received, 1–3
data_buffer[4]: is set to 255 when message is received
data_buffer[5]: is set to 255 if error has occurred
data_buffer[6]: address of the Slave which sent the message

so when the message receiving is finished (indicated by the data_buffer[4] status), you can store the message someplace else - in an appropriate memory location or similar, and start the packet receiving loop again. Every packet of data you receive, should be in the format similar to this, depending if it's a Slave or Master. The library is very comprehensively explained in the Help manual of the compiler itself, you should be able to understand how to use it, by reading the Help.

In short - the packet you are trying to receive should be compliant to this format. Often, message which is sent contains zeros, to make sure that the specific status bytes were actually changed by the receiving end / not just passed through...
If you have further questions, feel free to ask them here.

johnsonpay
Posts: 53
Joined: 27 May 2013 13:48

Re: RS485 library received multiple bytes data

#7 Post by johnsonpay » 24 Oct 2017 08:38

gonca wrote:Hello all,

could you kindly please share the code ?
Tks

Gonçalo

Hi Goncalo,

Are you facing this issue ? I still in testing

User avatar
dusan.poluga
mikroElektronika team
Posts: 780
Joined: 02 Feb 2017 14:21

Re: RS485 library received multiple bytes data

#8 Post by dusan.poluga » 26 Oct 2017 15:18

johnsonpay wrote:
dusan.poluga wrote:Hi,

I believe my colleague has explained this to you on the ticket.
Can you tell me what part of his explanation is not clear ?

Best Regards,

below is your colleague reply me, he explained very clear, but I don't want received start and stop bytes, example,master send 0xaa 0x10 0x11 0x12, then
my slave will received 0xaa 0x10 0x11 0x12 so I can create my own protocol.


The library uses up to 3 bytes to send, but that is only a buffer. After you confirm that 3 bytes has been received (by checking other "status" bytes that are sent consequently)

For example, the RS485 Master Receive function: RS485Master_Receive(char *data_buffer);

data_buffer: 7 byte buffer for storing received data, in the following manner:
data[0 - 2]: message content
data_buffer[3]: number of message bytes received, 1–3
data_buffer[4]: is set to 255 when message is received
data_buffer[5]: is set to 255 if error has occurred
data_buffer[6]: address of the Slave which sent the message

so when the message receiving is finished (indicated by the data_buffer[4] status), you can store the message someplace else - in an appropriate memory location or similar, and start the packet receiving loop again. Every packet of data you receive, should be in the format similar to this, depending if it's a Slave or Master. The library is very comprehensively explained in the Help manual of the compiler itself, you should be able to understand how to use it, by reading the Help.

In short - the packet you are trying to receive should be compliant to this format. Often, message which is sent contains zeros, to make sure that the specific status bytes were actually changed by the receiving end / not just passed through...
If you have further questions, feel free to ask them here.
The library was written to function this way like my colleague explained.
A user can always create his own protocol to transfer the data.

Best Regards,
Dusan Poluga.

amirzandieh
Posts: 1
Joined: 28 Mar 2023 15:02

Re: RS485 library received multiple bytes data

#9 Post by amirzandieh » 28 Mar 2023 15:20

/*
Hello,
This is my code for RS485 communication between to PIC micro (PIC16F1829:Master and PIC16F1824:Slave). But it do not work!
Main problem is sending more than 3-byte data to slave.

Logic of code:
1. Slave start sending a request to master by "#1".
2. Master send 3-byte of my data (Part 1).
3. Slave send again 2nd request to master by "#2".
4. Master send reminding 2-byte of data to slave (Part 2).
5. Slave compare sent byte with expected-byte for detecting error.
thanks for your attention.
Amir
*/

#define TXD

#define UART_SENTINEL '$' // end of sending data
#define NULL '\0'

sbit LED1 at RC3_bit; // uC.10
sbit LED2 at RC1_bit; // uC.9
volatile char slaveRxBuf[6];




#ifdef TXD //Master is data SENDER

sbit rs485_rxtx_pin at RC2_bit; // set transcieve pin
sbit rs485_rxtx_pin_direction at TRISC2_bit; // set transcieve pin direction
volatile char part1Flag=1;
volatile char masterTxBuf[]="ABCD$";
volatile char masterRxBuf[10];

void RS485_SendPart1(char *myData, char datalen, char Slave_address)
{
char part[10];

// Part 1
part[0] = myData[0];
part[1] = myData[1];
part[2] = myData[2];
part[4] = 0;
part[5] = 0;
part[6] = 0;
RS485Master_Send(part, datalen, Slave_address);
}

void RS485_SendPart2(char *myData, char datalen, char Slave_address)
{
char part[10];

// Part 2
part[0] = myData[3];
part[1] = myData[4]; // sentinel
part[2] = ' '; // dummy
part[4] = 0;
part[5] = 0;
part[6] = 0;
RS485Master_Send(part, datalen, Slave_address);
}

// Interrupt routine
void interrupt()
{
RS485Master_Receive(masterRxBuf);
}

void setup(void)
{
// 0b------10 -> Internal oscillator block (disabled menu(CFG Word1))
OSCCON = 0b01111010;

ANSELC = 0; // Configure AN pins as digital I/O
ADCON0 = 0b00110000;

//1829 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
APFCON0 |= 0x84; // RX/DT function is on RC5 (pin 5)
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;

INTCON = 0b11000000;
TRISC = 0b00100000;

LED1 = LED2 = 1;
Delay_ms(1000);
LED1 = LED2 = 0;

UART1_Init(9600); // initialize UART1 module
Delay_ms(100);

RS485Master_Init(); // initialize MCU as Master

RCIE_bit = 1; // enable interrupt on UART1 receive
TXIE_bit = 0; // disable interrupt on UART1 transmit
PEIE_bit = 1; // enable peripheral interrupts
GIE_bit = 1; // enable all interrupts
}

void main()
{
setup();

while (1)
{
if (masterRxBuf[0]=='#')
{
if (masterRxBuf[1]=='1')
RS485_SendPart1(masterTxBuf, 1, 160);
else if (masterRxBuf[1]=='2')
RS485_SendPart2(masterTxBuf, 1, 160);
}
masterRxBuf[0] = 0;
masterRxBuf[1] = 0;
masterRxBuf[2] = 0;
masterRxBuf[4] = 0;
masterRxBuf[5] = 0;
masterRxBuf[6] = 0;
}
}









#else //Slave is requester and RECEIVER

sbit rs485_rxtx_pin at RC2_bit; // set transcieve pin
sbit rs485_rxtx_pin_direction at TRISC2_bit; // set transcieve pin direction
char dataValid=0;
volatile char slaveRxBuf2[9];
volatile char slaveTxBuf[9];
volatile part1Requested=1;
volatile part1Received=0;
volatile part2Received=0;

// Interrupt routine
void interrupt()
{
RS485Slave_Receive(slaveRxBuf2);
part1Received = 0;
part2Received = 0;
if (slaveRxBuf2[4])
{
if (slaveRxBuf2[1] != UART_SENTINEL)
part1Received = 1;
else
part2Received = 1;
}
}

void setup()
{
// 0b------10 -> Internal oscillator block (disabled menu(CFG Word1))
OSCCON = 0b01111010;

ANSELC = 0; // Configure AN pins as digital I/O
ADCON0 = 0b00110000;

C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;

INTCON = 0b11000000;
TRISC = 0b00100000;

LED1 = LED2 = 1;
Delay_ms(1000);
LED1 = LED2 = 0;

UART1_Init(9600); // initialize UART1 module
Delay_ms(100);
RS485Slave_Init(160); // Intialize MCU as slave, address 160

RCIE_bit = 1; // enable interrupt on UART1 receive
TXIE_bit = 0; // disable interrupt on UART1 transmit
PEIE_bit = 1; // enable peripheral interrupts
GIE_bit = 1; // enable all interrupts
}

void main()
{
setup();

while (1)
{
if (!part1Requested)
{
part1Received = 0;
part2Received = 0;
part1Requested = 1;
// request 1st part to master
slaveTxBuf[0] = '#';
slaveTxBuf[1] = '1';
slaveTxBuf[2] = 0;
slaveTxBuf[4] = 0;
slaveTxBuf[5] = 0;
slaveTxBuf[6] = 0;
RS485Slave_Send(slaveTxBuf, 1);
}
if (part1Received)
{
part1Received = 0;
part2Received = 0;
part1Requested = 1;
slaveRxBuf[0] = slaveRxBuf2[0];
slaveRxBuf[1] = slaveRxBuf2[1];
slaveRxBuf[2] = slaveRxBuf2[2];

// request 1st part to master
slaveTxBuf[0] = '#';
slaveTxBuf[1] = '2';
slaveTxBuf[2] = 0;
slaveTxBuf[4] = 0;
slaveTxBuf[5] = 0;
slaveTxBuf[6] = 0;
RS485Slave_Send(slaveTxBuf, 1);
}
if (part2Received)
{
part1Received = 0;
part2Received = 0;
part1Requested = 0;
slaveRxBuf[3] = slaveRxBuf2[0];
slaveRxBuf[4] = slaveRxBuf2[1]; // sentinel
slaveRxBuf[5] = NULL;
slaveRxBuf[6] = 0;

if (strcmp(slaveRxBuf, "ABCD$")==0)
{
LED1 = 1;
Delay_ms(200);
LED1 = 0;
Delay_ms(200);
LED1 = 1;
Delay_ms(200);
LED1 = 0;
Delay_ms(200);
LED2 = 0;
}
}
else
{
LED1 = 0;
}
}
}
#endif

Post Reply

Return to “mikroC PRO for PIC General”