UNIX serial port won't transmit data to BS2 till after port is close
I'm trying to establish 2 way communication between my mac and the basic stamp using unix system calls to open() write() and read(). I'm using the USB BOE to transmit the data to my computer. I have had success doing the reads and writes separately but not at the same time. I would open and get a file descriptor to the device and set all the proper flags for the file descriptor but when ever I write to /dev/tty.usbserial-A4002X2A at 9600, the basic stamp doesn't actually receive the data until I close the file descriptor. I know this because I have the stamp light up an LED when it receives a byte of data through SERIN, not to mention I can see that the blue data received LED on the board iself doesn't light up until I close the connection. And because opening a new serial port connection resets the stamp, I can't open a new connection to read the reply message. Therefore it is impossible for me to send it a message and try to read it's response. I was thinking that there is some hardware buffer that isn't transmitting data until I do a close(fd) and I can't seem to find out how or why. I have even gone as far as trying to use tcflush() or ioctl() but I can't get it to work. Any suggestions or advice for a hopelessly lost fellow BS2 user? Here is my C code:

Post Edited (collegekid1) : 5/24/2008 7:33:28 PM GMT
int fileDescriptor = -1; struct termios options; const char *deviceFilePath = "/Dev/tty.usbserial-A4002X2A"; fileDescriptor = open(deviceFilePath, O_RDWR | O_NOCTTY | O_NDELAY); if (fileDescriptor == -1) { printf("Error opening serial port %s - %s(%d).\n", deviceFilePath, strerror(errno), errno); goto error; } if (fcntl(fileDescriptor, F_SETFL, 0) == -1) { printf("Error clearing O_NONBLOCK %s - %s(%d).\n", deviceFilePath, strerror(errno), errno); goto error; } if (tcgetattr(fileDescriptor, &options) == -1) { printf("Error getting tty attributes %s - %s(%d).\n", deviceFilePath, strerror(errno), errno); goto error; } cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); //using raw input options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //set character size and control flow options.c_cflag |= (CS8 | // Use 8 bit words CCTS_OFLOW | // CTS flow control of output <-----PROBLEM HERE DON'T SET HARDWARE CONTROL FLOW CRTS_IFLOW); // RTS flow control of input <-----PROBLEM HERE DON'T SET HARDWARE CONTROL FLOW //no parity options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; options.c_cflag &= ~CSIZE; //Enable the receiver and set local mode... options.c_cflag |= (CLOCAL | CREAD); //disable software flow control //options.c_iflag &= ~(IXON | IXOFF | IXANY); // Cause the new options to take effect immediately. if (tcsetattr(fileDescriptor, TCSANOW, &options) == -1) { printf("Error setting tty attributes %s - %s(%d).\n", deviceFilePath, strerror(errno), errno); goto error; } char writeBuf1[noparse][[/noparse]] = "2"; int written; if((written = write(fileDescriptor, writeBuf1, 1)) == -1) { printf("Error writing data - %s(%d).\n", strerror(errno), errno); goto error; } printf("\nBytes written: %d\n",written); printf("\nDone writing data...\n"); !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! program on the stamp hasn't received any data at this point so I can't try to read a reply message !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! close(fileDescriptor);

Post Edited (collegekid1) : 5/24/2008 7:33:28 PM GMT
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen