loading
 
Problem API C++ and RS232
Anonymous
16 year
I've downloaded the API C++ source code and I've modified the main.ccp for my use. In my program I use a function for writing to serial port "COM1" the letter "w"; this function is called in my "void main" in a loop where the program read by "rr.getVariable("command", buffer, 64)" the value of my variable "command".
This is my loop:

do
{
             rr.getVariable("command", buffer,64);
                   printf("buffer: %s\n",buffer);
                   RS232(L"w");
           Sleep(500);
}
          while(buffer!="exit");


The serial port function is:


void RS232(wchar_t *comando)
{

//-------------OPEN PORT----------------------

              HANDLE myPortH= CreateFileW(
                                            port,
                                            GENERIC_WRITE,
                                            FILE_SHARE_WRITE,
                                            0,
                                            OPEN_ALWAYS,
                                            FILE_FLAG_OVERLAPPED,
                                            0);

    if (myPortH == INVALID_HANDLE_VALUE)
    {
    printf("Error: %d. \n", GetLastError());
    return;
    }
    else printf("todo bien\n");

//--------------SETUP COMMUNICATION PORT------------------

       GetCommState(myPortH,&dcb);

       dcb.BaudRate = CBR_115200;        
       dcb.ByteSize = 8;            
       dcb.fNull = false;                
       dcb.fRtsControl = RTS_CONTROL_DISABLE;        
       dcb.fParity = NOPARITY;                
       dcb.StopBits = ONESTOPBIT;                

       SetCommState(myPortH,&dcb);

//-------------WRITE PORT-----------------------------------

       WriteFileEx(myPortH,comando,wcslen(comando),&o,0);
       CloseHandle(myPortH);
       return;        
}

When my program entry in my do-while loop it crashes with this error debug: "exception not managed to 0x00000000 in RRealm2.exe: 0xC0000005: access violation to the read 0x00000000.

Debug report point to RR_API::read(......) method in "RR_API.cpp":

int RR_API::read(int hSocket, unsigned char *buffer, int len)
{
    struct timeval    tim;
    fd_set fds;

    do
    {
        FD_ZERO(&fds);
        FD_SET(hSocket, &fds);

        tim.tv_sec = (timeout/1000);
        tim.tv_usec = (timeout%1000)*10;

breakpoint error--->    if (select(1024, &fds, NULL, NULL, &tim)<=0)
        {
            return(-2);
        }
    }
    while (!(FD_ISSET(hSocket, &fds)));

  return recv(hSocket, (char *)buffer, len, NULL);
}

Can help me someone?? Sorry for my bad english, I'm italian.
Anonymous 16 year
First thing is to try and isolate what is causing that error. Can you try to remove the RS232 code and run the program and see if you get the same error?

Also ... does this actually compile? The statement

while(buffer!="exit");

should be more like

while (stricmp(buffer, "exit")!=0);

unless buffer is not a char?? If so then you might have some issues once buffer is attempted to be read into.

STeven.
Anonymous 16 year
About the statement, I've seen that with my condition "while(buffer!="exit"); " my program not recognize the setting of my variable "command"....thanks for the tips.

I've tried to eliminate RS232 code and my program work properly. That error appear when I put the called function "RS232(wchar_t *command);" in a loop, but only if roborealm is connect otherwise it's all ok!
If I change the "timeout" in a API server option in roborealm then it work proprerly some time.

I post my .robo file.......

Thanks a lot for your help.

program.robo
Anonymous 16 year
Luca,

I would try changing FILE_FLAG_OVERLAPPED to FILE_ATTRIBUTE_NORMAL and see if that helps. The Overlapped causes a asynchronous write which will affect a select statement.

Also, you may want to open and initialize the COM port once and keep the port open during the life of your program instead of opening and closing it every loop iteration.

Also, are you aware of the serial module in RoboRealm? You can do everything that you are doing with the API directly in RoboRealm.

STeven.
Anonymous 16 year
Thanks a lot STeven for your  precise reply. I've already tried with serial port module of RoboRealm but the port cause some noise to my extern device (PIC16f628 with servo motor) because I think that the program dialog with port continuously.

I've tried to change in "FILE_ATTRIBUTE_NORMAL " but nothing is change

I've tried to implemente two functions as you said me; once for setting the COM1 port and once for write in, but the error remains. I've also tried to call the "write" function in my loop without "rr.getVariable" and it's all ok. Perhaps this problem is caused by TELNET communication. I don't undestand...

This forum thread has been closed due to inactivity (more than 4 months) or number of replies (more than 50 messages). Please start a New Post and enter a new forum thread with the appropriate title.

 New Post   Forum Index