NO

Author Topic: COM Port Communications  (Read 2662 times)

Offline daniel_bingamon

  • Member
  • *
  • Posts: 22
COM Port Communications
« on: November 04, 2015, 10:52:02 PM »
I know that something that worked in a previous version and now doesn't is always a bug.

Anyway:  When this open_com subroutine is used for opening COM ports on Pelles C 7.0 versions it runs fine, on 8.0 it can't receive anything:
Code: [Select]
//Prototype:
HANDLE open_com(int com_port, int baud, char *info_str);

HANDLE Com_id;
HANDLE Com_id_drv;

//Later on:
    Com_id_drv = open_com(Blwtbl.com, Drivebaud, Brate);
    Com_id = open_com(Sensorcom, 9600, Xrate);

//Subroutine:
HANDLE open_com(int com_port, int baud, char *info_str)
{

DCB dcb_com;
COMMTIMEOUTS comtimeout;
HANDLE port_handle = (HANDLE) -1;

  strcpy(info_str, "unintialized");
  if(com_port != 0)
  {
    sprintf(info_str, "COM%1d:", com_port);
    port_handle = CreateFile(info_str, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);

    sprintf(info_str, "COM%1d:%d,n,8,1", com_port, baud);

    if(port_handle <=(void *) 0)
    {
      MessageBox(Wsetup, "COM Port Open Error", "ERROR", MB_OK);
      //setup_event("Cannot access COM Port");
    }
    else
    {
      BuildCommDCB(info_str, &dcb_com);

      dcb_com.fOutX = 0;
      dcb_com.fInX = 0;
      dcb_com.Parity = NOPARITY;
      dcb_com.BaudRate = baud;
 
      dcb_com.ByteSize = 8;
      dcb_com.StopBits = ONESTOPBIT;
      dcb_com.fParity = 0;
 
      dcb_com.fOutxCtsFlow = 0;
      dcb_com.fOutxDsrFlow = 0;

      SetCommState(port_handle, &dcb_com);


      comtimeout.ReadIntervalTimeout = MAXDWORD;
      comtimeout.ReadTotalTimeoutMultiplier =0;
      comtimeout.ReadTotalTimeoutConstant = 0;
      comtimeout.WriteTotalTimeoutMultiplier = 0;
      comtimeout.WriteTotalTimeoutConstant = 0;
      SetCommTimeouts(port_handle, &comtimeout);
      FlushFileBuffers(port_handle);
    }
  }
  return port_handle;
}




void close_com(HANDLE com_id)
{

  if(com_id != (void *) 0)
  {
    CloseHandle(com_id);
    com_id = (void *) 0;
  }
}


//And example data request:
        sprintf(buf, "#%01d\r", 1);
        mk_ucase(buf);
        WriteFile(Com_id, buf, strlen(buf), &Written, NULL);

//And the read that fails on 8.0 but works on 7.0:
      temp = ReadFile(Com_id, buf, 256, &nReadChars, 0);

The I/O device has LED's on transmit and receive.
In both version, the LED's are showing data going out and responses coming back in.
The only difference is that with 8.0 the data does not get sensed by ReadFile.

So I assume that something in a library must be functioning differently.
« Last Edit: November 05, 2015, 09:29:11 AM by frankie »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: COM Port Communications
« Reply #1 on: November 05, 2015, 09:16:51 AM »
Check this part first, if it differs?
Code: [Select]
        sprintf(buf, "#%01d\r", 1);
        mk_ucase(buf);
Other functions are from Win32 API.
Take optimization off and look that mk_ucase(buf); if it fails.
Test it without that function.
Is that command string '#1'+CR?
If it should be '#01'+CR, formatting is wrong, should be "#%02d\r"
May the source be with you

Offline daniel_bingamon

  • Member
  • *
  • Posts: 22
Re: COM Port Communications
« Reply #2 on: November 11, 2015, 09:21:39 PM »
Tim,
Thank you for responding.  I was putting that response together at the end of the day.

The format string is an example of something generating: #1 followed <CR>
The mk_ucase make all alphabetic into capital letter, it's well time tested. 
Normally, the sprintf(buf, "#%01d\r", 1); gets a channel number for different data acquisition modules put in it, like 1, 2, 3, etc.

The external COM port device is sending a response string but the system is not hearing it when compiled under 8.0, all previous versions are working.  I downloaded the older 7.0 compiler and did a full rebuild and it ran fine.