Communication Program for Windows


With the permission of Mr. Yoshimura, here are the source codes for a communication program for an IBM compatible.


The following are the source codes using 32-bit API for communication programming in a Windows environment. It has been tested successfully with Morita-Shogi, Kanazawa-Shogi and Kakinoki-Shogi, but I cannot guarantee that the codes are free of bugs. You can use the codes in your program, but it is your responsibility to make your own
communication routines.
If you use the VC++ version 4.X, delete the following line in the file StdAFx.h:

#define VC_EXTRALEAN


static HANDLE hCom;
int  RS_init( )   /* Initialize Communication port */
{
    DCB           dcb ;
    COMMTIMEOUTS  CommTimeOuts ;
    if ( ( hCom = CreateFile ( "COM1",    /* Open file name as "COM1"  */
           GENERIC_READ | GENERIC_WRITE, /* Open file for read and write   */
           0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL ) )
                 == INVALID_HANDLE_VALUE   ) {
       return 1; // error
    }
    CommTimeOuts.ReadIntervalTimeout =  MAXDWORD;
    CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
    CommTimeOuts.ReadTotalTimeoutConstant = 1000;
    CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
    CommTimeOuts.WriteTotalTimeoutConstant = 1000;
    if ( !SetCommTimeouts( hCom, &CommTimeOuts )){
      CloseHandle(hCom);
      return 2; // SetCommTimeouts error
    }
    dcb.DCBlength = sizeof ( DCB ) ;
    dcb.BaudRate = 1200 ;
    dcb.fBinary = TRUE ;
    dcb.fParity = 0;
    dcb.fOutxCtsFlow = 0;         // CTS output flow control
    dcb.fOutxDsrFlow = 0;         // DSR output flow control
    dcb.fDtrControl =  1;         // DTR flow control type
    dcb.fDsrSensitivity = 0;      // DSR sensitivity
    dcb.fTXContinueOnXoff = 0;    // XOFF continues Tx
    dcb.fOutX = 1;                // XON/XOFF output flow control
    dcb.fInX = 1;                 // XON/XOFF input flow control
    dcb.fErrorChar = 0;           // enable error replacement
    dcb.fNull = 0;                // enable null stripping
    dcb.fRtsControl = 1;          // RTS flow control
    dcb.fAbortOnError = 0;        // abort reads/writes on error
    dcb.XonLim = 2048;            // transmit XON threshold
    dcb.XoffLim = 512;            // transmit XOFF threshold
    dcb.ByteSize = 8;             // number of bits/byte, 4-8
    dcb.Parity = NOPARITY;        // 0-4=no,odd,even,mark,space
    dcb.StopBits = ONESTOPBIT;    // 0,1,2 = 1, 1.5, 2
    dcb.XonChar = 17;             // Tx and Rx XON character
    dcb.XoffChar = 19;            // Tx and Rx XOFF character
    dcb.ErrorChar = 0;            // error replacement character
    dcb.EofChar = 0;              // end of input character
    dcb.EvtChar = 0;              // received event character
    if ( !SetCommState( hCom, &dcb ) ){
       CloseHandle(hCom);
        return 3;//SetCommState error
    }
    return(0);
}
void   RS_close ()                    /* Close Communication port   */
{
    CloseHandle( hCom );
}

/*
  Read one character
    when it reads a character, it returns 1
    when there is no character to read, it returns 0
*/
int RS_read(unsigned char *c)
{
       DWORD length;
       if( !ReadFile(hCom, c, 1, &length, NULL) ) return 0;
       return length;
}
/*
  Send a character
*/
void RS_send(unsigned char c)
{
       DWORD length;
       WriteFile(hCom, &c, 1, &length, NULL);
}