RS232 Programming for Visual C++ 2010

This is a discussion on RS232 Programming for Visual C++ 2010 within the C Programming forums, part of the General Programming Boards category; I am working on a project and I'm trying to fiure out how I'm going to write to the serial ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    2

    RS232 Programming for Visual C++ 2010

    I am working on a project and I'm trying to fiure out how I'm going to write to the serial port. Since my laptop does not have a serial port, I have purchased a usb to serial adapter. Now I need to send data from my C program to the serial port but I do not know how to address the port.
    I tried using this: RS232 serial port library for Linux and Windows

    However, I am getting compiler errors:
    "'PortDCB' : undeclared identifier " among others.
    (I am using Windows 7 64 bit and I am programming in Visual C++ 2010)Any ideas why or how I can solve this?
    (Full list of errors below)


    Thanks


    Errors:


    1>------ Build started: Project: CarSimulation, Configuration: Debug Win32 ------
    1> serial.c
    1> rs232.c
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(250): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(252): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(254): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(256): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(258): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(260): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(262): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(264): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(266): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(268): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(270): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(272): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(274): warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(105) : see declaration of 'strcpy'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(298): error C2275: 'DCB' : illegal use of this type as an expression
    1> c:\program files (x86)\microsoft sdks\windows\v7.0a\include\winbase.h(747) : see declaration of 'DCB'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(298): error C2146: syntax error : missing ';' before identifier 'port_settings'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(298): error C2065: 'port_settings' : undeclared identifier
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(299): error C2065: 'port_settings' : undeclared identifier
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(299): error C2065: 'port_settings' : undeclared identifier
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(300): error C2065: 'port_settings' : undeclared identifier
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(300): error C2224: left of '.DCBlength' must have struct/union type
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(300): error C2065: 'port_settings' : undeclared identifier
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(302): error C2065: 'port_settings' : undeclared identifier
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(302): warning C4133: 'function' : incompatible types - from 'int *' to 'LPDCB'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(309): error C2065: 'port_settings' : undeclared identifier
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(309): warning C4133: 'function' : incompatible types - from 'int *' to 'LPDCB'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(318): error C2275: 'COMMTIMEOUTS' : illegal use of this type as an expression
    1> c:\program files (x86)\microsoft sdks\windows\v7.0a\include\winbase.h(755) : see declaration of 'COMMTIMEOUTS'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(318): error C2146: syntax error : missing ';' before identifier 'Cptimeouts'
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(318): error C2065: 'Cptimeouts' : undeclared identifier
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(320): error C2065: 'Cptimeouts' : undeclared identifier
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(320): error C2224: left of '.ReadIntervalTimeout' must have struct/union type
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(321): error C2065: 'Cptimeouts' : undeclared identifier
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(321): error C2224: left of '.ReadTotalTimeoutMultiplier' must have struct/union type
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(322): error C2065: 'Cptimeouts' : undeclared identifier
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(322): error C2224: left of '.ReadTotalTimeoutConstant' must have struct/union type
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(323): error C2065: 'Cptimeouts' : undeclared identifier
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(323): error C2224: left of '.WriteTotalTimeoutMultiplier' must have struct/union type
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(324): error C2065: 'Cptimeouts' : undeclared identifier
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(324): error C2224: left of '.WriteTotalTimeoutConstant' must have struct/union type
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(326): error C2065: 'Cptimeouts' : undeclared identifier
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.c(326): warning C4133: 'function' : incompatible types - from 'int *' to 'LPCOMMTIMEOUTS'
    1> car.c
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.h(69): warning C4028: formal parameter 1 different from declaration
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.h(69): warning C4028: formal parameter 2 different from declaration
    1>c:\users\user\documents\visual studio 2010\projects\gapt\tests\carsimulation\carsimulati on\rs232.h(69): error C2371: 'cprintf' : redefinition; different basic types
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\conio.h(121) : see declaration of 'cprintf'
    1> Generating Code...
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I suspect you may want to decide whether you want to do this in C++ or in C. You are currently bumping into all sorts of issues due to the fact that you are compiling as C++.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    if you want to use the library, you need to show us the code that is causing these errors.

    you can get rid of the strcpy warnings by adding _CRT_SECURE_NO_WARNINGS to your compiler preprocessor definitions settings.

    second, you don't really need to use a library. the serial communications functions in Windows are not that difficult and you learn something by using them rather than hiding behind a library

    to initialize a serial port:
    CreateFile
    GetCommState
    SetCommState (to set baud rate,parity etc)
    GetCommTimeouts
    SetCommTimeouts (to set how read and write timeouts are handled. you can turn them off)

    then ReadFile and WriteFile

    don't use overlapped I/O at first. its more complicated so start without it.

    these are all documented in the VS2010 help. bring up help and search for 'serial communications'

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    2
    I have realized that the compiler I am using is not right since apparently the c file and the header file were written for MinGW. I have tried to download it but so far I am still confused. If I can work on my Visual Studio c file I would prefer it.

    Here is the code:
    Code:
    rs232.h:
    
    /*
    ***************************************************************************
    *
    * Author: Teunis van Beelen
    *
    * Copyright (C) 2005, 2006, 2007, 2008, 2009 Teunis van Beelen
    *
    * teuniz@gmail.com
    *
    ***************************************************************************
    *
    * This program is free software; you can redistribute it and/or modify
    * it under the terms of the GNU General Public License as published by
    * the Free Software Foundation version 2 of the License.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    * GNU General Public License for more details.
    *
    * You should have received a copy of the GNU General Public License along
    * with this program; if not, write to the Free Software Foundation, Inc.,
    * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    *
    ***************************************************************************
    *
    * This version of GPL is at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
    *
    ***************************************************************************
    */
    
    
    
    
    
    
    #ifndef rs232_INCLUDED
    #define rs232_INCLUDED
    
    
    #ifdef __cplusplus
    extern "C" {
    #endif
    
    
    #include <stdio.h>
    #include <string.h>
    
    
    
    
    
    
    #ifdef __linux__
    
    
    #include <termios.h>
    #include <sys/ioctl.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <limits.h>
    
    
    #else
    
    
    #include <windows.h>
    #include <WinDef.h>
    #include <io.h>
    
    
    #endif
    
    
    int OpenComport(int, int);
    int PollComport(int, unsigned char *, int);
    int SendByte(int, unsigned char);
    int SendBuf(int, unsigned char *, int);
    void CloseComport(int);
    void cprintf(int, const char *);
    int IsCTSEnabled(int);
    
    
    
    
    #ifdef __cplusplus
    } /* extern "C" */
    #endif
    
    
    #endif
    rs232.c:

    Code:
    #include "rs232.h"
    #include <stdio.h>
    //#include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <dos.h>
    #include <windows.h>
    #include <winbase.h>
    #include <WinDef.h>
    #include <io.h>
    
    
    
    
    
    
    #ifdef __linux__   /* Linux */
    
    
    
    
    int Cport[22],
        error;
    
    
    struct termios new_port_settings,
           old_port_settings[22];
    
    
    char comports[22][13]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2","/dev/ttyS3","/dev/ttyS4","/dev/ttyS5",
                           "/dev/ttyS6","/dev/ttyS7","/dev/ttyS8","/dev/ttyS9","/dev/ttyS10","/dev/ttyS11",
                           "/dev/ttyS12","/dev/ttyS13","/dev/ttyS14","/dev/ttyS15","/dev/ttyUSB0",
                           "/dev/ttyUSB1","/dev/ttyUSB2","/dev/ttyUSB3","/dev/ttyUSB4","/dev/ttyUSB5"};
    
    
    
    
    int OpenComport(int comport_number, int baudrate)
    {
      int baudr;
    
    
      if((comport_number>21)||(comport_number<0))
      {
        printf("illegal comport number\n");
        return(1);
      }
    
    
      switch(baudrate)
      {
        case      50 : baudr = B50;
                       break;
        case      75 : baudr = B75;
                       break;
        case     110 : baudr = B110;
                       break;
        case     134 : baudr = B134;
                       break;
        case     150 : baudr = B150;
                       break;
        case     200 : baudr = B200;
                       break;
        case     300 : baudr = B300;
                       break;
        case     600 : baudr = B600;
                       break;
        case    1200 : baudr = B1200;
                       break;
        case    1800 : baudr = B1800;
                       break;
        case    2400 : baudr = B2400;
                       break;
        case    4800 : baudr = B4800;
                       break;
        case    9600 : baudr = B9600;
                       break;
        case   19200 : baudr = B19200;
                       break;
        case   38400 : baudr = B38400;
                       break;
        case   57600 : baudr = B57600;
                       break;
        case  115200 : baudr = B115200;
                       break;
        case  230400 : baudr = B230400;
                       break;
        case  460800 : baudr = B460800;
                       break;
        case  500000 : baudr = B500000;
                       break;
        case  576000 : baudr = B576000;
                       break;
        case  921600 : baudr = B921600;
                       break;
        case 1000000 : baudr = B1000000;
                       break;
        default      : printf("invalid baudrate\n");
                       return(1);
                       break;
      }
    
    
      Cport[comport_number] = open(comports[comport_number], O_RDWR | O_NOCTTY | O_NDELAY);
      if(Cport[comport_number]==-1)
      {
        perror("unable to open comport ");
        return(1);
      }
    
    
      error = tcgetattr(Cport[comport_number], old_port_settings + comport_number);
      if(error==-1)
      {
        close(Cport[comport_number]);
        perror("unable to read portsettings ");
        return(1);
      }
      memset(&new_port_settings, 0, sizeof(new_port_settings));  /* clear the new struct */
    
    
      new_port_settings.c_cflag = baudr | CS8 | CLOCAL | CREAD;
      new_port_settings.c_iflag = IGNPAR;
      new_port_settings.c_oflag = 0;
      new_port_settings.c_lflag = 0;
      new_port_settings.c_cc[VMIN] = 0;      /* block untill n bytes are received */
      new_port_settings.c_cc[VTIME] = 0;     /* block untill a timer expires (n * 100 mSec.) */
      error = tcsetattr(Cport[comport_number], TCSANOW, &new_port_settings);
      if(error==-1)
      {
        close(Cport[comport_number]);
        perror("unable to adjust portsettings ");
        return(1);
      }
    
    
      return(0);
    }
    
    
    
    
    int PollComport(int comport_number, unsigned char *buf, int size)
    {
      int n;
    
    
    #ifndef __STRICT_ANSI__                       /* __STRICT_ANSI__ is defined when the -ansi option is used for gcc */
      if(size>SSIZE_MAX)  size = (int)SSIZE_MAX;  /* SSIZE_MAX is defined in limits.h */
    #else
      if(size>4096)  size = 4096;
    #endif
    
    
      n = read(Cport[comport_number], buf, size);
    
    
      return(n);
    }
    
    
    
    
    int SendByte(int comport_number, unsigned char byte)
    {
      int n;
    
    
      n = write(Cport[comport_number], &byte, 1);
      if(n<0)  return(1);
    
    
      return(0);
    }
    
    
    
    
    int SendBuf(int comport_number, unsigned char *buf, int size)
    {
      return(write(Cport[comport_number], buf, size));
    }
    
    
    
    
    void CloseComport(int comport_number)
    {
      close(Cport[comport_number]);
      tcsetattr(Cport[comport_number], TCSANOW, old_port_settings + comport_number);
    }
    
    
    /*
    Constant  Description
    TIOCM_LE  DSR (data set ready/line enable)
    TIOCM_DTR DTR (data terminal ready)
    TIOCM_RTS RTS (request to send)
    TIOCM_ST  Secondary TXD (transmit)
    TIOCM_SR  Secondary RXD (receive)
    TIOCM_CTS CTS (clear to send)
    TIOCM_CAR DCD (data carrier detect)
    TIOCM_CD  Synonym for TIOCM_CAR
    TIOCM_RNG RNG (ring)
    TIOCM_RI  Synonym for TIOCM_RNG
    TIOCM_DSR DSR (data set ready)
    */
    
    
    int IsCTSEnabled(int comport_number)
    {
      int status;
    
    
      status = ioctl(Cport[comport_number], TIOCMGET, &status);
    
    
      if(status&TIOCM_CTS) return(1);
      else return(0);
    }
    
    
    
    
    
    
    
    
    
    
    #else         /* windows */
    
    
    
    
    HANDLE Cport[16];
    
    
    
    
    char comports[16][10]={"\\\\.\\COM1",  "\\\\.\\COM2",  "\\\\.\\COM3",  "\\\\.\\COM4",
                           "\\\\.\\COM5",  "\\\\.\\COM6",  "\\\\.\\COM7",  "\\\\.\\COM8",
                           "\\\\.\\COM9",  "\\\\.\\COM10", "\\\\.\\COM11", "\\\\.\\COM12",
                           "\\\\.\\COM13", "\\\\.\\COM14", "\\\\.\\COM15", "\\\\.\\COM16"};
    
    
    char baudr[9600];
    
    
    
    
    int OpenComport(int comport_number, int baudrate)
    {
      if((comport_number>15)||(comport_number<0))
      {
        printf("illegal comport number\n");
        return(1);
      }
    
    
      switch(baudrate)
      {
        case     110 : strcpy(baudr, "baud=110 data=8 parity=N stop=1");
                       break;
        case     300 : strcpy(baudr, "baud=300 data=8 parity=N stop=1");
                       break;
        case     600 : strcpy(baudr, "baud=600 data=8 parity=N stop=1");
                       break;
        case    1200 : strcpy(baudr, "baud=1200 data=8 parity=N stop=1");
                       break;
        case    2400 : strcpy(baudr, "baud=2400 data=8 parity=N stop=1");
                       break;
        case    4800 : strcpy(baudr, "baud=4800 data=8 parity=N stop=1");
                       break;
        case    9600 : strcpy(baudr, "baud=9600 data=8 parity=N stop=1");
                       break;
        case   19200 : strcpy(baudr, "baud=19200 data=8 parity=N stop=1");
                       break;
        case   38400 : strcpy(baudr, "baud=38400 data=8 parity=N stop=1");
                       break;
        case   57600 : strcpy(baudr, "baud=57600 data=8 parity=N stop=1");
                       break;
        case  115200 : strcpy(baudr, "baud=115200 data=8 parity=N stop=1");
                       break;
        case  128000 : strcpy(baudr, "baud=128000 data=8 parity=N stop=1");
                       break;
        case  256000 : strcpy(baudr, "baud=256000 data=8 parity=N stop=1");
                       break;
        default      : printf("invalid baudrate\n");
                       return(1);
                       break;
      }
    
    
      Cport[comport_number] = CreateFileA(comports[comport_number],
                          GENERIC_READ|GENERIC_WRITE,
                          0,                          /* no share  */
                          NULL,                       /* no security */
                          OPEN_EXISTING,
                          0,                          /* no threads */
                          NULL);                      /* no templates */
    
    
      if(Cport[comport_number]==INVALID_HANDLE_VALUE)
      {
        printf("unable to open comport\n");
        return(1);
      }
    
    
      
      
    
    
      DCB port_settings;
      memset(&port_settings, 0, sizeof(port_settings));  /* clear the new struct  */
      port_settings.DCBlength = sizeof(port_settings);
    
    
      if(!BuildCommDCBA(baudr, &port_settings))
      {
        printf("unable to set comport dcb settings\n");
        CloseHandle(Cport[comport_number]);
        return(1);
      }
    
    
      if(!SetCommState(Cport[comport_number], &port_settings))
      {
        printf("unable to set comport cfg settings\n");
        CloseHandle(Cport[comport_number]);
        return(1);
      }
    
    
    
    
      
      COMMTIMEOUTS Cptimeouts;
    
    
      Cptimeouts.ReadIntervalTimeout         = MAXDWORD;
      Cptimeouts.ReadTotalTimeoutMultiplier  = 0;
      Cptimeouts.ReadTotalTimeoutConstant    = 0;
      Cptimeouts.WriteTotalTimeoutMultiplier = 0;
      Cptimeouts.WriteTotalTimeoutConstant   = 0;
    
    
      if(!SetCommTimeouts(Cport[comport_number], &Cptimeouts))
      {
        printf("unable to set comport time-out settings\n");
        CloseHandle(Cport[comport_number]);
        return(1);
      }
    
    
      return(0);
    }
    
    
    
    
    int PollComport(int comport_number, unsigned char *buf, int size)
    {
      int n;
    
    
      if(size>4096)  size = 4096;
    
    
    /* added the void pointer cast, otherwise gcc will complain about */
    /* "warning: dereferencing type-punned pointer will break strict aliasing rules" */
    
    
      ReadFile(Cport[comport_number], buf, size, (LPDWORD)((void *)&n), NULL);
    
    
      return(n);
    }
    
    
    
    
    int SendByte(int comport_number, unsigned char byte)
    {
      int n;
    
    
      WriteFile(Cport[comport_number], &byte, 1, (LPDWORD)((void *)&n), NULL);
    
    
      if(n<0)  return(1);
    
    
      return(0);
    }
    
    
    
    
    int SendBuf(int comport_number, unsigned char *buf, int size)
    {
      int n;
    
    
      if(WriteFile(Cport[comport_number], buf, size, (LPDWORD)((void *)&n), NULL))
      {
        return(n);
      }
    
    
      return(-1);
    }
    
    
    
    
    void CloseComport(int comport_number)
    {
      CloseHandle(Cport[comport_number]);
    }
    
    
    
    
    int IsCTSEnabled(int comport_number)
    {
      int status;
    
    
      GetCommModemStatus(Cport[comport_number], (LPDWORD)((void *)&status));
    
    
      if(status&MS_CTS_ON) return(1);
      else return(0);
    }
    
    
    
    
    #endif
    
    
    
    
    void cprintf(int comport_number, const char *text)  /* sends a string to serial port */
    {
      while(*text != 0)   SendByte(comport_number, *(text++));
    }

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    in C you can't declare variables in the middle of a function. they have to be at the top. move your declarations of 'DCB portSettings' and 'COMMTIMEOUTS Cptimeout' to the beginning of the function they are in, before any executable code.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    dmh2000's response is good. Those API calls are not particularly hard to use, so just write it yourself.
    Last edited by iMalc; 03-28-2012 at 03:20 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by dmh2000 View Post
    in C you can't declare variables in the middle of a function. they have to be at the top. move your declarations of 'DCB portSettings' and 'COMMTIMEOUTS Cptimeout' to the beginning of the function they are in, before any executable code.
    That may be true with regards to the C89 standard, I think they rectified that in C99 and you can declare them anywhere ?!
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,279
    Quote Originally Posted by claudiu View Post
    That may be true with regards to the C89 standard, I think they rectified that in C99 and you can declare them anywhere ?!
    Indeed, they did.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    that is correct, but C99 is not supported in MS Visual C/C++.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    2,560
    You may be interested in this paper on Serial Port Programming. You should be able to use this information in both C or C++.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual C++ and Visual Studio 2010
    By 03jh01 in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2010, 04:03 AM
  2. c programming in visual c++ 2010
    By sharan01 in forum C Programming
    Replies: 7
    Last Post: 09-06-2010, 02:01 PM
  3. Visual Studio 2010 - so where is it now?
    By Devils Child in forum C++ Programming
    Replies: 9
    Last Post: 04-12-2010, 12:51 PM
  4. Windows 7 RC, Visual Studio 2010 Beta, Office 2010 beta, anyone in?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-20-2009, 01:57 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21