segfault/infinite loop problem

Previous topic - Next topic

Julian

Ok I am pretty sure that this is a GLBasic Problem and not a C Problem so I will post this here. I am Trying to get this Serial Communication Lib to work under Linux (my Pandora to be exact). At first here is my working C code I am trying to port to GLBasic:
Code (glbasic) Select
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>

int fd1;
char buff[1];
int rd;

int main()
{
fd1=open("/dev/rfcomm0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd1 == -1 )
{
perror("open_port: Unable to open port");
}

else
{
fcntl(fd1, F_SETFL,0);
struct termios options;
tcgetattr(fd1, &options);
cfsetispeed(&options, B38400);
cfsetospeed(&options, B38400);
tcsetattr(fd1, TCSANOW, &options);
printf("Port has been sucessfully opened and %d is the file description\n",fd1);
}

while(1)
{
rd=read(fd1,buff,1);
printf("%s", buff);
}
close(fd1);
return 0;
}

This is my GLBasic Testcode:
Code (glbasic) Select
PRINT "Opening Serial Port....",0,0
SHOWSCREEN

IF comopen() = FALSE
PRINT "Failed",0,0
SHOWSCREEN
KEYWAIT
RETURN
ELSE
PRINT "Opened!",0,0
ENDIF


WHILE TRUE
PRINT readserialline$(),0,0
IF KEY(1) = 1; RETURN; ENDIF
SHOWSCREEN
WEND


FUNCTION readserialline$:
LOCAL buffer$ = ""
LOCAL line$ = ""
WHILE ASC(buffer$) <> 10
buffer$ = COM_Read$(0,1)
IF ASC(buffer$) <> 10 OR ASC(buffer$) <> 12 THEN line$ = line$ + buffer$
WEND
RETURN line$
ENDFUNCTION

FUNCTION comopen:
INIOPEN "settings.ini"
LOCAL port$  = INIGET$("GPS","COMPORT","/dev/rfcomm0")
LOCAL baud   = INTEGER(INIGET$("GPS","BAUDRATE",38400))
LOCAL parity = INTEGER(INIGET$("GPS","PARITY",0)) // 0=none, 1=odd, 2=even, 3=mark, 4=space
LOCAL datab  = INTEGER(INIGET$("GPS","DATABITS",8)) // data bits
LOCAL stop   = INTEGER(INIGET$("GPS","STOPBITS",0)) // stop bits: 0=1 (ONE) stop bit, 1=1.5 bits, 2=2 bits
// Open COM port for reading
IF COM_Open(0, port$, TRUE, FALSE) = FALSE THEN RETURN FALSE
// Set Baud rate and handshake
IF COM_SetComState(0, baud, parity, datab, stop) = FALSE THEN RETURN FALSE
RETURN TRUE
ENDFUNCTION

I can open and read the Serial Port but only if I comment out the COM_SetComState command. Here are some important parts of my modified SerialPort Lib:
Code (glbasic) Select
FUNCTION COM_Open: iSlot%, port$, bRead, bWrite
INLINE
#ifdef WIN32
[...] //not relevant
#else
return (OpenAdrPort(iSlot, port_Str.c_str()) < 0) ? FALSE:TRUE;
#endif
ENDINLINE
ENDFUNCTION

[...]

FUNCTION COM_SetComState: iSlot%, baud, parity, databits, stopbits
INLINE
    #ifdef WIN32
   [...] //not relevant
    #else
    return (SetAdrPortMode(iSlot, baud) < 0) ? FALSE:TRUE;
    //SetAdrPortMode(iSlot, baud);
    // #NEED parity and shit
    #endif
ENDINLINE
ENDFUNCTION

[...]

int OpenAdrPort(int iSlot, const char* device)
{
    DGStr sPortName = device;

    // make sure port is closed
    //CloseAdrPort(iSlot);

    g_hSerialFile[iSlot] = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
    if (g_hSerialFile[iSlot] < 0)
    {
        STDERR (CGStr("open error ")+DGNat(errno)+CGStr(" ")+CGStr(strerror(errno))+CGStr("\n"));
    }
    else
    {
    printf("%s opened\n", device);
    fcntl(g_hSerialFile[iSlot], F_SETFL,0);
    printf("file control changed\n", device);
    }
    printf("Returing file Pointer\n");
    return g_hSerialFile[iSlot];
}

[...]

int SetAdrPortMode(int iSlot, int baud)
{
if(g_hSerialFile[iSlot] != INVALID_HANDLE)
    {
    struct termios options;
    printf("read attributes\n");
tcgetattr(g_hSerialFile[iSlot], &options);
printf("set Input speed\n");
cfsetispeed(&options, B38400);
printf("set Output speed\n");
cfsetospeed(&options, B38400);
printf("set attributes\n");
tcsetattr(g_hSerialFile[iSlot], TCSANOW, &options);
return 0;
    }
    return -1;
}

This code results in a segfault. I was not able to pip out the output so I copied it from console. I attached it as log1.txt
What strange is, is that the program sets the attributes and then tries to read it again and then segfaults.
So I commented out the "cfsetospeed" and "cfsetispeed" lines (to see if I can read and write the attributes without modifying). Now the program doesn't sefault anymore but loops while setting up the serial device, returns FALSE after several attempts and I have no idea why. I was able to pipe out the bash output and attached it as log2.txt.
I have no idea where the segfault or the inifinte loop comes form. Any ideas from the more experienced programmers?

[attachment deleted by admin]

mentalthink

HI Julian, I don´t look too much your code.... But I use the COM library of Gernnot and for me runs perfectly, I use for Arduino, but under Windows... but I can tell you the library runs very fine.

Julian

I wouldn't need to modify it if it would work. The original code from the Thread I linked in Post #1 doesn't compile for the Pandora. I will try to port the C file 1:1 and I will try it with the new GLBasic V11 when I have some time for it.

Julian

Ok just a quick status update. I ported the c code from post#1 1:1 to GLBasic INLINE and that is working. I copied all the necessary (type)defs/structs/function prototypes from the c/c++ Development Tools PND I use on the Pandora so there is a chance that this is the problem with the Code from Gernot (I didn't change/checked what was already implemented).