Shop OBEX P1 Docs P2 Docs Learn Events
UNIX serial port won't transmit data to BS2 till after port is close — Parallax Forums

UNIX serial port won't transmit data to BS2 till after port is close

collegekid1collegekid1 Posts: 2
edited 2008-05-24 15:24 in BASIC Stamp
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:

    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);


confused.gif

Post Edited (collegekid1) : 5/24/2008 7:33:28 PM GMT

Comments

  • collegekid1collegekid1 Posts: 2
    edited 2008-05-24 08:36
    OMG. I can't believe how small of a change I had to make to fix this. If you look at where I'm setting the character size and control, I actually enabled HARDWARE control flow so absolutely nothing would transmit unless CTS (Clear To Send) and RTS (Request To Send) signals are exchanged. Once I took those out it my code worked like a charm. HUHHHHHHHHH. Well at least now I'm a pro at serial connection establishment since I was practically programming with system call level methods to get it to work. Kinda wish I didn't bang me head against the wall this entire week, but oh well.
  • FranklinFranklin Posts: 4,747
    edited 2008-05-24 15:24
    Thanks for posting your solution. I'm sure there will be someones forehead you will have saved with this information. If you want you can go back to the original message and edit it to make the offending code more obvious by clicking the pencil in the upper right of your original post.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
Sign In or Register to comment.