Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update Commit

- code clean-up...
- added macro COM_PORT to comlib
- moved print_portscan into comport
- removed platform specific sources (using merged my1comlib!)
  • Loading branch information...
commit 8eb874171209afb064b078a66a972cad2637530b 1 parent 17b77a3
@azman authored
View
4 makefile
@@ -1,12 +1,12 @@
# makefile for my1termu - a cross-platform serial port console interface
PROJECT = my1termu
-CONSOBJ = $(PROJECT).o my1cons.o my1comport.o my1comlib.o
+CONSOBJ = my1cons.o my1comlib.o my1comport.o $(PROJECT).o
CONSPRO = $(PROJECT)
BZ2PATH = $(HOME)/temp
TEMPPRO = my1console
-TESTOBJ = $(TEMPPRO).o my1cons.o
+TESTOBJ = my1cons.o $(TEMPPRO).o
TESTPRO = $(TEMPPRO)
DELETE = rm -rf
View
39 src/my1comlib.c
@@ -13,8 +13,8 @@
#define COM_PORT_NAME "/dev/ttyS"
#define USLEEP_FOR_FLUSH 200000
#define MASK_LFLAG (ICANON|ECHO|ECHOE|ISIG)
-//#define OPEN_DFLAG (O_RDWR|O_NOCTTY|O_NDELAY|O_NONBLOCK)
-#define OPEN_DFLAG (O_RDWR|O_NOCTTY)
+#define OPEN_DFLAG (O_RDWR|O_NOCTTY|O_NDELAY|O_NONBLOCK)
+//#define OPEN_DFLAG (O_RDWR|O_NOCTTY)
#define INVALID_HANDLE_VALUE -1
#endif
//------------------------------------------------------------------------------
@@ -22,7 +22,7 @@ void initialize_serial(ASerialPort_t* aPort)
{
// reset handler/flag
aPort->mHandle = INVALID_HANDLE_VALUE;
- aPort->mPortIndex = -1;
+ aPort->mPortIndex = INVALID_PORT_INDEX;
sprintf(aPort->mPortName,"%s",COM_PORT_NAME);
#ifdef DO_MINGW
// configure 9600,N81 for COM
@@ -52,12 +52,12 @@ void initialize_serial(ASerialPort_t* aPort)
#endif
}
//------------------------------------------------------------------------------
-inline int serial_ready(ASerialPort_t* aPort)
+int serial_ready(ASerialPort_t* aPort)
{
- return aPort->mPortIndex!=-1? 1 : 0;
+ return aPort->mPortIndex==INVALID_PORT_INDEX? 0 : 1;
}
//------------------------------------------------------------------------------
-inline int serial_opened(ASerialPort_t* aPort)
+int serial_opened(ASerialPort_t* aPort)
{
return aPort->mHandle==INVALID_HANDLE_VALUE? 0 : 1;
}
@@ -66,7 +66,8 @@ int open_serial(ASerialPort_t* aPort)
{
char cCommPort[MAX_COM_CHAR];
// only if not opened and ready!
- if(aPort->mHandle==INVALID_HANDLE_VALUE&&aPort->mPortIndex!=-1)
+ if(aPort->mHandle==INVALID_HANDLE_VALUE
+ &&aPort->mPortIndex!=INVALID_PORT_INDEX)
{
sprintf(cCommPort,"%s%d",aPort->mPortName,aPort->mPortIndex);
#ifdef DO_MINGW
@@ -143,10 +144,9 @@ int find_serial(ASerialPort_t* aPort, int *pCount)
if(aPort->mHandle!=INVALID_HANDLE_VALUE)
return -1; // port opened!
-
for(cTest=1;cTest<=MAX_COM_PORT;cTest++)
{
- if(check_serial(aPort,cTest))
+ if(check_serial(aPort,COM_PORT(cTest)))
{
if(!cFirst) cFirst = cTest;
cCount++;
@@ -166,14 +166,13 @@ int check_serial(ASerialPort_t* aPort, int anIndex)
#else
long cHandle;
#endif
- if(anIndex<=0||anIndex>MAX_COM_PORT)
+ if(anIndex<COM_PORT(1)||anIndex>COM_PORT(MAX_COM_PORT))
return 0;
-#ifdef DO_MINGW
sprintf(cCommPort,"%s%d",aPort->mPortName,anIndex);
+#ifdef DO_MINGW
cHandle = CreateFile(cCommPort,GENERIC_READ | GENERIC_WRITE,
0,NULL,OPEN_EXISTING,0,NULL);
#else
- sprintf(cCommPort,"%s%d",aPort->mPortName,anIndex-1);
cHandle = open(cCommPort, OPEN_DFLAG);
#endif
if(cHandle==INVALID_HANDLE_VALUE)
@@ -190,22 +189,14 @@ int check_serial(ASerialPort_t* aPort, int anIndex)
int set_serial(ASerialPort_t* aPort, int anIndex)
{
if(!check_serial(aPort,anIndex))
- return 0;
-#ifdef DO_MINGW
+ return INVALID_PORT_INDEX;
aPort->mPortIndex = anIndex;
-#else
- aPort->mPortIndex = anIndex-1;
-#endif
- return get_serial(aPort);
+ return aPort->mPortIndex;
}
//------------------------------------------------------------------------------
int get_serial(ASerialPort_t* aPort)
{
-#ifdef DO_MINGW
return aPort->mPortIndex;
-#else
- return aPort->mPortIndex+1;
-#endif
}
//------------------------------------------------------------------------------
int set_serialconfig(ASerialPort_t* aPort, ASerialCfg_t* aConfig)
@@ -480,7 +471,11 @@ void flush_serial(ASerialPort_t* aPort)
//------------------------------------------------------------------------------
void wait_outgoing(ASerialPort_t* aPort)
{
+#ifdef DO_MINGW
+ return;
+#else
tcdrain(aPort->mHandle);
+#endif
}
//------------------------------------------------------------------------------
int check_incoming(ASerialPort_t* aPort)
View
4 src/my1comlib.h
@@ -7,10 +7,14 @@
//------------------------------------------------------------------------------
#ifdef DO_MINGW
#include <windows.h> // win api for com terminal
+#define COM_PORT(X) (X)
#else
#include <termios.h> // terminal control structure
+#define COM_PORT(X) (X-1)
#endif
//------------------------------------------------------------------------------
+#define INVALID_PORT_INDEX -1
+//------------------------------------------------------------------------------
typedef unsigned char byte_t;
//------------------------------------------------------------------------------
typedef struct __ASerialCfg
View
348 src/my1comlnx.c
@@ -1,348 +0,0 @@
-//------------------------------------------------------------------------------
-#include "my1comlnx.h"
-#include <stdio.h> // ssprintf, sscanf
-#include <unistd.h> // close
-#include <fcntl.h> // OPEN_DFLAG
-#include <sys/ioctl.h> // FIONREAD
-//------------------------------------------------------------------------------
-#define COM_PORT_NAME "/dev/ttyS"
-//------------------------------------------------------------------------------
-#define USLEEP_FOR_FLUSH 200000
-#define MASK_LFLAG (ICANON|ECHO|ECHOE|ISIG)
-#define OPEN_DFLAG (O_RDWR|O_NOCTTY|O_NDELAY|O_NONBLOCK)
-#define INVALID_HANDLE_VALUE -1
-//------------------------------------------------------------------------------
-void initialize_serial(ASerialPort_t* aPort)
-{
- // reset handler/flag
- aPort->mHandle = INVALID_HANDLE_VALUE;
- aPort->mPortIndex = -1;
- sprintf(aPort->mPortName,"%s",COM_PORT_NAME);
-
- // default baud rate
- speed_t cBaudRate = B9600;
- // define raw settings?
- aPort->mTIO.c_iflag = 0; // raw input?
- aPort->mTIO.c_oflag = 0; // raw output?
- aPort->mTIO.c_lflag &= ~MASK_LFLAG; // raw line?
- aPort->mTIO.c_cflag = 0; // clear everything first
- // configure 9600,N81 for ttyS
- cfsetispeed(&aPort->mTIO, cBaudRate);
- cfsetospeed(&aPort->mTIO, cBaudRate);
- //aPort->mTIO.c_cflag |= PARENB; // enable parity
- //aPort->mTIO.c_cflag |= PARODD; // use odd-parity, else even
- //aPort->mTIO.c_cflag |= CSTOPB; // 2 stop bit, else 1
- aPort->mTIO.c_cflag &= ~CSIZE; // clear size
- aPort->mTIO.c_cflag |= CS8; // set 8-bit char
- aPort->mTIO.c_cflag |= CLOCAL; // ignore modem control
- aPort->mTIO.c_cflag |= CREAD; // enable receiver
-}
-//------------------------------------------------------------------------------
-inline int serial_ready(ASerialPort_t* aPort)
-{
- return aPort->mPortIndex!=-1? 1 : 0;
-}
-//------------------------------------------------------------------------------
-inline int serial_opened(ASerialPort_t* aPort)
-{
- return aPort->mHandle==INVALID_HANDLE_VALUE? 0 : 1;
-}
-//------------------------------------------------------------------------------
-int open_serial(ASerialPort_t* aPort)
-{
- char cCommPort[MAX_COM_CHAR];
-
- // only if not opened and ready!
- if(aPort->mHandle==INVALID_HANDLE_VALUE&&aPort->mPortIndex!=-1)
- {
- sprintf(cCommPort,"%s%d",aPort->mPortName,aPort->mPortIndex);
- aPort->mHandle = open(cCommPort, OPEN_DFLAG);
- if(aPort->mHandle==INVALID_HANDLE_VALUE)
- return 0;
- // save current attributes
- if(tcgetattr(aPort->mHandle, &aPort->mTIOsave)<0)
- {
- close(aPort->mHandle);
- aPort->mHandle = INVALID_HANDLE_VALUE;
- return 0;
- }
- // set desired attributes
- if(tcsetattr(aPort->mHandle, TCSANOW, &aPort->mTIO)<0)
- {
- close(aPort->mHandle);
- aPort->mHandle = INVALID_HANDLE_VALUE;
- return 0;
- }
- }
-
- return serial_opened(aPort);
-}
-//------------------------------------------------------------------------------
-int close_serial(ASerialPort_t* aPort)
-{
- int cCheck = 1;
- if(aPort->mHandle==INVALID_HANDLE_VALUE)
- return cCheck;
- // set saved original attributes
- tcsetattr(aPort->mHandle, TCSANOW, &aPort->mTIOsave);
- cCheck = close(aPort->mHandle);
- if(cCheck)
- aPort->mHandle = INVALID_HANDLE_VALUE;
- return cCheck;
-}
-//------------------------------------------------------------------------------
-int find_serial(ASerialPort_t* aPort, int *pCount)
-{
- int cTest, cCount = 0, cFirst = 0;
-
- if(aPort->mHandle!=INVALID_HANDLE_VALUE)
- return -1; // port opened!
-
- for(cTest=1;cTest<=MAX_COM_PORT;cTest++)
- {
- if(check_serial(aPort,cTest))
- {
- if(!cFirst) cFirst = cTest;
- cCount++;
- }
- }
- if(pCount) *pCount = cCount;
-
- return cFirst;
-}
-//------------------------------------------------------------------------------
-int check_serial(ASerialPort_t* aPort, int anIndex)
-{
- char cCommPort[MAX_COM_CHAR];
- int cValid = 1;
- long cHandle;
-
- if(anIndex<=0||anIndex>MAX_COM_PORT)
- return 0;
-
- sprintf(cCommPort,"%s%d",aPort->mPortName,anIndex-1);
- cHandle = open(cCommPort, OPEN_DFLAG);
- if(cHandle==INVALID_HANDLE_VALUE)
- cValid = 0;
- else
- close(cHandle);
-
- return cValid;
-}
-//------------------------------------------------------------------------------
-int set_serial(ASerialPort_t* aPort, int anIndex)
-{
- if(!check_serial(aPort,anIndex))
- return 0;
- aPort->mPortIndex = anIndex-1;
- return get_serial(aPort);
-}
-//------------------------------------------------------------------------------
-int get_serial(ASerialPort_t* aPort)
-{
- return aPort->mPortIndex+1;
-}
-//------------------------------------------------------------------------------
-int set_serialconfig(ASerialPort_t* aPort, ASerialCfg_t* aConfig)
-{
- speed_t cBaudRate;
- tcflag_t cSizeFlag, cParityOpt;
- switch(aConfig->mBaudRate)
- {
- case 1:
- cBaudRate = B19200;
- break;
- case 2:
- cBaudRate = B38400;
- break;
- case 3:
- cBaudRate = B57600;
- break;
- case 4:
- cBaudRate = B115200;
- break;
- case 0:
- default:
- cBaudRate = B9600;
- break;
- }
- cfsetispeed(&aPort->mTIO, cBaudRate);
- cfsetospeed(&aPort->mTIO, cBaudRate);
- switch(aConfig->mByteSize)
- {
- case 5:
- cSizeFlag = CS5;
- break;
- case 6:
- cSizeFlag = CS6;
- break;
- case 7:
- cSizeFlag = CS7;
- break;
- case 8:
- default:
- cSizeFlag = CS8;
- break;
- }
- aPort->mTIO.c_cflag &= ~CSIZE; // clear size
- aPort->mTIO.c_cflag |= cSizeFlag; // set size
- switch(aConfig->mParity)
- {
- case 0:
- default:
- cParityOpt = ~PARENB & ~PARODD;
- aPort->mTIO.c_cflag &= cParityOpt;
- break;
- case 1:
- cParityOpt = PARENB | PARODD;
- aPort->mTIO.c_cflag |= cParityOpt;
- break;
- case 2:
- cParityOpt = PARENB;
- aPort->mTIO.c_cflag &= ~PARENB;
- aPort->mTIO.c_cflag |= cParityOpt;
- break;
- case 3: // mark - parity always logical 1 (-12v)
- case 4: // space - parity always logical 0 (+12v)
- // not implemented?
- cParityOpt = ~PARENB & ~PARODD;
- aPort->mTIO.c_cflag &= cParityOpt;
- break;
- }
- switch(aConfig->mStopBits)
- {
- case 2:
- aPort->mTIO.c_cflag |= CSTOPB; // 2 stop bit
- break;
- case 1: // 1.5 not implemented
- case 0:
- default:
- aPort->mTIO.c_cflag &= ~CSTOPB; // 1 stop bit
- break;
- }
- return 0;
-}
-//------------------------------------------------------------------------------
-int get_serialconfig(ASerialPort_t* aPort, ASerialCfg_t* aConfig)
-{
- speed_t cBaudRate;
- tcflag_t cSizeFlag, cParityOpt;
- cBaudRate = cfgetispeed(&aPort->mTIO);
- switch(cBaudRate)
- {
- case B19200:
- aConfig->mBaudRate = 1;
- break;
- case B38400:
- aConfig->mBaudRate = 2;
- break;
- case B57600:
- aConfig->mBaudRate = 3;
- break;
- case B115200:
- aConfig->mBaudRate = 4;
- break;
- case B9600:
- default:
- aConfig->mBaudRate = 0;
- break;
- }
- cSizeFlag = aPort->mTIO.c_cflag & CSIZE;
- switch(cSizeFlag)
- {
- case CS5:
- aConfig->mByteSize = 5;
- break;
- case CS6:
- aConfig->mByteSize = 6;
- break;
- case CS7:
- aConfig->mByteSize = 7;
- break;
- case CS8:
- default:
- aConfig->mByteSize = 8;
- break;
- }
- cParityOpt = aPort->mTIO.c_cflag & PARENB;
- if(!cParityOpt)
- {
- aConfig->mParity = 0;
- }
- else
- {
- if(aPort->mTIO.c_cflag & PARODD)
- {
- aConfig->mParity = 1;
- }
- else
- {
- aConfig->mParity = 2;
- }
- }
- switch(aPort->mTIO.c_cflag&CSTOPB)
- {
- case CSTOPB:
- aConfig->mStopBits = 2;
- break;
- default:
- aConfig->mStopBits = 0;
- break;
- }
- return 0;
-}
-//------------------------------------------------------------------------------
-void put_byte_serial(ASerialPort_t* aPort, byte_t aData)
-{
- if(aPort->mHandle==INVALID_HANDLE_VALUE)
- return;
- write(aPort->mHandle,&aData,1);
-#ifdef MY1DEBUG
- if((aData < ' ')||(aData >'~'))
- fprintf(stderr,"PutByte:0x%02x '.'\n", aData);
- else
- fprintf(stderr,"PutByte:0x%02x '%c'\n", aData, aData);
-#endif
-}
-//------------------------------------------------------------------------------
-byte_t get_byte_serial(ASerialPort_t* aPort)
-{
- byte_t cValue;
- if(aPort->mHandle==INVALID_HANDLE_VALUE)
- return 0;
- if(read(aPort->mHandle, &cValue,1)<=0)
- cValue = 0;
-#ifdef MY1DEBUG
- if((cValue < ' ')||(cValue >'~'))
- fprintf(stderr,"GetByte:0x%02x '.'\n", cValue);
- else
- fprintf(stderr,"GetByte:0x%02x '%c'\n", cValue, cValue);
-#endif
- return cValue;
-}
-//------------------------------------------------------------------------------
-void purge_serial(ASerialPort_t* aPort)
-{
- if(aPort->mHandle==INVALID_HANDLE_VALUE)
- return;
- usleep(USLEEP_FOR_FLUSH);
- tcflush(aPort->mHandle,TCIFLUSH);
-}
-//------------------------------------------------------------------------------
-void flush_serial(ASerialPort_t* aPort)
-{
- if(aPort->mHandle==INVALID_HANDLE_VALUE)
- return;
- usleep(USLEEP_FOR_FLUSH);
- tcflush(aPort->mHandle,TCOFLUSH);
-}
-//------------------------------------------------------------------------------
-int check_incoming(ASerialPort_t* aPort)
-{
- int cCount;
- if(aPort->mHandle==INVALID_HANDLE_VALUE)
- return 0;
- ioctl(aPort->mHandle, FIONREAD, &cCount); // TIOCOUTQ for output
- return cCount;
-}
-//------------------------------------------------------------------------------
View
48 src/my1comlnx.h
@@ -1,48 +0,0 @@
-//------------------------------------------------------------------------------
-#ifndef __MY1COMLNXH
-#define __MY1COMLNXH
-//------------------------------------------------------------------------------
-#define MAX_COM_PORT 16
-#define MAX_COM_CHAR 16
-//------------------------------------------------------------------------------
-#include <termios.h> // terminal control structure
-//------------------------------------------------------------------------------
-typedef unsigned char byte_t;
-//------------------------------------------------------------------------------
-typedef struct __ASerialCfg
-{
- byte_t mBaudRate; // 0-9600(default),1-19200,2-38400,3-57600,4-115200
- byte_t mByteSize; // number of bits/byte, 5-8 (default=8)
- byte_t mParity; // 0-4=no,odd,even,mark,space (default=0)
- byte_t mStopBits; // 0,1,2 = 1, 1.5, 2 (default=0, 1 NOT implemented)
-}
-ASerialCfg_t;
-//------------------------------------------------------------------------------
-typedef struct __ASerialPort
-{
- struct termios mTIO, mTIOsave;
- long mHandle;
- int mPortIndex;
- char mPortName[MAX_COM_CHAR];
-}
-ASerialPort_t;
-//------------------------------------------------------------------------------
-void initialize_serial(ASerialPort_t* aPort);
-int serial_ready(ASerialPort_t* aPort);
-int serial_opened(ASerialPort_t* aPort);
-int open_serial(ASerialPort_t* aPort);
-int close_serial(ASerialPort_t* aPort);
-int find_serial(ASerialPort_t* aPort, int *pCount);
-int check_serial(ASerialPort_t* aPort, int anIndex); // [1,MAX_COM_PORT]
-int set_serial(ASerialPort_t* aPort, int anIndex);
-int get_serial(ASerialPort_t* aPort);
-int set_serialconfig(ASerialPort_t* aPort, ASerialCfg_t* aConfig);
-int get_serialconfig(ASerialPort_t* aPort, ASerialCfg_t* aConfig);
-void put_byte_serial(ASerialPort_t* aPort, byte_t aData);
-byte_t get_byte_serial(ASerialPort_t* aPort);
-void purge_serial(ASerialPort_t* aPort);
-void flush_serial(ASerialPort_t* aPort);
-int check_incoming(ASerialPort_t* aPort);
-//------------------------------------------------------------------------------
-#endif
-//------------------------------------------------------------------------------
View
22 src/my1comport.c
@@ -5,6 +5,24 @@
//------------------------------------------------------------------------------
#define VERSIONSTR "2012.2"
#define AUTHORSTR "azman@my1matrix.net"
+//---------------------------------------------------------------------------
+void print_portscan(ASerialPort_t* aPort)
+{
+ int test, cCount = 0;
+ printf("\n--------------------\n");
+ printf("COM Port Scan Result\n");
+ printf("--------------------\n");
+ for(test=1;test<=MAX_COM_PORT;test++)
+ {
+ if(check_serial(aPort,COM_PORT(test)))
+ {
+ printf("%s%d: ",aPort->mPortName,COM_PORT(test));
+ cCount++;
+ printf("Ready.\n");
+ }
+ }
+ printf("\nDetected Port(s): %d\n\n",cCount);
+}
//------------------------------------------------------------------------------
int initialize_port(ASerialPort_t* aPort, int anIndex)
{
@@ -21,12 +39,12 @@ int prepare_port(ASerialPort_t* aPort, int anIndex)
return set_serial(aPort,anIndex);
}
//------------------------------------------------------------------------------
-inline int isready_port(ASerialPort_t* aPort)
+int isready_port(ASerialPort_t* aPort)
{
return serial_ready(aPort);
}
//------------------------------------------------------------------------------
-inline int isopened_port(ASerialPort_t* aPort)
+int isopened_port(ASerialPort_t* aPort)
{
return serial_opened(aPort);
}
View
1  src/my1comport.h
@@ -4,6 +4,7 @@
//------------------------------------------------------------------------------
#include "my1comlib.h"
//------------------------------------------------------------------------------
+void print_portscan(ASerialPort_t* aPort);
int initialize_port(ASerialPort_t* aPort, int anIndex);
int prepare_port(ASerialPort_t* aPort, int anIndex);
int isready_port(ASerialPort_t* aPort);
View
246 src/my1comw32.c
@@ -1,246 +0,0 @@
-//------------------------------------------------------------------------------
-#include "my1comw32.h"
-#include <stdio.h> // ssprintf, sscanf
-//------------------------------------------------------------------------------
-#define COM_PORT_NAME "\\\\.\\COM" // -> "\\" is ONE \ char!
-//------------------------------------------------------------------------------
-void initialize_serial(ASerialPort_t* aPort)
-{
- // reset handler/flag
- aPort->mHandle = INVALID_HANDLE_VALUE;
- aPort->mPortIndex = -1;
- sprintf(aPort->mPortName,"%s",COM_PORT_NAME);
-
- // configure 9600,N81 for COM
- aPort->mDCB.DCBlength = sizeof(DCB);
- aPort->mDCB.BaudRate = 9600;
- aPort->mDCB.ByteSize = 8;
- aPort->mDCB.Parity = 0;
- aPort->mDCB.StopBits = 0;
-}
-//------------------------------------------------------------------------------
-inline int serial_ready(ASerialPort_t* aPort)
-{
- return aPort->mPortIndex!=-1? 1 : 0;
-}
-//------------------------------------------------------------------------------
-inline int serial_opened(ASerialPort_t* aPort)
-{
- return aPort->mHandle==INVALID_HANDLE_VALUE? 0 : 1;
-}
-//------------------------------------------------------------------------------
-int open_serial(ASerialPort_t* aPort)
-{
- char cCommPort[MAX_COM_CHAR];
-
- // only if not opened and ready!
- if(aPort->mHandle==INVALID_HANDLE_VALUE&&aPort->mPortIndex!=-1)
- {
- sprintf(cCommPort,"%s%d",aPort->mPortName,aPort->mPortIndex);
- aPort->mHandle = CreateFile(cCommPort,
- GENERIC_READ|GENERIC_WRITE,
- 0,0x0,OPEN_EXISTING,0,0x0); // NON-OVERLAPPED
- if(aPort->mHandle==INVALID_HANDLE_VALUE)
- return 0;
- // save configurable params
- aPort->mDCBsave.BaudRate = aPort->mDCB.BaudRate;
- aPort->mDCBsave.ByteSize = aPort->mDCB.ByteSize;
- aPort->mDCBsave.Parity = aPort->mDCB.Parity;
- aPort->mDCBsave.StopBits = aPort->mDCB.StopBits;
- if(!GetCommState(aPort->mHandle,&aPort->mDCB))
- {
- CloseHandle(aPort->mHandle);
- aPort->mHandle = INVALID_HANDLE_VALUE;
- return 0;
- }
- // re-assign configurable params
- aPort->mDCB.BaudRate = aPort->mDCBsave.BaudRate;
- aPort->mDCB.ByteSize = aPort->mDCBsave.ByteSize;
- aPort->mDCB.Parity = aPort->mDCBsave.Parity;
- aPort->mDCB.StopBits = aPort->mDCBsave.StopBits;
- if(!SetCommState(aPort->mHandle,&aPort->mDCB))
- {
- CloseHandle(aPort->mHandle);
- aPort->mHandle = INVALID_HANDLE_VALUE;
- return 0;
- }
- }
-
- return serial_opened(aPort);
-}
-//------------------------------------------------------------------------------
-int close_serial(ASerialPort_t* aPort)
-{
- int cCheck = 1;
- if(aPort->mHandle==INVALID_HANDLE_VALUE)
- return cCheck;
- cCheck = CloseHandle(aPort->mHandle);
- if(cCheck)
- aPort->mHandle = INVALID_HANDLE_VALUE;
- return cCheck;
-}
-//------------------------------------------------------------------------------
-int find_serial(ASerialPort_t* aPort, int *pCount)
-{
- int cTest, cCount = 0, cFirst = 0;
-
- if(aPort->mHandle!=INVALID_HANDLE_VALUE)
- return -1; // port opened!
-
- for(cTest=1;cTest<=MAX_COM_PORT;cTest++)
- {
- if(check_serial(aPort,cTest))
- {
- if(!cFirst) cFirst = cTest;
- cCount++;
- }
- }
- if(pCount) *pCount = cCount;
-
- return cFirst;
-}
-//------------------------------------------------------------------------------
-int check_serial(ASerialPort_t* aPort, int anIndex)
-{
- char cCommPort[MAX_COM_CHAR];
- int cValid = 1;
- HANDLE cHandle;
-
- if(anIndex<=0||anIndex>MAX_COM_PORT)
- return 0;
-
- sprintf(cCommPort,"%s%d",aPort->mPortName,anIndex);
- cHandle = CreateFile(cCommPort,GENERIC_READ | GENERIC_WRITE,
- 0,NULL,OPEN_EXISTING,0,NULL);
- if(cHandle==INVALID_HANDLE_VALUE)
- cValid = 0;
- else
- CloseHandle(cHandle);
-
- return cValid;
-}
-//------------------------------------------------------------------------------
-int set_serial(ASerialPort_t* aPort, int anIndex)
-{
- if(!check_serial(aPort,anIndex))
- return 0;
- aPort->mPortIndex = anIndex;
- return get_serial(aPort);
-}
-//------------------------------------------------------------------------------
-int get_serial(ASerialPort_t* aPort)
-{
- return aPort->mPortIndex;
-}
-//------------------------------------------------------------------------------
-int set_serialconfig(ASerialPort_t* aPort, ASerialCfg_t* aConfig)
-{
- switch(aConfig->mBaudRate)
- {
- case 1:
- aPort->mDCB.BaudRate = 19200;
- break;
- case 2:
- aPort->mDCB.BaudRate = 38400;
- break;
- case 3:
- aPort->mDCB.BaudRate = 57600;
- break;
- case 4:
- aPort->mDCB.BaudRate = 115200;
- break;
- case 0:
- default:
- aPort->mDCB.BaudRate = 9600;
- break;
- }
- if(aConfig->mByteSize<0x9&&aConfig->mByteSize>0x3)
- aPort->mDCB.ByteSize = aConfig->mByteSize;
- if(aConfig->mParity<0x05)
- aPort->mDCB.Parity = aConfig->mParity;
- if(aConfig->mStopBits<0x03)
- aPort->mDCB.StopBits = aConfig->mStopBits;
- return 0;
-}
-//------------------------------------------------------------------------------
-int get_serialconfig(ASerialPort_t* aPort, ASerialCfg_t* aConfig)
-{
- switch(aPort->mDCB.BaudRate)
- {
- case 19200:
- aConfig->mBaudRate = 1;
- break;
- case 38400:
- aConfig->mBaudRate = 2;
- break;
- case 57600:
- aConfig->mBaudRate = 3;
- break;
- case 115200:
- aConfig->mBaudRate = 4;
- break;
- default:
- aConfig->mBaudRate = 0;
- break;
- }
- aConfig->mByteSize = aPort->mDCB.ByteSize;
- aConfig->mParity = aPort->mDCB.Parity;
- aConfig->mStopBits = aPort->mDCB.StopBits;
- return 0;
-}
-//------------------------------------------------------------------------------
-void put_byte_serial(ASerialPort_t* aPort, byte_t aData)
-{
- DWORD cDummy;
- if(aPort->mHandle==INVALID_HANDLE_VALUE)
- return;
- WriteFile(aPort->mHandle,&aData,1,&cDummy,0x0);
-#ifdef MY1DEBUG
- if((aData < ' ')||(aData >'~'))
- fprintf(stderr,"PutByte:0x%02x '.'\n", aData);
- else
- fprintf(stderr,"PutByte:0x%02x '%c'\n", aData, aData);
-#endif
-}
-//------------------------------------------------------------------------------
-byte_t get_byte_serial(ASerialPort_t* aPort)
-{
- byte_t cValue;
- DWORD cDummy;
- if(aPort->mHandle==INVALID_HANDLE_VALUE)
- return 0;
- if(!ReadFile(aPort->mHandle,&cValue,1,&cDummy,0x0))
- cValue = 0;
-#ifdef MY1DEBUG
- if((cValue < ' ')||(cValue >'~'))
- fprintf(stderr,"GetByte:0x%02x '.'\n", cValue);
- else
- fprintf(stderr,"GetByte:0x%02x '%c'\n", cValue, cValue);
-#endif
- return cValue;
-}
-//------------------------------------------------------------------------------
-void purge_serial(ASerialPort_t* aPort)
-{
- if(aPort->mHandle==INVALID_HANDLE_VALUE)
- return;
- PurgeComm(aPort->mHandle,PURGE_RXCLEAR); // clear the input buffer
-}
-//------------------------------------------------------------------------------
-void flush_serial(ASerialPort_t* aPort)
-{
- if(aPort->mHandle==INVALID_HANDLE_VALUE)
- return;
- FlushFileBuffers(aPort->mHandle); // flush the transmit buffer
-}
-//------------------------------------------------------------------------------
-int check_incoming(ASerialPort_t* aPort)
-{
- COMSTAT cTestStat;
- DWORD cDummy; // ignore errors!
- if(aPort->mHandle==INVALID_HANDLE_VALUE)
- return 0;
- ClearCommError(aPort->mHandle,&cDummy,&cTestStat);
- return cTestStat.cbInQue; // just check input queue buffer
-}
-//------------------------------------------------------------------------------
View
48 src/my1comw32.h
@@ -1,48 +0,0 @@
-//------------------------------------------------------------------------------
-#ifndef __MY1COMW32H
-#define __MY1COMW32H
-//------------------------------------------------------------------------------
-#define MAX_COM_PORT 16
-#define MAX_COM_CHAR 16
-//------------------------------------------------------------------------------
-#include <windows.h> // win api for com terminal
-//------------------------------------------------------------------------------
-typedef unsigned char byte_t;
-//------------------------------------------------------------------------------
-typedef struct __ASerialCfg
-{
- byte_t mBaudRate; // 0-9600(default),1-19200,2-38400,3-57600,4-115200
- byte_t mByteSize; // number of bits/byte, 4-8 (default=8)
- byte_t mParity; // 0-4=no,odd,even,mark,space (default=0)
- byte_t mStopBits; // 0,1,2 = 1, 1.5, 2 (default=0)
-}
-ASerialCfg_t;
-//------------------------------------------------------------------------------
-typedef struct __ASerialPort
-{
- DCB mDCB, mDCBsave;
- HANDLE mHandle;
- int mPortIndex;
- char mPortName[MAX_COM_CHAR];
-}
-ASerialPort_t;
-//------------------------------------------------------------------------------
-void initialize_serial(ASerialPort_t* aPort);
-int serial_ready(ASerialPort_t* aPort);
-int serial_opened(ASerialPort_t* aPort);
-int open_serial(ASerialPort_t* aPort);
-int close_serial(ASerialPort_t* aPort);
-int find_serial(ASerialPort_t* aPort, int *pCount);
-int check_serial(ASerialPort_t* aPort, int anIndex); // [1,MAX_COM_PORT]
-int set_serial(ASerialPort_t* aPort, int anIndex);
-int get_serial(ASerialPort_t* aPort);
-int set_serialconfig(ASerialPort_t* aPort, ASerialCfg_t* aConfig);
-int get_serialconfig(ASerialPort_t* aPort, ASerialCfg_t* aConfig);
-void put_byte_serial(ASerialPort_t* aPort, byte_t aData);
-byte_t get_byte_serial(ASerialPort_t* aPort);
-void purge_serial(ASerialPort_t* aPort);
-void flush_serial(ASerialPort_t* aPort);
-int check_incoming(ASerialPort_t* aPort);
-//------------------------------------------------------------------------------
-#endif
-//------------------------------------------------------------------------------
View
39 src/my1termu.c
@@ -35,35 +35,6 @@ void about(void)
printf(" -d | --dos : msdos terminal - send cr after lf.\n\n");
}
//---------------------------------------------------------------------------
-void scanlist(ASerialPort_t* aPort)
-{
- int test, cCount = 0;
- printf("\n-----------------------\n");
- printf("Serial Port Scan Result\n");
- printf("-----------------------\n");
- for(test=1;test<=MAX_COM_PORT;test++)
- {
- if(check_serial(aPort,test))
- {
- printf("%s%d: ",aPort->mPortName,test);
- cCount++;
- printf("Ready.\n");
- }
- }
- printf("\nDetected Ports: %d\n\n",cCount);
-}
-//---------------------------------------------------------------------------
-void check(ASerialPort_t* aPort)
-{
- int cFirst, cCount = 0;
- printf("-----------------------\n");
- printf("Serial Port Information\n");
- printf("-----------------------\n");
- cFirst = refresh_ports(aPort, &cCount);
- printf("Detected Ports: %d\n",cCount);
- printf("Prepared Port: %d\n\n",cFirst);
-}
-//---------------------------------------------------------------------------
void sendprint(ASerialPort_t* aPort, int aData, termopts_t anOpt)
{
put_byte_port(aPort,aData); // send
@@ -110,7 +81,7 @@ int main(int argc, char* argv[])
about();
return 0;
case 's':
- scanlist(&cPort);
+ print_portscan(&cPort);
return 0;
case 'f':
options |= OPT_FASTPORT; // use fast 115200
@@ -162,7 +133,7 @@ int main(int argc, char* argv[])
}
else if(!strcmp(argv[loop],"--scan"))
{
- scanlist(&cPort);
+ print_portscan(&cPort);
return 0;
}
else if(!strcmp(argv[loop],"--fast"))
@@ -202,7 +173,7 @@ int main(int argc, char* argv[])
if(!prepare_port(&cPort,terminal))
{
about();
- check(&cPort);
+ print_portscan(&cPort);
printf("\n\nCannot prepare port with name '%s'!\n\n",cPort.mPortName);
return ERROR_GENERAL;
}
@@ -370,9 +341,9 @@ int main(int argc, char* argv[])
}
else if(key==KEY_F9) // clear input/output buffer
{
- printf("\n\nFlushing input buffer!\n\n");
+ printf("\nFlushing input buffer!\n");
clear_ibuffer(&cPort);
- printf("\n\nPurging output buffer!\n\n");
+ printf("Purging output buffer!\n\n");
clear_obuffer(&cPort);
}
else if(key==KEY_F10)
Please sign in to comment.
Something went wrong with that request. Please try again.