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 CCS Technical Support

Serial Communication

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



Joined: 04 Oct 2012
Posts: 1

View user's profile Send private message

Serial Communication
PostPosted: Thu Oct 04, 2012 3:09 am     Reply with quote

Hello Guys,

We are trying to setup a serial communication via other ports which are not RX/TX (hardware communication ports) and we are facing with the problem that the program is waiting until the serial data arrives. This means that if the data never arrives then the program gets stuck there.

We want the program to return back to the loop if the serial data does not arrive within 5 ms (eg.)

Can you please let us know how we can achieve this and provide us the sample code ?

Thanks in advance for your time and assistance.

Best Regards,

Akin
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Thu Oct 04, 2012 4:29 am     Reply with quote

Roughly:

Code:

    int16 wait_for;

    waitfor=1000;
    do {
        if (kbhit()) {
            //here get your data and exit

            break;
        }
        delay_us(5);
    } while (--waitfor>0);

kbhit, returns immediately, but tells you if the incoming serial line has dropped (signalling the start of a character). So this will loop 1000*, testing kbhit, and waiting 5uSec between tests -> so slightly over 5mSec, and getting the data if it arrives.

Best Wishes
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Thu Oct 04, 2012 12:47 pm     Reply with quote

You shouldn't "wait until serial data arrives". But if you have a processor with a single UART, using other pins for software UARTs is quite challenging. You might do better to buy a few cheap PICs and use them just for the UART function, and get them to talk to your processor via some other interface.

If you just want one extra UART, and if it doesn't have to be full duplex, you can do it easily enough using an interrupt, first on a low-going edge (the start bit) and then again with a timer, once on each data bit. That way the processor never has to "wait" for communication at all.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Thu Oct 04, 2012 4:15 pm     Reply with quote

if your problem is requiring more than 1 RS-232 i/o pair then have you considered parts like the very cost effective 18F4xK22 and the 18F2xk22
with DUAL hardware serial ports ???
( and a raft of other useful hardware functions , downright CHEAP tool)
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Thu Oct 04, 2012 6:54 pm     Reply with quote

asmboy wrote:
if your problem is requiring more than 1 RS-232 i/o pair then have you considered parts like the very cost effective 18F4xK22 and the 18F2xk22
with DUAL hardware serial ports ???



Agreed -- for all the hassles of dealing with 2 serial ports, get a PIC with > 1 hardware EUSARTs.

All the hassle goes away.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Fri Oct 05, 2012 9:41 am     Reply with quote

OK, but what if the requirement is 3 or 4 or more UARTs? The guy never said how many he wants, and everyone is talking as if 1 extra will do the job.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Oct 05, 2012 10:12 am     Reply with quote

Quote:

3 or 4 or more UARTs?


external 16* family 4 or 8 uarts with FIFO awkward but can work
hardware is the only way to go .......

IMHO ONE or more software USARTS is at least ONE too many.

it is a wholly unreliable way to code a commercial work - 4 sure, since
sooner or later the RX function will return junk bytes , and
software Rs232 transmit will fail if there are interrupts are enabled.
bkamen



Joined: 07 Jan 2004
Posts: 1615
Location: Central Illinois, USA

View user's profile Send private message

PostPosted: Fri Oct 05, 2012 11:39 am     Reply with quote

John P wrote:
OK, but what if the requirement is 3 or 4 or more UARTs? The guy never said how many he wants, and everyone is talking as if 1 extra will do the job.


If the poster wants more than 2, then look to the PIC24 and PIC32 series of microcontrollers *IF HE wants them built in*.


I did a PIC project with an external Octal UART connected to a pic18F97J60 and that worked as well - with known limitations.

-Ben
_________________
Dazed and confused? I don't think so. Just "plain lost" will do. :D
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Fri Oct 05, 2012 1:27 pm     Reply with quote

Or of course quite a few I2C UART's, or just a second PIC.

Best Wishes
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