Benches Uncategorized

1

Opening the USB ports in C++

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hi All,

I need to open a USB port to send an encrypted text file to it. In Uni we have been doing this but instead of the USB ports we have been using the COM1 port. However I don't have a COM port on my mac so I wanted to use the USB port and a flash disk.

Here is my code at the moment:

#include <iostream>

#include <stdio.h>

#include <curses.h>

int main (int argc, char * const argv[]) { // Start of the program

#define CNTRLZ 0x9A

FILE * fp;

FILE * dp;

char FileOpenSTR[] ="\TEMPtext.txt";

char WriteAccess [] = "w";

char ReadAccess [] = "r";

char COM1 [] = "COM1";

//char USB []="";

//char FireWire[]="";

int c;

if ((fp = fopen(FileOpenSTR, ReadAccess)) == NULL) {

printf("fail to open data filen"); return 1;

}

if ((dp = fopen(COM1, WriteAccess)) == NULL) {

printf("fail to open COM portn"); return 1;

}

while ((c = fgetc(fp )) != EOF) {

fputc( c, dp);

}

fputc(CNTRLZ, dp);

fflush(dp);

fclose(fp);

return 0; // End of the program

}

The above code is to open the COM port and I'm not sure what I have to change to open the USB or FireWire ports.

Anyone help here?

Thanks,

Greg

1 reply

Sign in or join Beachball Help to reply.

  1. 0

    Dale Ranta

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    It depends upon what you are trying to do. If you have a serial device that you want to send data to, you can get a USB to serial converter and drive it from the Maicntosh through the converter using the OpenTransport or UNIX routines. If you have some special USB device connected, you use the IOKit routines to locate and write to USB devices.