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

arrays, strings, rs232 with integers above 128 and 0 (solved

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



Joined: 28 Jan 2014
Posts: 7

View user's profile Send private message

arrays, strings, rs232 with integers above 128 and 0 (solved
PostPosted: Sat Dec 26, 2015 9:14 am     Reply with quote

compiler ver 5.051
PIC 18F45K22

Hello,

I apologize for the subject right off as I''m not sure how to best describe what I'm trying to figure out. But, here it goes.
I'm trying to wrap my head around sending and receiving integers above 128 decimal and also a zero via the rs232 port. And then searching a received string.

After reading the manual and on this site and going over example code I have a program that sends ASCII codes via the rs232 port and then receives a response and parses the response looking a sequence. All works great.
To send I use
Code:

    fprintf(DEVICE,"TURNON");  // TURNON is the ascii code the device wants
 


Now I need to do the same except this device uses integers above 128 and also 0 decimal in the form of a packet.

The new device wants this 0xa9,0x01,0x30,0x01,0x01,0x01,0x31,0x9a to turn on
So I created the following:
Code:

   int TURNON[8] = {0xa9,0x01,0x30,0x01,0x01,0x01,0x31,0x9a};
   fprintf(DEVICE,TURNON);

this works until I needed the send the turn off code which is
0xa9,0x01,0x30,0x00,0x00,0x00,0x31,0x9a

Code:

 int TURNOFF[8] = {0xa9,0x01,0x30,0x00,0x00,0x00,0x31,0x9a};
  fprintf(DEVICE,TURNOFF);


this only sends a9 01 30 then stops I'm guessing because 0x00 is a null and terminates the array input ? or ?

I also need to read a similar sequence in and parse it. I have been receiving the data on interrupt and creating an array, I copy the array to a string then search looking for a subset in it using the strstr function.
and since the incoming packet? has various 0x00 integers which are also nulls

Can anyone help me understand how to send and receive 0x00 as part of a "packet" via the serial port and also how to receive it and parse it ? I assume I cannot use strstr to do this since I'm not searching for an ascii character.

Happy to post my receive and parsing code if that would be helpful. but didn't want to confuse things, that might be obvious with a better understanding on my part

Thanks

Tim


Last edited by 1953cub on Mon Dec 28, 2015 4:17 pm; edited 2 times in total
asmboy



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

View user's profile Send private message AIM Address

PostPosted: Sat Dec 26, 2015 9:37 am     Reply with quote

You need to study fundamental C syntax.

In the first code example,
All you are actually doing is outputting the
QUOTED TEXT string .
your slave device is actually seeing the following ( if sent back to back).

TURNONTURNOFF

NOT the content of the named arrays.

in the second , the \0 chars are a problem as you see.

try this instead
Code:
void do_on(void)
 int i=0;
 while (i<8){ putc(device,turnon[i++]);}
}

void do_off(void)
 int i=0;
 while (i<8){ putc(device,turnoff[i++]);}
}


Last edited by asmboy on Sat Dec 26, 2015 9:46 am; edited 2 times in total
1953cub



Joined: 28 Jan 2014
Posts: 7

View user's profile Send private message

PostPosted: Sat Dec 26, 2015 9:45 am     Reply with quote

Sorry, I had mistakenly typed the quotes in. I have the serial going to a PC for testing using "realterm" and set the display to hex with spaces. this is what I get when do the turn off send.

A9 01 30
gpsmikey



Joined: 16 Nov 2010
Posts: 588
Location: Kirkland, WA

View user's profile Send private message

PostPosted: Sat Dec 26, 2015 9:56 am     Reply with quote

Not sure if this is part of the problem you are seeing, but I have been bitten by it before - even if you do manage to send \0 as part of the character sequence, windows may be "helping" you by treating it as a special character. I have run into that before where it sees a CR or Newline (I forget exactly) and it appended the other part of the CR/LF to the character as it went by. Even Microsoft got bitten by that one in the past - back in the DOS days, if you created backup floppies on your system it worked great until the disk number was "0a" at which point it appended a "0D" to the character sequence which completely screwed up the backup sequence. They had to fix that one quickly Twisted Evil The \0 is definitely suspect since it is a string terminator (and you and windows may not agree on exactly what it should be doing with it when it sees it )

mikey
_________________
mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3
asmboy



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

View user's profile Send private message AIM Address

PostPosted: Sat Dec 26, 2015 10:07 am     Reply with quote

http://sourceforge.net/projects/realterm/

Realterm is KNOWN by this fellow to NOT have the behavior GPSMIKEY refers to, and of course your target device should not - as it is a device, not a computer at all, right ?
1953cub



Joined: 28 Jan 2014
Posts: 7

View user's profile Send private message

PostPosted: Sat Dec 26, 2015 5:37 pm     Reply with quote

Thanks, asmboy I made a slight change to fputc to use the DEVICE stream and that part is now working. So getting fprint out and sending the array one byte at a time to fputc works !!

Code:

void do_on(void)
 {int i=0;
 while (i<8){ fputc(turnon[i++],device);}
}


Thanks gpsmikey realterm was working correctly, setting the "display as" to Hex[space] allowed me to see the output correctly for testing. The real device is a control for a scientific instrument as asmboy mentions. But I will keep the windows issue in mind.
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