ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

rs232.c
Go to the documentation of this file.
00001 /*
00002  *  FreeLoader - rs232.c
00003  *
00004  *  Copyright (C) 2003  Brian Palmer  <brianp@sginet.com>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020 
00021 #include <windows.h>
00022 #include <winioctl.h>
00023 #include <tchar.h>
00024 #include <stdio.h>
00025 
00026 #include "rs232.h"
00027 
00028 
00029 HANDLE  hPortHandle = NULL;
00030 
00031 
00032 BOOL Rs232OpenPortWin32(TCHAR* CommPort)
00033 {
00034     TCHAR   PortName[MAX_PATH];
00035     DWORD   ErrorCode;
00036 
00037     // First check and make sure they don't already have the
00038     // OBD2 connection open. We don't want to open things twice.
00039     if (hPortHandle != NULL)
00040     {
00041         _tprintf(TEXT("Port handle not NULL. Must be already open. Returning FALSE...\n"));
00042         return FALSE;
00043     }
00044 
00045     _stprintf(PortName, TEXT("\\\\.\\%s"), CommPort);
00046 
00047     hPortHandle = CreateFile(PortName,
00048                             GENERIC_READ|GENERIC_WRITE,
00049                             0,
00050                             0,
00051                             OPEN_EXISTING,
00052                             0,
00053                             0);
00054 
00055     if (hPortHandle == INVALID_HANDLE_VALUE)
00056     {
00057         hPortHandle = NULL;
00058         ErrorCode = GetLastError();
00059 
00060         _tprintf(TEXT("CreateFile(\"%s\") failed. GetLastError() = %lu.\n"), PortName, ErrorCode);
00061 
00062         return FALSE;
00063     }
00064 
00065     return TRUE;
00066 }
00067 
00068 BOOL Rs232ClosePortWin32(VOID)
00069 {
00070     HANDLE  hTempPortHandle = hPortHandle;
00071 
00072     hPortHandle = NULL;
00073 
00074     if (hTempPortHandle == NULL)
00075     {
00076         return FALSE;
00077     }
00078 
00079     return CloseHandle(hTempPortHandle);
00080 }
00081 
00082 // DeviceControlString
00083 //   [in] Pointer to a null-terminated string that specifies device-control information.
00084 //   The string must have the same form as the mode command's command-line arguments.
00085 //
00086 //   For example, the following string specifies a baud rate of 1200, no parity, 8 data bits, and 1 stop bit:
00087 //   "baud=1200 parity=N data=8 stop=1"
00088 //
00089 //   The following string specifies a baud rate of 115200, no parity, 8 data bits, and 1 stop bit:
00090 //   "115200,n,8,1"
00091 //
00092 //   The device name is ignored if it is included in the string, but it must specify a valid device, as follows:
00093 //   "COM1: baud=1200 parity=N data=8 stop=1"
00094 //
00095 //   For further information on mode command syntax, refer to the end-user documentation for your operating system.
00096 BOOL Rs232ConfigurePortWin32(TCHAR* DeviceControlString)
00097 {
00098     DCB     dcb;
00099     DWORD   ErrorCode;
00100 
00101     /*if (!GetCommState(hPortHandle, &dcb))
00102     {
00103         ErrorCode = GetLastError();
00104 
00105         _tprintf(TEXT("GetCommState() failed. GetLastError() = %lu.\n"), ErrorCode);
00106 
00107         return FALSE;
00108     }
00109 
00110     dcb.BaudRate = BaudRate;
00111     dcb.ByteSize = DataBits;
00112     dcb.Parity = Parity;
00113     dcb.StopBits = StopBits;
00114     dcb.fBinary = TRUE;
00115     dcb.fDsrSensitivity = FALSE;
00116     dcb.fParity = (Parity == NOPARITY) ? FALSE : TRUE;
00117     dcb.fOutX = FALSE;
00118     dcb.fInX = FALSE;
00119     dcb.fNull = FALSE;
00120     dcb.fAbortOnError = TRUE;
00121     dcb.fOutxCtsFlow = FALSE;
00122     dcb.fOutxDsrFlow = FALSE;
00123     dcb.fDtrControl = DTR_CONTROL_DISABLE;
00124     dcb.fDsrSensitivity = FALSE;
00125     dcb.fRtsControl = RTS_CONTROL_DISABLE;
00126     dcb.fOutxCtsFlow = FALSE;
00127     dcb.fOutxCtsFlow = FALSE;*/
00128 
00129 
00130     memset(&dcb, 0, sizeof(DCB));
00131     dcb.DCBlength = sizeof(dcb);
00132     if (!BuildCommDCB(DeviceControlString, &dcb))
00133     {
00134         ErrorCode = GetLastError();
00135 
00136         _tprintf(TEXT("BuildCommDCB() failed. GetLastError() = %lu.\n"), ErrorCode);
00137 
00138         return FALSE;
00139     }
00140 
00141     if (!SetCommState(hPortHandle, &dcb))
00142     {
00143         ErrorCode = GetLastError();
00144 
00145         _tprintf(TEXT("SetCommState() failed. GetLastError() = %lu.\n"), ErrorCode);
00146 
00147         return FALSE;
00148     }
00149 
00150     // Set the timeouts
00151     if (!Rs232SetCommunicationTimeoutsWin32(MAXDWORD, 0, 0, 0, 0))
00152     {
00153         return FALSE;
00154     }
00155 
00156     return TRUE;
00157 }
00158 
00159 // Members
00160 //  ReadIntervalTimeout
00161 //   Specifies the maximum time, in milliseconds, allowed to elapse between the arrival of two characters on the communications line. During a ReadFile operation, the time period begins when the first character is received. If the interval between the arrival of any two characters exceeds this amount, the ReadFile operation is completed and any buffered data is returned. A value of zero indicates that interval time-outs are not used.
00162 //   A value of MAXDWORD, combined with zero values for both the ReadTotalTimeoutConstant and ReadTotalTimeoutMultiplier members, specifies that the read operation is to return immediately with the characters that have already been received, even if no characters have been received.
00163 //
00164 //  ReadTotalTimeoutMultiplier
00165 //   Specifies the multiplier, in milliseconds, used to calculate the total time-out period for read operations. For each read operation, this value is multiplied by the requested number of bytes to be read.
00166 //  ReadTotalTimeoutConstant
00167 //   Specifies the constant, in milliseconds, used to calculate the total time-out period for read operations. For each read operation, this value is added to the product of the ReadTotalTimeoutMultiplier member and the requested number of bytes.
00168 //   A value of zero for both the ReadTotalTimeoutMultiplier and ReadTotalTimeoutConstant members indicates that total time-outs are not used for read operations.
00169 //
00170 //  WriteTotalTimeoutMultiplier
00171 //   Specifies the multiplier, in milliseconds, used to calculate the total time-out period for write operations. For each write operation, this value is multiplied by the number of bytes to be written.
00172 //  WriteTotalTimeoutConstant
00173 //   Specifies the constant, in milliseconds, used to calculate the total time-out period for write operations. For each write operation, this value is added to the product of the WriteTotalTimeoutMultiplier member and the number of bytes to be written.
00174 //   A value of zero for both the WriteTotalTimeoutMultiplier and WriteTotalTimeoutConstant members indicates that total time-outs are not used for write operations.
00175 //
00176 // Remarks
00177 //  If an application sets ReadIntervalTimeout and ReadTotalTimeoutMultiplier to MAXDWORD and sets ReadTotalTimeoutConstant to a value greater than zero and less than MAXDWORD, one of the following occurs when the ReadFile function is called:
00178 //
00179 //   If there are any characters in the input buffer, ReadFile returns immediately with the characters in the buffer.
00180 //   If there are no characters in the input buffer, ReadFile waits until a character arrives and then returns immediately.
00181 //   If no character arrives within the time specified by ReadTotalTimeoutConstant, ReadFile times out.
00182 BOOL Rs232SetCommunicationTimeoutsWin32(DWORD ReadIntervalTimeout, DWORD ReadTotalTimeoutMultiplier, DWORD ReadTotalTimeoutConstant, DWORD WriteTotalTimeoutMultiplier, DWORD WriteTotalTimeoutConstant)
00183 {
00184     COMMTIMEOUTS    ct;
00185     DWORD           ErrorCode;
00186 
00187     if (!GetCommTimeouts(hPortHandle, &ct))
00188     {
00189         ErrorCode = GetLastError();
00190 
00191         _tprintf(TEXT("GetCommTimeouts() failed. GetLastError() = %lu.\n"), ErrorCode);
00192 
00193         return FALSE;
00194     }
00195 
00196     ct.ReadIntervalTimeout = ReadIntervalTimeout;
00197     ct.ReadTotalTimeoutConstant = ReadTotalTimeoutConstant;
00198     ct.ReadTotalTimeoutMultiplier = ReadTotalTimeoutMultiplier;
00199     ct.WriteTotalTimeoutConstant = WriteTotalTimeoutConstant;
00200     ct.WriteTotalTimeoutMultiplier = WriteTotalTimeoutMultiplier;
00201 
00202     if (!SetCommTimeouts(hPortHandle, &ct))
00203     {
00204         ErrorCode = GetLastError();
00205 
00206         _tprintf(TEXT("SetCommTimeouts() failed. GetLastError() = %lu.\n"), ErrorCode);
00207 
00208         return FALSE;
00209     }
00210 
00211     return TRUE;
00212 }
00213 
00214 BOOL Rs232ReadByteWin32(BYTE* DataByte)
00215 {
00216     DWORD   BytesRead = 0;
00217     DWORD   ErrorCode;
00218 
00219     // If ReadFile() fails then report error
00220     if (!ReadFile(hPortHandle, DataByte, 1, &BytesRead, NULL))
00221     {
00222         ErrorCode = GetLastError();
00223 
00224         _tprintf(TEXT("ReadFile() failed. GetLastError() = %lu.\n"), ErrorCode);
00225 
00226         return FALSE;
00227     }
00228 
00229     // If ReadFile() succeeds, but BytesRead isn't 1
00230     // then a timeout occurred.
00231     if (BytesRead != 1)
00232     {
00233         return FALSE;
00234     }
00235 
00236     return TRUE;
00237 }
00238 
00239 BOOL Rs232WriteByteWin32(BYTE DataByte)
00240 {
00241     DWORD   BytesWritten = 0;
00242     BOOL    Success;
00243     DWORD   ErrorCode;
00244 
00245     Success = WriteFile(hPortHandle, &DataByte, 1, &BytesWritten, NULL);
00246 
00247     if (!Success || BytesWritten != 1)
00248     {
00249         ErrorCode = GetLastError();
00250 
00251         _tprintf(TEXT("WriteFile() failed. GetLastError() = %lu.\n"), ErrorCode);
00252 
00253         return FALSE;
00254     }
00255 
00256     return TRUE;
00257 }

Generated on Sat May 26 2012 04:17:51 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.