home *** CD-ROM | disk | FTP | other *** search
/ What PC? 1996 April / WHAT_PC_APR_96.ISO / internet / twinsock / src / comms.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-19  |  8.1 KB  |  331 lines

  1. /*
  2.  *  TwinSock - "Troy's Windows Sockets"
  3.  *
  4.  *  Copyright (C) 1994  Troy Rollo <troy@cbme.unsw.EDU.AU>
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the license in the file LICENSE.TXT included
  8.  *  with the TwinSock distribution.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  13.  */
  14. #include <windows.h>
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "twinsock.h"
  18.  
  19. extern    HINSTANCE    hinst;
  20. int    iPortChanged = 0;
  21. static    char    achStartPort[256];
  22.  
  23. static    UINT
  24. GetConfigInt(char const *pchItem, UINT nDefault)
  25. {
  26.     return GetPrivateProfileInt("Config", pchItem, nDefault, "TWINSOCK.INI");
  27. }
  28.  
  29. enum LineSpeed { B110, B300, B600, B1200, B2400, B4800, B9600,
  30.          B14400, B19200, B38400, B56000, B128000, B256000,
  31.          BBOGUS };
  32. unsigned GetSpeedIndex(enum LineSpeed lsSpeed);
  33.  
  34. void
  35. InitComm(int    idComm)
  36. {
  37.     DCB    dcb;
  38.     unsigned speed;
  39.  
  40.     dcb.Id = idComm;
  41.     speed = GetConfigInt("Speed", B19200);
  42.     dcb.BaudRate = GetSpeedIndex((enum LineSpeed) speed);
  43.     dcb.ByteSize = GetConfigInt("Databits", 8);
  44.     dcb.Parity = GetConfigInt("Parity", NOPARITY);
  45.     dcb.StopBits = GetConfigInt("StopBits", ONESTOPBIT);
  46.     dcb.RlsTimeout = GetConfigInt("RlsTimeout", 0);
  47.     dcb.CtsTimeout = GetConfigInt("CtsTimeout", 0);
  48.     dcb.DsrTimeout = GetConfigInt("DsrTimeout", 0);
  49.     dcb.fBinary = TRUE;
  50.     dcb.fRtsDisable = GetConfigInt("fRtsDisable", FALSE);
  51.     dcb.fParity = GetConfigInt("fParity", FALSE);
  52.     dcb.fOutxCtsFlow = GetConfigInt("OutxCtsFlow", TRUE);
  53.     dcb.fOutxDsrFlow = GetConfigInt("OutxDsrFlow", FALSE);
  54.     dcb.fDummy = 0;
  55.     dcb.fDtrDisable = GetConfigInt("fDtrDisable", FALSE);
  56.     dcb.fOutX = GetConfigInt("fOutX", TRUE);
  57.     dcb.fInX = GetConfigInt("fInX", FALSE);
  58.     dcb.fPeChar = 0;
  59.     dcb.fNull = 0;
  60.     dcb.fChEvt = 0;
  61.     dcb.fDtrflow = GetConfigInt("fDtrFlow", FALSE);
  62.     dcb.fRtsflow = GetConfigInt("fRtsFlow", FALSE);
  63.     dcb.fDummy2 = 0;
  64.     dcb.XonChar = '\021';
  65.     dcb.XoffChar = '\023';
  66.     dcb.XonLim = 100;
  67.     dcb.XoffLim = 900;
  68.     dcb.PeChar = 0;
  69.     dcb.EofChar = 0;
  70.     dcb.EvtChar = 0;
  71.     dcb.TxDelay = 0;
  72.  
  73.     SetCommState(&dcb);
  74. }
  75.  
  76. static    struct
  77. {
  78.     unsigned        iValue;
  79.     char           *strSpeed;
  80.     enum LineSpeed    lsSpeed;
  81. } aSpeeds[] =
  82. {
  83.     { CBR_110,    "110",    B110    },
  84.     { CBR_300,    "300",    B300    },
  85.     { CBR_600,    "600",    B600    },
  86.     { CBR_1200,    "1200",    B1200    },
  87.     { CBR_2400,    "2400",    B2400    },
  88.     { CBR_4800,    "4800",    B4800    },
  89.     { CBR_9600,    "9600",    B9600    },
  90.     { CBR_14400,    "14400",B14400    },
  91.     { CBR_19200,    "19200",B19200    },
  92.     { CBR_38400,    "38400",B38400    },
  93.     { CBR_56000,    "57600",B56000    },
  94.     { CBR_128000,    "128000",B128000},
  95.     { CBR_256000,    "256000",B256000},
  96.     { 0,        "BOGUS",BBOGUS    },
  97. };
  98.  
  99. /*
  100.  * Returns the appropriate port speed index value
  101.  * necessary to setup the COM port
  102.  */
  103. unsigned GetSpeedIndex(enum LineSpeed lsSpeed)
  104. {
  105.     int i;
  106.     
  107.     for (i = 0; aSpeeds[i].lsSpeed != BBOGUS; ++i)
  108.     {
  109.         if (lsSpeed == aSpeeds[i].lsSpeed)
  110.         break;
  111.     }
  112.     return (aSpeeds[i].iValue);
  113. }
  114.  
  115. static    char    *apchPorts[] =
  116. {
  117.     "COM1",
  118.     "COM2",
  119.     "COM3",
  120.     "COM4",
  121.     0
  122. };
  123.  
  124. char    *apchParities[] =
  125. {
  126.     "None",
  127.     "Odd",
  128.     "Even",
  129.     "Mark",
  130.     "Space",
  131.     0
  132. };
  133.  
  134. char    *apchStopBits[] =
  135. {
  136.     "1",
  137.     "1.5",
  138.     "2",
  139.     0
  140. };
  141.  
  142. #define    CE_PORT     101
  143. #define    CE_SPEED    103
  144. #define    CE_DATABITS    105
  145. #define    CE_PARITY    107
  146. #define    CE_STOPBITS    109
  147.  
  148. static    void
  149. FillCommsDialog(HWND hDlg)
  150. {
  151.     int    i;
  152.     long    iSpeedNow;
  153.     char    achTmp[100];
  154.  
  155.     GetPrivateProfileString("Config", "Port", "COM1", achTmp, 100, "TWINSOCK.INI");
  156.     SetDlgItemText(hDlg, CE_PORT, achTmp);
  157.     strcpy(achStartPort, achTmp);
  158.     for (i = 0; apchPorts[i]; i++)
  159.         SendDlgItemMessage(hDlg, CE_PORT, CB_ADDSTRING, 0, (LPARAM) apchPorts[i]);
  160.     iSpeedNow = GetPrivateProfileInt("Config", "Speed", (int) B19200, "TWINSOCK.INI");
  161.     for (i = 0; aSpeeds[i].lsSpeed != BBOGUS; ++i)
  162.     {
  163.         sprintf(achTmp, "%s", aSpeeds[i].strSpeed);    
  164.         SendDlgItemMessage(hDlg, CE_SPEED, CB_ADDSTRING, 0, (LPARAM) achTmp);
  165.         if (aSpeeds[i].lsSpeed == iSpeedNow)
  166.             SendDlgItemMessage(hDlg, CE_SPEED, CB_SETCURSEL, i, 0);
  167.     }
  168.  
  169.     for (i = 5; i <= 8; i++)
  170.     {
  171.         sprintf(achTmp, "%d", i);
  172.         SendDlgItemMessage(hDlg, CE_DATABITS, CB_ADDSTRING, 0, (LPARAM) achTmp);
  173.     }
  174.     i = GetPrivateProfileInt("Config", "DataBits", 8, "TWINSOCK.INI");
  175.     SendDlgItemMessage(hDlg, CE_DATABITS, CB_SETCURSEL, i - 5, 0);
  176.  
  177.     for (i = 0; apchParities[i]; i++)
  178.         SendDlgItemMessage(hDlg, CE_PARITY, CB_ADDSTRING, 0, (LPARAM) apchParities[i]);
  179.     i = GetPrivateProfileInt("Config", "Parity", 0, "TWINSOCK.INI");
  180.     SendDlgItemMessage(hDlg, CE_PARITY, CB_SETCURSEL, i, 0);
  181.  
  182.     for (i = 0; apchStopBits[i]; i++)
  183.         SendDlgItemMessage(hDlg, CE_STOPBITS, CB_ADDSTRING, 0, (LPARAM) apchStopBits[i]);
  184.     i = GetPrivateProfileInt("Config", "StopBits", 0, "TWINSOCK.INI");
  185.     SendDlgItemMessage(hDlg, CE_STOPBITS, CB_SETCURSEL, i, 0);
  186. }
  187.  
  188. void
  189. ReadCommsDialog(HWND hDlg)
  190. {
  191.     LRESULT    i;
  192.     char    achTemp[100];
  193.  
  194.     GetDlgItemText(hDlg, CE_PORT, achTemp, 100);
  195.     WritePrivateProfileString("Config", "Port", achTemp, "TWINSOCK.INI");
  196.     if (stricmp(achTemp, achStartPort))
  197.         iPortChanged = 1;
  198.  
  199.     i = SendDlgItemMessage(hDlg, CE_SPEED, CB_GETCURSEL, 0, 0);
  200.     i = aSpeeds[i].lsSpeed;
  201.     sprintf(achTemp, "%d", i);
  202.     WritePrivateProfileString("Config", "Speed", achTemp, "TWINSOCK.INI");
  203.  
  204.     i = SendDlgItemMessage(hDlg, CE_DATABITS, CB_GETCURSEL, 0, 0);
  205.     i += 5;
  206.     sprintf(achTemp, "%d", i);
  207.     WritePrivateProfileString("Config", "Databits", achTemp, "TWINSOCK.INI");
  208.  
  209.     i = SendDlgItemMessage(hDlg, CE_PARITY, CB_GETCURSEL, 0, 0);
  210.     sprintf(achTemp, "%d", i);
  211.     WritePrivateProfileString("Config", "Parity", achTemp, "TWINSOCK.INI");
  212.  
  213.     i = SendDlgItemMessage(hDlg, CE_STOPBITS, CB_GETCURSEL, 0, 0);
  214.     sprintf(achTemp, "%d", i);
  215.     WritePrivateProfileString("Config", "StopBits", achTemp, "TWINSOCK.INI");
  216. }
  217.  
  218. #pragma argsused
  219. BOOL    CALLBACK
  220. CommsDlgProc(    HWND    hDlg,
  221.         UINT    wMsg,
  222.         WPARAM    wParam,
  223.         LPARAM    lParam)
  224. {
  225.     switch(wMsg)
  226.     {
  227.     case WM_INITDIALOG:
  228.         FillCommsDialog(hDlg);
  229.         return TRUE;
  230.  
  231.     case WM_COMMAND:
  232.         switch(wParam)
  233.         {
  234.         case IDOK:
  235.             ReadCommsDialog(hDlg);
  236.             EndDialog(hDlg, TRUE);
  237.             break;
  238.  
  239.         case IDCANCEL:
  240.             EndDialog(hDlg, FALSE);
  241.             break;
  242.         }
  243.         break;
  244.     }
  245.     return FALSE;
  246. }
  247.  
  248. BOOL
  249. CommsEdit(HWND hwndParent)
  250. {
  251.     FARPROC    fpDlgProc;
  252.     BOOL    bStatus;
  253.  
  254.     fpDlgProc = MakeProcInstance((FARPROC) CommsDlgProc, hinst);
  255.     bStatus = DialogBox(hinst, "COMMS_DLG", hwndParent, fpDlgProc);
  256.     FreeProcInstance(fpDlgProc);
  257.     return bStatus;
  258. }
  259.  
  260.  
  261.  
  262. #define    DL_NUMBER    101
  263. #define    DL_PULSE    102
  264. #define    DL_TONE        103
  265.  
  266. #pragma argsused
  267. BOOL    CALLBACK
  268. DialDlgProc(    HWND    hDlg,
  269.         UINT    wMsg,
  270.         WPARAM    wParam,
  271.         LPARAM    lParam)
  272. {
  273.     char    achNumber[84];
  274.     int    iMethod;
  275.  
  276.     switch(wMsg)
  277.     {
  278.     case WM_INITDIALOG:
  279.         iMethod = GetPrivateProfileInt("Config", "DialingMethod", 1, "TWINSOCK.INI");
  280.         SendMessage(GetDlgItem(hDlg, iMethod ? DL_TONE : DL_PULSE), BM_SETCHECK, 1, 0);
  281.         GetPrivateProfileString("Config", "LastNumber", "", achNumber, 80, "TWINSOCK.INI");
  282.         SetDlgItemText(hDlg, DL_NUMBER, achNumber);
  283.         EnableWindow(GetDlgItem(hDlg, IDOK),
  284.             (GetDlgItemText(hDlg, DL_NUMBER, achNumber, 80) != 0));
  285.         return TRUE;
  286.  
  287.     case WM_COMMAND:
  288.         switch(wParam)
  289.         {
  290.         case IDOK:
  291.             GetDlgItemText(hDlg, DL_NUMBER, achNumber + 4, 76);
  292.             WritePrivateProfileString("Config", "LastNumber", achNumber + 4, "TWINSOCK.INI");
  293.             iMethod = SendMessage(GetDlgItem(hDlg, DL_TONE), BM_GETCHECK, 0, 0);
  294.             WritePrivateProfileString("Config", "DialingMethod", iMethod ? "1" : "0", "TWINSOCK.INI");
  295.             EndDialog(hDlg, TRUE);
  296.             strncpy(achNumber, iMethod ? "ATDT" : "ATDP", 4);
  297.             strcat(achNumber, "\r");
  298.             SendData(achNumber, strlen(achNumber));
  299.             break;
  300.  
  301.         case IDCANCEL:
  302.             EndDialog(hDlg, FALSE);
  303.             break;
  304.  
  305.         case DL_NUMBER:
  306.             EnableWindow(GetDlgItem(hDlg, IDOK),
  307.                 (GetDlgItemText(hDlg, DL_NUMBER, achNumber, 80) != 0));
  308.             break;
  309.  
  310.         }
  311.         break;
  312.     }
  313.     return FALSE;
  314. }
  315.  
  316. BOOL
  317. DialNumber(HWND hwndParent)
  318. {
  319.     FARPROC    fpDlgProc;
  320.     BOOL    bStatus;
  321.  
  322.     fpDlgProc = MakeProcInstance((FARPROC) DialDlgProc, hinst);
  323.     bStatus = DialogBox(hinst, "DIAL_DLG", hwndParent, fpDlgProc);
  324.     FreeProcInstance(fpDlgProc);
  325.     return bStatus;
  326. }
  327.  
  328.  
  329.  
  330.  
  331.