Serial Port data question

Discussion to talk about software related topics only.
Post Reply
docaberle
Posts: 10
Joined: Tue Aug 26, 2008 2:39 pm

Serial Port data question

Post by docaberle »

I'm confused on sending data out the serial port. I'm sending data from the netburner 5270LC to a Pic16F688 chip. I currently can send and receive ASCII characters but was wondering how do I send any other data like decimal or hex numbers? The pic can receive these different types of values but it appears the 5270 can only send ASCII using
writestring( fd1, "abff");
I would like for example to have the 'ff' above sent in hex format or send data like "ab", 0xff, 127 all one right after another or something in addition to just ascii.
mirov
Posts: 2
Joined: Sat Oct 04, 2008 2:56 pm

Re: Serial Port data question

Post by mirov »

I know nothing about the writestring function, but is it like a sprintf ?
Can you simply writestring(fd, "%c", 0x18); to send out a unsigned
8 bit character ?

-Russ
docaberle
Posts: 10
Joined: Tue Aug 26, 2008 2:39 pm

Re: Serial Port data question

Post by docaberle »

That doesn't work.
Syntax:

#include <stdio.h>
int sprintf( char *buffer, const char *format, ... );
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: Serial Port data question

Post by rnixon »

It depends on what you have defined as stdio for a serial port. It is uart0 by default.

So for uart 0, you can use iprintf or printf. The iprintf adds much less code to your application because it only does integers, not floating point. Use printf if you need floating point.

If you are writing to a serial port that is not stdio, then you can use isprintf or sprintf to make an ascii string, then use writestring to send it out.

If you want to write binary data (not ascii), then you can use write(), but make sure you check the return value to ensure you wrote all the bytes.
docaberle
Posts: 10
Joined: Tue Aug 26, 2008 2:39 pm

Re: Serial Port data question

Post by docaberle »

This works. You have to set up a set up an array of char's to send and use the write function.

SerialClose( 1 );
int fd1 = OpenSerial( 1, 2400, 1, 8, eParityNone );

char mydata[5];
char *mydatapointer;
mydatapointer = &mydata[0];
mydata[0]=16;
mydata[1]=17;
write(fd1, mydatapointer, 2);
Ridgeglider
Posts: 513
Joined: Sat Apr 26, 2008 7:14 am

Re: Serial Port data question

Post by Ridgeglider »

You may also want to look at the fdprintf function (which as you might suspect) printf_s directly to an fd allowing you to avoid an snprintf followed by a write to an fd.

See the fdprintf example in c:\nburn\examples\fdprintf.
Post Reply