Skip to content

Instantly share code, notes, and snippets.

@berak
Created January 22, 2013 14:14
Show Gist options
  • Save berak/4594937 to your computer and use it in GitHub Desktop.
Save berak/4594937 to your computer and use it in GitHub Desktop.
//
// ------------------------------------------------- serialport.h --------------------------------------------------------
//
#ifndef __serialport_onboard__
#define __serialport_onboard__
int serialOpen (int nComPortNr, int readOnly);
int serialClose();
int serialIsOpen();
int serialSetTimeout (int iTotalReadTimeout);
int serialSetMode(int nBaud, int nBits, int nParity, int nStopp);
int serialWriteCommByte (unsigned char ucByte);
int serialSendData (const char *buffer, int iBytesToWrite);
int serialReadData (char *buffer, int iMaxCount);
#endif // __serialport_onboard__
//
// ------------------------------------------------- serialport.cpp -----------------------------------------------------
//
#include "serialport.h"
#include <windows.h>
// our handle, fuckin g global so it's only one per program !
HANDLE hComm = INVALID_HANDLE_VALUE;
// cache settings to restore after closing:
DCB dcb_alt;
COMMTIMEOUTS timeouts_alt;
//
// start with default mode/timeout settings:
//
int serialOpen (int nComPortNr, int readOnly)
{
DWORD flag = GENERIC_READ;
char szPort[15];
if (INVALID_HANDLE_VALUE != hComm) // we're open?
return (TRUE);
wsprintf (szPort, "\\\\.\\COM%d", nComPortNr);
if ( ! readOnly )
flag |= GENERIC_WRITE;
hComm = CreateFile (szPort, // COM-Port цffnen
flag, //
FILE_SHARE_READ, // (non) exclusive
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL); // non OVERLAPPED, exclusive
if (hComm == INVALID_HANDLE_VALUE)
{
MessageBox (NULL, "could not open comport !", NULL, 0);
return (FALSE);
}
return 1;
}
int serialClose()
{
int bResult;
if(INVALID_HANDLE_VALUE == hComm)
return (TRUE);
SetCommTimeouts(hComm, &timeouts_alt);
SetCommState(hComm, &dcb_alt);
bResult = CloseHandle(hComm);
hComm = INVALID_HANDLE_VALUE;
return(bResult);
}
int serialIsOpen()
{
if (INVALID_HANDLE_VALUE != hComm)
return (TRUE);
else
return (FALSE);
}
int serialSetMode(int nBaud, int nBits, int nParity, int nStopp)
{
DCB dcb;
if (INVALID_HANDLE_VALUE == hComm)
{
SetLastError(ERROR_INVALID_HANDLE);
return (FALSE);
}
ZeroMemory (&dcb, sizeof(dcb));
if (!GetCommState (hComm, &dcb))
{
close ();
MessageBox (NULL, "could not set serialMode !\nGetCommState()", NULL, 0);
return (FALSE);
}
dcb_alt = dcb;
dcb.DCBlength = sizeof(DCB);
dcb.fBinary = TRUE; // always TRUE
dcb.fParity = TRUE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_ENABLE;
dcb.fDsrSensitivity = FALSE;
dcb.fTXContinueOnXoff = TRUE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
dcb.fRtsControl = RTS_CONTROL_ENABLE;
dcb.fAbortOnError = FALSE;
dcb.wReserved = 0; // always true!
dcb.BaudRate = nBaud;
dcb.ByteSize = (BYTE)nBits;
dcb.Parity = (BYTE)nParity;
dcb.StopBits = (BYTE)nStopp;
dcb.fParity = (dcb.Parity != NOPARITY);
if (!SetCommState (hComm, &dcb))
{
// try to revert:
if (!SetCommState (hComm, &dcb_alt))
{
serialClose ();
MessageBox (NULL, "could not set COM-Port Parameter\nSetCommState()", NULL, 0);
return (FALSE);
}
return 0;
}
return 1;
}
// how long do we wait for input?
int serialSetTimeout (int iTotalReadTimeout)
{
COMMTIMEOUTS timeouts;
if (INVALID_HANDLE_VALUE == hComm)
return (TRUE);
if (!GetCommTimeouts(hComm, &timeouts_alt))
{
serialClose ();
MessageBox (NULL, "could not open comport!\nGetCommTimeouts()", NULL, 0);
return (FALSE);
}
timeouts.ReadIntervalTimeout = MAXDWORD ;
timeouts.ReadTotalTimeoutMultiplier = MAXDWORD ;
timeouts.ReadTotalTimeoutConstant = (DWORD) iTotalReadTimeout;
timeouts.WriteTotalTimeoutMultiplier = 1000;
timeouts.WriteTotalTimeoutConstant = 1000;
if (!SetCommTimeouts(hComm, &timeouts))
return (FALSE);
return(TRUE);
}
int serialWriteCommByte (unsigned char ucByte)
{
if (INVALID_HANDLE_VALUE == hComm)
{
SetLastError(ERROR_INVALID_HANDLE);
return (FALSE);
}
if (!TransmitCommChar(hComm, ucByte))
return (FALSE);
return (TRUE);
}
int serialSendData (const char *buffer, int iBytesToWrite)
{
DWORD dwBytesWritten = 0;
if (INVALID_HANDLE_VALUE == hComm)
{
SetLastError(ERROR_INVALID_HANDLE);
return (0);
}
WriteFile(hComm, buffer, iBytesToWrite, &dwBytesWritten, NULL);
return ((int) dwBytesWritten);
}
int serialReadData (char *buffer, int iMaxCount)
{
DWORD dwRead = 0;
char chRead;
int i = 0;
if (INVALID_HANDLE_VALUE == hComm)
{
SetLastError(ERROR_INVALID_HANDLE);
return (0);
}
while (ReadFile(hComm, &chRead, 1, &dwRead, NULL))
{
if (dwRead != 1)
break;
buffer[i++] = chRead;
if (i == iMaxCount)
break;
}
return (i);
}
//
// ------------------------------------------------- main / test --------------------------------------------------------
//
#include <stdio.h>
#include "serialport.h"
//
// micro serial konsole,
// stdin to com, com to stdout.
//
// compile_gcc : gcc serialport.c stest.c -o serial
// compile_cl : cl serialport.c stest.c user32.lib -o serial
void myExit()
{
printf( "... bye.\n" );
serialClose();
}
int main(int argc, char **argv)
{
char b[0xff];
int nComPortNr=1;
int nBaud=9600;
int nBits=8;
int nParity=0;
int nStopp=0;
int nTime=0;
int ro = 0;
int server = 0; // read only loop
int ok = 0;
int i=1;
for ( ; i<argc; i++ )
{
if ( ! strcmp( argv[i], "-ro" ) )
{
ro = 1;
} else
if ( ! strcmp( argv[i], "-server" ) )
{
server = 1;
} else
if ( ! strcmp( argv[i], "-port" ) )
{
nComPortNr = atoi( argv[ ++i ] );
} else
if ( ! strcmp( argv[i], "-baud" ) )
{
nBaud = atoi( argv[ ++i ] );
} else
if ( ! strcmp( argv[i], "-bits" ) )
{
nBits = atoi( argv[ ++i ] );
} else
if ( ! strcmp( argv[i], "-parity" ) )
{
nParity = atoi( argv[ ++i ] );
} else
if ( ! strcmp( argv[i], "-stopp" ) )
{
nStopp = atoi( argv[ ++i ] );
} else
if ( ! strcmp( argv[i], "-time" ) )
{
nTime = atoi( argv[ ++i ] );
}
}
// make shure, we close our port:
atexit( myExit );
ok = serialOpen( nComPortNr, ro );
if ( ! ok ) return 1;
ok = serialSetMode( nBaud, nBits, nParity, nStopp );
if ( ! ok ) return 2;
ok = serialSetTimeout( nTime );
if ( ! ok ) return 3;
printf( "... started on COM%d [%d,%d,%d,%d][%d]\n",nComPortNr,nBaud, nBits, nParity, nStopp,nTime );
while ( ok )
{
if ( server ) // read only loop
{
b[0] = 0;
i = serialReadData( b, 0xfe );
printf( "%i< %s\n", i,b );
serialSendData( "Ok.", 4 );
}
else
{
b[0]=0;
printf( "> " );
fgets( b, 0xfe, stdin );
if ( ! strncmp( b, "bye", 3 ) )
break;
serialSendData( b, strlen(b) + 1 );
b[0] = 0;
i = serialReadData( b, 0xfe );
printf( "%i< %s\n", i, b );
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment