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.
Serial Port data question
Re: Serial Port data question
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
Can you simply writestring(fd, "%c", 0x18); to send out a unsigned
8 bit character ?
-Russ
Re: Serial Port data question
That doesn't work.
Syntax:
#include <stdio.h>
int sprintf( char *buffer, const char *format, ... );
Syntax:
#include <stdio.h>
int sprintf( char *buffer, const char *format, ... );
Re: Serial Port data question
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.
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.
Re: Serial Port data question
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);
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);
-
- Posts: 513
- Joined: Sat Apr 26, 2008 7:14 am
Re: Serial Port data question
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.
See the fdprintf example in c:\nburn\examples\fdprintf.