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

serial string problem

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








serial string problem
PostPosted: Wed Sep 17, 2008 4:49 pm     Reply with quote

hi;

I'm attempting to communicate over ttl serial to an lcd screen and have been unsuccesful in doing so. The problem is sending a string array like this works ....as intended...this is inside a superloop.
Code:
     
char packet_buffer [50];
 
        packet_buffer[0] = 0;
        packet_buffer[1] = 0;
        packet_buffer[2] = 'H';
        packet_buffer[3] = 'e';
        packet_buffer[4] = 'l';
        packet_buffer[5] = 'l';
        packet_buffer[6] = 'o';   
      packet_buffer[7] = ' ';
      packet_buffer[8] = 'W';
      packet_buffer[9] = 'o';
      packet_buffer[10] = 'r';
      packet_buffer[11] = 'l';
      packet_buffer[12] = 'd';
      packet_buffer[13] = ' ';
      packet_buffer[14] = ' ';
      packet_buffer[15] = ' ';
      packet_buffer[16] = ' ';
      packet_buffer[17] = ' ';
      packet_buffer[18] = ' ';
      packet_buffer[19] = ' ';
      packet_buffer[20] = ' ';
      packet_buffer[21] = ' ';

                send_packet(packet_buffer, 22);

Since this is a clumsy way of sending text, I am using this code to try and do the same thing but the data does not show up on the lcd..
Code:
strcpy(packet_buffer, "00Hello World          ");

This is the loop that actually does the serial output.
Code:
   void send_packet(int* packet_ptr, int packet_length)
{
unsigned char vern;
data_length = packet_length;

    while (data_length--)
   {
      vern = *packet_ptr++;
      putc(vern);
   }
}


This data is being sent to a crystalfontz 635 lcd, hence the leading "00" which designates cursor position.
I'm not sure where the issue lies, I'm guessing the compiler may be adding invisible characters, but beyond that I'm out of ideas....if anyone has experience sending strings over serial, I'd love to hear from you.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Sep 17, 2008 5:09 pm     Reply with quote

00 and "00" are not the same.
0 == binary 0
'0' == ASCII character 0 == 0x30 == 48 decimal

What probably happens is that you are setting the cursor at position 48,48...

Try instead
Code:
"\0\0Hello World          "
The '\0' is an escape sequence for sending binary 0.


Note that the CCS compiler has a nice non-standard feature for redirecting the output of the printf statement. This can help you to make your code smaller and easier to understand.
Code:
printf(putc, "\0\0Hello World          ");
The first parameter to the printf function here is a function accepting a single character. Printf will then call this function in a loop for all the characters specified in the string.
This feature is used in the CCS LCD.C driver routine. You can even add your own special escape sequences, for example '\f' for clearing the screen, etc. See the function lcd_putc in C:\PICC\drivers\lcd.c
For more example code on this subject search the examples directory for the keyword lcd_putc.
Guest








PostPosted: Wed Sep 17, 2008 5:30 pm     Reply with quote

i've taken the "00" cursor position out of the equation so the code now reads as follows....issue still exists, works fine with my baseline code but not with the strcpy instruction.
Code:

 void send_packet(int* packet_ptr, int packet_length)
{
unsigned char vern;
data_length = packet_length;

putc(0);   
putc(0);

    while (data_length--)
   {
      vern = *packet_ptr++;
      putc(vern);
   }
}


Quote:
he first parameter to the printf function here is a function accepting a single character. Printf will then call this function in a loop for all the characters specified in the string.

if i understand correctly, this would automatically do my "while" loop listed above, provided the "putc" function was setup properly in the lcd.c routine??

one more wrench to throw in the gears, i've seen both strcpy & strNcpy used for this application....my understanding is that the strNcpy does not leave a leading 0, i'll give this a shot.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Sep 17, 2008 5:45 pm     Reply with quote

I don't see an obvious error in your code but it is not a complete working program so there might be something you left out that is important.

Can you post a complete, compilable, program showing your problem? Maximum 25 lines, including #fuses, #use rs232, etc. So we can copy / paste it into our compiler.

Also post your compiler version number (at the top of the *.lst file).

Anonymous wrote:
Quote:
he first parameter to the printf function here is a function accepting a single character. Printf will then call this function in a loop for all the characters specified in the string.

