Jump to content
Sign in to follow this  
iiyama87

Communication Problems Rs232

Recommended Posts

Hello everyone,

 

I’m having problems with communication between 2 microcontrollers.

The connections is made with RS232, 9600B.

 

I am using rs232.h and rs232_driver.h

 

The microcontroller which is sending (18F1220) does the following:

 

Char buffer[10];

 

While(1)

{

Puts(“START”);

Delay_ms(100);

}

 

 

The receiving microcontroller (18F4550) does the following:

 

Char buffer[10];

Char received[10];

 

Gets(received);

 

If(received==”START”)

{

Status=1;

}

Puts(received); //This goes to the PC’s serial port

 

 

When I look on the PC with HyperTerminal I see START, empty line, START en so on. However the if statement isn’t executed.

 

Does anyone know why I see START in HyperTerminal but isn’t detected by the if statement of the microcontroller?

 

Any information is welcome.

Share this post


Link to post
Share on other sites

Hi iiyama87,

 

Unfortunately you cannot directly compare a string with the contents of a string buffer.

 

What you need to do is create a string parser that will process data in your buffer character by character.

 

Try sending and detecting a single character (followed by NULL) first and build on that.

 

All HyperTerminal is doing is receiving and displaying what you sent character by character.

 

Google something like 'RS232 String Parser'.

 

Good luck

 

davidb

Share this post


Link to post
Share on other sites

strcmp to be exact. I don't know off hand how Puts and Gets are implemented in your context but if they are done according to the spec, strcmp should work for you. Just remember to use:

 

if(!strcmp(received,”START")){ // received == START.

Status = 1;

}

 

Most folks forget that it returns 0 if they equal since strcmp is used to compare less than, equal or greater than it will return -1,0 or 1 (usually).

 

Here are some notes from cplusplus.com

 

int puts ( const char * str );

Write string to stdout

Writes the C string pointed by str to stdout and appends a newline character ('\n').

The function begins copying from the address specified (str) until it reaches the terminating null character ('\0'). This final null-character is not copied to stdout

 

char * gets ( char * str );

Get string from stdin

Reads characters from stdin and stores them as a string into str until a newline character ('\n') or the End-of-File is reached.

The ending newline character ('\n') is not included in the string.

A null character ('\0') is automatically appended after the last character copied to str to signal the end of the C string.

Notice that gets does not behave exactly as fgets does with stdin as argument: First, the ending newline character is not included with gets while with fgets it is. And second, gets does not let you specify a limit on how many characters are to be read, so you must be careful with the size of the array pointed by str to avoid buffer overflows.

Edited by trossin

Share this post


Link to post
Share on other sites

Hi Folks

 

The point here is that "strings" simply don't exist as a data type in 'C'.

All that you can find in 'C' is arrays of 'char', as can be seen by the way you declare it.

 

The '\0' termination in himself is the conventioned marker for the end of a partially filled array of char.

This marker allowed for the implementation of functionality similar to the "string" data type of other programing languages .

 

But being "arrays" makes it necessary to walk along the array comparing char by char to obtain a string comparision.

 

In terms of practical use, dadidb and trossin already covered it, I only have one note to had to what they explained.

 

Some "string functions" have a buffer size parameter to prevent the risk of buffer overflow, for instance, "strncpy".

When using one of those, there is the risk of losing the terminating '\0' if the buffer fills.

In this case we should take precautions to put it back there before doing any more processing that relies on the presence of the '\0'.

 

 

Best regards

 

Jorge

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...