CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

RS485 UART - PC to PIC [SOLVED]

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

RS485 UART - PC to PIC [SOLVED]
PostPosted: Wed Jun 08, 2016 1:23 pm     Reply with quote

Hi All,

I am trying to establish communication between my Raspberry Pi and PIC microcontroller. Im using UART over RS485 to get this done.

I was able to make 2 pics communicate with each other using RS485. However its a different scenario with PC.

I see that there is mismatch in the data sent by PIC and PC. I use RS485.c which is supplied by CCS to get the communication going.


CCS code for PIC transmitter.
Code:

   for(try=0; try<5; ++try) {
      rs485_collision = 0;
      fputc((int16)0x100|rs485_id, RS485_CD);
      fputc((int16)0x100|to, RS485_CD);
      fputc(len, RS485_CD);

      for(i=0, cs=rs485_id^to^len; i<len; ++i) {
         cs ^= *data;
         fputc(*data, RS485_CD);
         ++data;
      }

      fputc(cs, RS485_CD);


Result for the above code for the string "GOD" was 254 254 52 checksum -99 in Microcontroller

C code for PC transmitter

Code:

int rs485_send_message(int8_t tto, int8_t len, int8_t* data) {
   int8_t try, i, cs;
   int n, ret = 0;

      
n = write(fd,(int16_t)0x100|rs485_id, sizeof((int16_t)0x100|rs485_id));
n = write(fd,(int16_t)0x100|tto, sizeof((int16_t)0x100|tto,1));
n = write(fd,len, sizeof(len));

      for(i=0, cs=rs485_id^tto^len; i<len; ++i) {
         cs ^= *data;
       printf("Data %s" , *data);
         n = write(fd, *data, 1);
         ++data;
      }


The result for the code in PC for "GOD" was 71 79 68
Checksum 27

What am I doing wrong has anybody used a PC as master and PIC as slave? Confused Confused Confused Confused

Any guidance sample code would be appreciated.

I am really sorry for my bad english, really tired with this project.
Crying or Very sad
_________________
Regards,
Devil
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 08, 2016 2:12 pm     Reply with quote

In the for() loop below, you are processing data at one byte per pass
through the loop. But then, you are attempting to display a string
on each pass through the loop. You should not do that. You should
be using %c, or %x to display one byte per pass.
Quote:
for(i=0, cs=rs485_id^tto^len; i<len; ++i) {
cs ^= *data;
printf("Data %s" , *data);
n = write(fd, *data, 1);
++data;
}



In the write() function below, you are giving it a single byte. You didn't
post the declaration for the write() function, but I'll bet that it wants a
pointer. The middle parameter should be just 'data' (without the quotes),
and not *data.
Quote:
for(i=0, cs=rs485_id^tto^len; i<len; ++i) {
cs ^= *data;
printf("Data %s" , *data);
n = write(fd, *data, 1);
++data;
}
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Wed Jun 08, 2016 3:52 pm     Reply with quote

Hi,

You are totally silent on the hardware interface details between the PC and the PIC, which makes me think this is a simulation exercise. Is that correct?

A PC won't have native RS485 ports, so you'll have to add an RS485 connection to the PC. So, you need to build or buy an RS485 interface for the PC end. Home made interfaces tend to be problematic as many don't adequately control the data direction on the RS485 bus. Commercially available solutions tend to be a bit expensive. Can you tell us which approach you took so that we may better understand your hardware configuration?

BTW, I use a 'B&B Electronics' USB-to-RS485 adapter for all my RS485 development work. Here in the US I think it was about $90 USD.

http://www.bb-elec.com/Products/USB-Connectivity/USB-to-Serial-Adapters/Mini-USB-to-Serial-Converters/485USBTB-2W.aspx
_________________
John

If it's worth doing, it's worth doing in real hardware!
temtronic



Joined: 01 Jul 2010
Posts: 9163
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jun 08, 2016 4:00 pm     Reply with quote

He also says he has a PI which is NOT a PC, so I'm even more confused....
and if he wants the PI to be the 'master', well it don't run CCS C !!

Jay
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

Everything is real -> architecture of my project.
PostPosted: Thu Jun 09, 2016 12:51 am     Reply with quote

Thank you Guru's for your reply.

@PCM programmer
I must have made a mistake, taking the code from a recent backup. I was really tired, frustrated and sad when I posted this topic.
I am pretty sure I must have used %u or %d for printf, that is how I received
Quote:
"GOD" was 254 254 52 checksum -99 in Microcontroller


Quote:
The result for the code in PC for "GOD" was 71 79 68
Checksum 27


@ezflyr - John
I am a real fan of Ttelmah, I have seen him say always never to trust a simulator. So everything I explained here is real hardware. Posting pics and once again I am sorry for my Shabby workplace. [If it works here it will work anywhere in the world ] Laughing

Pic1 - Entire shabby work bench https://drive.google.com/open?id=0BzMh-VPshlgpdjlaTzhsaVhSVzg

Pic2 - Hardware transmitter PIC16F73 + Max485 https://drive.google.com/open?id=0BzMh-VPshlgpWWdoYWZseFdmWnc

Pic3 - Receiver PIC16F73 + Max485 https://drive.google.com/open?id=0BzMh-VPshlgpZERSZUdkelJQQ1U

Pic4 - Raspberry pi - I am using a USB to Rs485 Coverter. https://drive.google.com/open?id=0BzMh-VPshlgpd3FNMXBYcVo1aWs

Pic5 - USB to RS485 converter https://drive.google.com/open?id=0BzMh-VPshlgpUFpLeEFXb0ZPRjg

@temtronic
You are right - Raspberry pi is much more than a PC Laughing

To be frank I really hated C and Linux until I got this Pi.


@ All
My first step was to make 2 pics communicate as master and Slave, I was able to receive the data that was transmitted by the master to the slave. Thanks to Ttelmah and the Gurus who silently helped me with their historical posts.

My second step was to make Raspberry Pi communicate with the same slave [I still am not able to do it.]

I checked my USB to RS485 and it really works and is not damaged. I can see the data that is being trasmitted by master to the slave using my Raspberry pi.

The only problem that I could really see was that the data is not being sent in the right format by the raspberry pi vs my PIC16F73 transmitter.
I copied the rs485_send_message from the driver files of CCS and made it work with the GCC for Raspberry Pi so that the logic does not change.

But again the data send out by the pic is not same as the data sent out by the raspberry pi.

Any help would be really appreciated, Am I doing it right? or are there any easy ways to get this done?
_________________
Regards,
Devil
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Thu Jun 09, 2016 1:18 am     Reply with quote

OK. Some data:

You are using a USB to RS485 adapter on the Pi.

Critical thing with RS485, as used by the PIC library, is that it uses 9bit. The Linux implementation on the Pi, does not make this easy.

Look at this:

<http://bohdan-danishevsky.blogspot.co.uk/2014/12/9-bit-uart-8m1-8s1-modes-in-linux-hack.html>

You have to cheat the UART, by swapping parity to get 9bit operation.

Currently your attempt to send the device address won't be working.

Alternative is to write your own software format for the PIC, using only 8bit transmission.
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

Thank you Mr T
PostPosted: Thu Jun 09, 2016 11:44 pm     Reply with quote

Thank you Mr T

Let me try it out, will post the details in a day or two.
_________________
Regards,
Devil
devilfrmheaven



Joined: 09 Feb 2016
Posts: 53

View user's profile Send private message

Update on Rs485 -- UART scrapped modbus is on.
PostPosted: Sun Jun 12, 2016 3:54 am     Reply with quote

Hi All,

Thank you all for the help. I felt UART over RS485 was simpler than modbus hence completely ignored the Modbus.

Since we had issues with the 9bit transmission with Pic microcontroller Vs 8 bit in linux machine and the headache of converting 9bits to 8bits and viceversa. I decided to go after Modbus.

Good news is PIC16F73 the small chip as suggested before is very well capable of handling Modbus and is working fine for me.

I was able to successfully transmit data collected by sensors over RS485 using modbus protocol.

Wink Wink Smile Smile Smile Smile Very Happy Very Happy Very Happy
_________________
Regards,
Devil
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Sun Jun 12, 2016 9:05 am     Reply with quote

Well done. Mark the thread 'solved' (you just edit the title to say this).
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group