Programming for PSC-USB...almost there...
CBueche
Posts: 2
Hi folks,
I'm developing a C/C++ app for Mac OS X, and trying to talk to the PSC-USB.··I've downloaded and installed the FTDI drivers, and things look good.· The device is recognized and·/dev/tty.usbserial-1B1 is·created.· I've downloaded the Apple sample code for serial read/writes and it looks straightforward where it counts (read()/write()).
Initially, all I want to do is read the firmware version (cmd = "!SCVER?\r").· Using the following sample snippet for port configuration, this has worked once, but only once:
· // Confirm communications
· int iCount;
· // Does file exist?
· iFile = open(m_szFilename, O_RDWR | O_NOCTTY | O_NDELAY);
· if(iFile == -1) goto IOError;
· if(fcntl(iFile, F_SETFL, 0) == -1) goto IOError;
· // Configure port
· struct termios sSaveAttrs;
· struct termios sMyAttrs;
· if(tcgetattr(iFile, &sSaveAttrs) == -1) goto IOError;
· // Set raw input, one second timeout
· // These options are documented in the man page for termios
· // (in Terminal enter: man termios)
· sMyAttrs = sSaveAttrs;
· sMyAttrs.c_cflag |= (CLOCAL | CREAD);
· sMyAttrs.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
· sMyAttrs.c_oflag &= ~OPOST;
· sMyAttrs.c_cc[noparse][[/noparse] VMIN ] = 1;
· sMyAttrs.c_cc[noparse][[/noparse] VTIME ] = 0;
·
· /* RTS-CTS */
· sMyAttrs.c_cflag |= CRTSCTS;
· sMyAttrs.c_iflag &= ~(IXON | IXOFF | IXANY);
·
· cfsetospeed(&sMyAttrs,B2400);
· cfsetispeed(&sMyAttrs,B2400);
·
· // Set the options
· if(tcsetattr(iFile, TCSANOW, &sMyAttrs) == -1) goto IOError;
· // Send version request
· sprintf(sBuf, "!CSVER?");
· sBuf[noparse][[/noparse]7] = 0x0d;
· iCount = write(iFile, sBuf, 8);
· if(iCount != 8) goto IOError;
· memset(sBuf, 0, sizeof(sBuf));
· iCount = read(iFile, sBuf, 3);
· if(iCount != 3) goto IOError;
· // Restore port
· if(tcsetattr(iFile, TCSANOW, &sSaveAttrs) == -1) goto IOError;
Does anyone out there have better port settings than those I'm using?· Is this a question better referred to FTDI?· I confess that I haven't been through the nitty gritty of what each of these settings means, but without technical specs for what's called for, it seems that anything I come up with·would be a bit of a Smile shoot.
Thanks,
Chuck Bueche
Craniac Entertainment
San Francisco
I'm developing a C/C++ app for Mac OS X, and trying to talk to the PSC-USB.··I've downloaded and installed the FTDI drivers, and things look good.· The device is recognized and·/dev/tty.usbserial-1B1 is·created.· I've downloaded the Apple sample code for serial read/writes and it looks straightforward where it counts (read()/write()).
Initially, all I want to do is read the firmware version (cmd = "!SCVER?\r").· Using the following sample snippet for port configuration, this has worked once, but only once:
· // Confirm communications
· int iCount;
· // Does file exist?
· iFile = open(m_szFilename, O_RDWR | O_NOCTTY | O_NDELAY);
· if(iFile == -1) goto IOError;
· if(fcntl(iFile, F_SETFL, 0) == -1) goto IOError;
· // Configure port
· struct termios sSaveAttrs;
· struct termios sMyAttrs;
· if(tcgetattr(iFile, &sSaveAttrs) == -1) goto IOError;
· // Set raw input, one second timeout
· // These options are documented in the man page for termios
· // (in Terminal enter: man termios)
· sMyAttrs = sSaveAttrs;
· sMyAttrs.c_cflag |= (CLOCAL | CREAD);
· sMyAttrs.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
· sMyAttrs.c_oflag &= ~OPOST;
· sMyAttrs.c_cc[noparse][[/noparse] VMIN ] = 1;
· sMyAttrs.c_cc[noparse][[/noparse] VTIME ] = 0;
·
· /* RTS-CTS */
· sMyAttrs.c_cflag |= CRTSCTS;
· sMyAttrs.c_iflag &= ~(IXON | IXOFF | IXANY);
·
· cfsetospeed(&sMyAttrs,B2400);
· cfsetispeed(&sMyAttrs,B2400);
·
· // Set the options
· if(tcsetattr(iFile, TCSANOW, &sMyAttrs) == -1) goto IOError;
· // Send version request
· sprintf(sBuf, "!CSVER?");
· sBuf[noparse][[/noparse]7] = 0x0d;
· iCount = write(iFile, sBuf, 8);
· if(iCount != 8) goto IOError;
· memset(sBuf, 0, sizeof(sBuf));
· iCount = read(iFile, sBuf, 3);
· if(iCount != 3) goto IOError;
· // Restore port
· if(tcsetattr(iFile, TCSANOW, &sSaveAttrs) == -1) goto IOError;
Does anyone out there have better port settings than those I'm using?· Is this a question better referred to FTDI?· I confess that I haven't been through the nitty gritty of what each of these settings means, but without technical specs for what's called for, it seems that anything I come up with·would be a bit of a Smile shoot.
Thanks,
Chuck Bueche
Craniac Entertainment
San Francisco
Comments
a) Not knowing that the written data *will* get echo'ed back (Thanks, Dave Andreae), and
b) Reading the port too soon.
Adding Sleep(1) after the write() fixed everything.· Now it would be nice to dig up how to delay for a period shorter than 1 second...
A 1.5 msec delay is mentioned in the docs, but apparently that's a delay before just the first character of data becomes available.· I'm just guessing.· Not having an analyzer for the data stream, that's the best I can do.
'Hope this helps someone else!
- Chuck