if i understand correctly, this would automatically do my "while" loop listed above, provided the "putc" function was setup properly in the lcd.c routine??
Almost... Since you are using the serial port you can use putc() as it is, no modifications required. And yes, it will rid you of the send_packet function.

This should work
Code:
#use rs232  <your setup>
void main()
{
  printf(putc, "\0\0Hello World          ");

  while(1);
}


The lcd_putc in lcd.c is using four parallel lines to communicate with the lcd. I mentioned it because it shows the power of this mechanism and is an example of how you can add more functionality like escape sequences for clearing the screen, etc.
Guest








PostPosted: Wed Sep 17, 2008 8:40 pm     Reply with quote

here's the code...in its current state, it displays hello world correctly. Too test out the STRCPY anamolies, add comments t the packet_buffer array and remove comments where noted.
Code:

#include "C:\Projects\0714\main.h"
#include <string.h>
#include <stdio.h>
#use rs232(stream=serial, baud=115200,parity=N,xmit=PIN_C7,rcv=PIN_C6,bits=8,ERRORS)

void send_packet(int* packet_ptr, int packet_length);
char packet_buffer [30];

void main ()
{
while(TRUE)                                 
   {
//strcpy(packet_buffer, "Hello World          ");     

//buffer array
        packet_buffer[0] = 0;       
        packet_buffer[1] = 0;
        packet_buffer[2] = 'H';
        packet_buffer[3] = 'e';
        packet_buffer[4] = 'l';
        packet_buffer[5] = 'l';
        packet_buffer[6] = 'o';   
      packet_buffer[7] = ' ';
      packet_buffer[8] = 'W';
      packet_buffer[9] = 'o';
      packet_buffer[10] = 'r';
      packet_buffer[11] = 'l';
      packet_buffer[12] = 'd';
      packet_buffer[13] = ' ';
      packet_buffer[14] = ' ';
      packet_buffer[15] = ' ';
      packet_buffer[16] = ' ';
      packet_buffer[17] = ' ';
      packet_buffer[18] = ' ';
      packet_buffer[19] = ' ';
      packet_buffer[20] = ' ';
      packet_buffer[21] = ' ';

      send_packet(packet_buffer, 22);   
    }
}

void send_packet(int* packet_ptr, int packet_length)
{
int data_length;
char vern;
data_length = packet_length;

putc(31);   //Command to LCD Write DATA
putc(22);   //Packet Length
//putc(0);   //USED W/ STRCPY COMMAND ONLY, X CURSOR POSITION
//putc(0);  //USED W/ STRCPY COMMAND ONLY, Y CURSOR POSITION
    while (data_length--)
   {
      vern = *packet_ptr++;
      putc(vern);
   }
putc(14);   //LSB CRC
putc(229);  //MSB CRC
}

The LCD requires a CRC calculation to be sent w/ the last word of the packet....these are the known values given to me by the vendor, crc code looks like another appetizing can of worms, one thing at a time here.
Guest








PostPosted: Wed Sep 17, 2008 8:41 pm     Reply with quote

ccs version 4.016
drdelphi



Joined: 22 Apr 2007
Posts: 22
Location: Romania

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Sep 17, 2008 11:10 pm     Reply with quote

When you use strcpy, call send_packet(packet_buffer, 20) instead of 22 because you send the two '0's manually. Calling with 22 will send two more bytes which will be interpreted by the display as the crc. This might be the issue.
On the other hand, I don't think it's a good idea to use '\0' since you are working with null terminated strings. Probably you'll end up having a 0-length string.
And the last thing ... what pic are you using ? I'm asking because I noticed that your rs232 is set to use C6 as RX and C7 as TX. Usually they are reversed for the hardware uart. C6 is TX and C7 is RX for 18F...

good luck
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Sep 18, 2008 1:21 am     Reply with quote

drdelphi wrote:
On the other hand, I don't think it's a good idea to use '\0' since you are working with null terminated strings. Probably you'll end up having a 0-length string.
True! I feel ashamed I made such a basic error. Embarassed

guest wrote:
ccs version 4.016
Don't use this version!
Check the long sticky thread on top of this forum about the new V4 compiler. In short: the first releases were of terrible quality and anything before v4.030 will for sure give you problems.
Revert to the stable v3.249 available for download at the time you bought this compiler or update to a recent version. If you forgot to download v3.249 contact CCS support.
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