maniac84
Full Member level 6
- Joined
- Mar 4, 2012
- Messages
- 337
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 1,298
- Activity points
- 3,661
hi guys,
I'm trying to create a converter which convert RS485 signal to USB signal using PIC18F25K50. In other words, whatever I received on the RS485 side, I will straightaway transmit it out through the USB port.
I will describe my application. I have a device A and a device B which both using RS485 to communicate with each other. My PIC18F25k50 will be put in device C which is just a listener to listen to what device A and device B communicate. Device C will be connected by USB to device D. So, device C will receive whatever device A and device B in RS485 signal (serial) and convert it to USB signal and transmit to device D.
Below is my sample code. I'm using my main() loop to do the serial receiving:
For one protocols that device A and device B communicate, it will begin with SOH (0x01) and end with EOT (0x04).
The problem that I face is the USB side does not send out the protocol fast enough. Like if I got received 20 protocols (20x20bytes=400bytes) at a time on the serial side, the USB side will only be able to send out 6 or 7 protocols only. Is it that my coding does not process the protocols fast enough?
Like for example, if my first protocols has not been send out by the USB in time, I already received the second protocol which will overwrite the 1st protocol. So, I've miss the 1st protocol. Is this happening?
The baudrate of my serial is 9600 and the crystal I'm using is 20MHz.
I'm trying to create a converter which convert RS485 signal to USB signal using PIC18F25K50. In other words, whatever I received on the RS485 side, I will straightaway transmit it out through the USB port.
I will describe my application. I have a device A and a device B which both using RS485 to communicate with each other. My PIC18F25k50 will be put in device C which is just a listener to listen to what device A and device B communicate. Device C will be connected by USB to device D. So, device C will receive whatever device A and device B in RS485 signal (serial) and convert it to USB signal and transmit to device D.
Below is my sample code. I'm using my main() loop to do the serial receiving:
Code:
void main(void)
{
while(1)
{
if (PIR1bits.RCIF==1)
{
rxbuffer = RCREG;
RS485RXDATA[RxDataPos++] = rxbuffer;
if (rxbuffer == 0x04)
b485Complete = 1;
}
if (b485Complete == 1)
{
if (HIDTxHandleBusy(USBInHandle) == 0)
{
memset (hid_in, 0, sizeof(hid_in));
byTmp1 = strlen((char*)RS485RXDATA);
hid_report_in[0] = SOH;
memcpy ((void*)&hid_in[1], (void*)&RS485RXDATA[0], byTmp1);
hid_report_in[byTmp1+1] = EOT;
hid_report_in[byTmp1+2] = 0x00;
USBInHandle = HIDTxPacket(HID_EP, (BYTE*)&hid_in, HID_INPUT_REPORT_BYTES); // 64 bytes
b485Complete = 0;
n485RxDataPos = 0;
memset (RS485RXDATA, 0, sizeof(RS485RXDATA));
}
}
}
}
For one protocols that device A and device B communicate, it will begin with SOH (0x01) and end with EOT (0x04).
The problem that I face is the USB side does not send out the protocol fast enough. Like if I got received 20 protocols (20x20bytes=400bytes) at a time on the serial side, the USB side will only be able to send out 6 or 7 protocols only. Is it that my coding does not process the protocols fast enough?
Like for example, if my first protocols has not been send out by the USB in time, I already received the second protocol which will overwrite the 1st protocol. So, I've miss the 1st protocol. Is this happening?
The baudrate of my serial is 9600 and the crystal I'm using is 20MHz.
Last edited: