Basically, how would one take this code (written for a parallel port) and change it to make it work with a serial port?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/io.h>

#define base 0x378           /* /dev/lp0 */

main(int argc, char **argv) {                    
	int value;

	if (argc!=2)
		printf("Enter a number between 0 and 255.\n"), exit(1);

	if (sscanf(argv[1],"%i",&value)!=1)
		printf("Parameter is not a number.\n"), exit(1);

	if ((value<0) || (value>255))
		printf("Enter a number between 0 and 255.\n"), exit(1);

	if (ioperm(base,1,1))
		perror("ioperm");

	printf("Setting base %x to value %d\n", base, value);

	outb((unsigned char)value, base);
}

Small project I'm working on and I don't want to ruin any hardware. I know they have different numbers of pins and I'm not sure how to adjust it to run with a serial port.

You seem to be working with linux. If so, the best way is to avoid outb() and friends. Open /dev/ttyS0, and write to it just like to a regular file. Do not forget to configure the protocol (baud rate, parity, stop bits, etc) with tcsetattr.

See details here.

You seem to be working with linux. If so, the best way is to avoid outb() and friends. Open /dev/ttyS0, and write to it just like to a regular file. Do not forget to configure the protocol (baud rate, parity, stop bits, etc) with tcsetattr.

See details here.

Ugh. I found the code online because I didn't know how to write it myself. I'm working with Windows (VS 2010), not Linux. I'm sure that changes things entirely. Back to researching I suppose...

I'm sure that changes things entirely.

Not really. A filename surely changes (from /dev/ttyS0 to COM1), and open() becames CreateFile(). Instead of tcsetattr there is IOCTL_SERIAL_SET_DCB.

If worst comes to worst, there's always an SFU; more or less supported.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.