0
votes

I'm implementing a simple protocol to do file transfer between 2 PC's over serial port and I'm getting a weird error.

On the main I call a function "llopen":

int 
llopen(int port, int type) {

    int     fd = 0;
    char*   PORT;


    PORT = malloc( sizeof(char) * (strlen(COM) + 1) );
    sprintf(PORT,"%s%d",COM,port);

    fd = initialization(PORT); // Open(...): returns a file descriptor!

    switch(type) {
            case SENDER:
                    return connectSender(fd);
                    break;
            case RECEIVER:
                    return connectReceiver(fd);
                    break;
    }

    return fd; // The fd value here is 5

}

After that, I call a function llwrite(int fd, ...) to write a string to the file descriptor, but I'm getting an error: "Bad file descriptor" on llwrite(int fd, ...). If I call again the initialization(port) function, before that, it works and it writes the N bytes on the file descriptor, but if I don't it gives to me the "Bad file descriptor" error again.

Here it is the llwrite(int fd, ...) function:

int 
llwrite(int fileDescriptor, unsigned char* buffer, unsigned int length) {


    // The fd value here is 5
    return writeBuffer(fileDescriptor,buffer,length);

}

Even before the return statement if I call, for instance, the tcflush(...) function I'm getting the "Bad file descriptor" error.

Any clue? Thanks in advance!

EDIT:

The problem is solved.

llopen(...) was wrong. I was returning the number of bytes wrote on the ConnectReceiver(...) / ConnectSender(...) and not the file descriptor

Now it's right:

int 
llopen(int port, int type) {

   int     fd = 0;
   char*   PORT;


   PORT = malloc( sizeof(char) * (strlen(COM) + 1) );
   sprintf(PORT,"%s%d",COM,port);

   fd = initialization(PORT); // Open(...): returns a file descriptor!

   switch(type) {
           case SENDER:
                   if( connectSender(fd) > 0 ) return fd;
           case RECEIVER:
                   if( connectReceiver(fd) > 0 ) return fd;
   }

   return -1;

}
1
You don't allocate enough bytes for PORT, it should be at least +2 to have enough space for the port number. This is otherwise a seemingly strange mix of *nix and Windows code.Hans Passant
note that this only permits the use of port COM1 to COM9, and may fail with some usb/serial converters. you should really open \\.\COM1 (beware of escaping the backslashes properly...)Adrien Plisson
@HansPassant Yes, you're right. But the problem is not that. I mean, I can execute ConnectSender(...) and ConnectReceiver(...) and I'm getting the correct file descriptor, but then I'm trying to execute llwrite(int fd) and it's not working.joaoqalves
@AdrienPlisson It is irrelevant on the case.joaoqalves
Well, you know you are corrupting the heap and then wonder why the program isn't working correctly. Stop corrupting the heap first. Use a debugger if that doesn't help.Hans Passant

1 Answers

0
votes

There's not really enough information here, but it's worth a shot noting that you do

return connectSender(fd);
break;

The break there is dead code, since the return stops execution of the function. Perhaps you didn't mean to return?

If that's not the case try using strace to get more details about what's going on. If you're not on linux other OSes should have similar tools, such as dtruss or ktrace.