comio_win32.c 4.52 KB
Newer Older
1 2 3
/* comio_win32.c */

/* Synchronet Serial Communications I/O Library Functions for Win32 */
4 5 6 7 8 9 10

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
 *																			*
 * This library is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU Lesser General Public License		*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU Lesser General Public License for more details: lgpl.txt or	*
 * http://www.fsf.org/copyleft/lesser.html									*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "comio.h"
#include "genwrap.h"

deuce's avatar
DLL-ify  
deuce committed
41
char* COMIOCALL comVersion(char* str, size_t len)
42 43 44 45 46 47 48 49 50
{
	char revision[16];

	sscanf("$Revision$", "%*s %s", revision);

	safe_snprintf(str,len,"Synchronet Communications I/O Library for "PLATFORM_DESC" v%s", revision);
	return str;
}

deuce's avatar
DLL-ify  
deuce committed
51
COM_HANDLE COMIOCALL comOpen(const char* device)
52 53 54
{
	COM_HANDLE handle;
	COMMTIMEOUTS timeouts;
55
	DCB	dcb;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

	if((handle=CreateFile(device
		,GENERIC_READ|GENERIC_WRITE 	/* Access */
		,0								/* Share mode */
		,NULL							/* Security attributes */
		,OPEN_EXISTING					/* Create access */
		,FILE_ATTRIBUTE_NORMAL			/* File attributes */
		,NULL							/* Template */
		))==INVALID_HANDLE_VALUE)
		return COM_HANDLE_INVALID;


	if(GetCommTimeouts(handle, &timeouts)) {
		timeouts.ReadIntervalTimeout=MAXDWORD;
		timeouts.ReadTotalTimeoutMultiplier=0;
		timeouts.ReadTotalTimeoutConstant=0;		// No-wait read timeout
		timeouts.WriteTotalTimeoutMultiplier=0;
		timeouts.WriteTotalTimeoutConstant=5000;	// 5 seconds
		SetCommTimeouts(handle,&timeouts);
	}

77 78 79 80 81 82 83 84
	/* Force N-8-1 mode: */
	if(GetCommState(handle, &dcb)==TRUE) {
		dcb.ByteSize	= 8;
		dcb.Parity		= NOPARITY;
		dcb.StopBits	= ONESTOPBIT;
		SetCommState(handle, &dcb);
	}

85 86 87
	return handle;
}

deuce's avatar
DLL-ify  
deuce committed
88
BOOL COMIOCALL comClose(COM_HANDLE handle)
89 90 91 92
{
	return CloseHandle(handle);
}

deuce's avatar
DLL-ify  
deuce committed
93
long COMIOCALL comGetBaudRate(COM_HANDLE handle)
rswindell's avatar
rswindell committed
94 95 96 97 98 99 100 101 102
{
	DCB dcb;

	if(GetCommState(handle, &dcb)!=TRUE)
		return COM_ERROR;

	return dcb.BaudRate;
}

deuce's avatar
DLL-ify  
deuce committed
103
BOOL COMIOCALL comSetBaudRate(COM_HANDLE handle, unsigned long rate)
rswindell's avatar
rswindell committed
104 105 106 107 108 109 110 111 112 113 114
{
	DCB dcb;

	if(GetCommState(handle, &dcb)!=TRUE)
		return FALSE;

	dcb.BaudRate=rate;

	return SetCommState(handle, &dcb);
}

deuce's avatar
DLL-ify  
deuce committed
115
int COMIOCALL comGetModemStatus(COM_HANDLE handle)
116 117 118 119 120 121 122 123 124
{
	DWORD status=0;
	
	if(GetCommModemStatus(handle, &status))
		return status;
	else
		return COM_ERROR;
}

deuce's avatar
DLL-ify  
deuce committed
125
BOOL COMIOCALL comRaiseDTR(COM_HANDLE handle)
126 127 128 129
{
	return EscapeCommFunction(handle, SETDTR);
}

deuce's avatar
DLL-ify  
deuce committed
130
BOOL COMIOCALL comLowerDTR(COM_HANDLE handle)
131 132 133 134
{
	return EscapeCommFunction(handle, CLRDTR);
}

deuce's avatar
DLL-ify  
deuce committed
135
BOOL COMIOCALL comWriteByte(COM_HANDLE handle, BYTE ch)
136 137 138 139 140 141
{
	DWORD wr=0;

	return WriteFile(handle, &ch, sizeof(ch), &wr, NULL) && wr==sizeof(BYTE);
}

deuce's avatar
DLL-ify  
deuce committed
142
int COMIOCALL comWriteBuf(COM_HANDLE handle, const BYTE* buf, size_t buflen)
143 144 145 146 147 148 149 150 151
{
	DWORD wr=0;

	if(!WriteFile(handle, buf, buflen, &wr, NULL))
		return COM_ERROR;

	return wr;
}

deuce's avatar
DLL-ify  
deuce committed
152
int COMIOCALL comWriteString(COM_HANDLE handle, const char* str)
153 154 155 156
{
	return comWriteBuf(handle, str, strlen(str));
}

deuce's avatar
DLL-ify  
deuce committed
157
BOOL COMIOCALL comReadByte(COM_HANDLE handle, BYTE* ch)
158 159 160 161 162 163
{
	DWORD rd;

	return ReadFile(handle, ch, sizeof(BYTE), &rd, NULL) && rd==sizeof(BYTE);
}

deuce's avatar
DLL-ify  
deuce committed
164
BOOL COMIOCALL comPurgeInput(COM_HANDLE handle)
165 166 167 168
{
	return PurgeComm(handle, PURGE_RXCLEAR);
}

deuce's avatar
DLL-ify  
deuce committed
169
BOOL COMIOCALL comPurgeOutput(COM_HANDLE handle)
170 171 172
{
	return PurgeComm(handle, PURGE_TXCLEAR);
}
173