Permalink
Browse files

Dirty Commit

- code clean-up! saving this...
- still not properly tested...
  • Loading branch information...
1 parent 7d283c0 commit 65e551be8dcfcce32ebe3ed02b429664e1ce192a @azman committed Feb 10, 2012
Showing with 106 additions and 80 deletions.
  1. +2 −2 src/my1comlib.c
  2. +3 −3 src/my1comport.c
  3. +1 −5 src/my1comport.h
  4. +100 −70 src/my1termu.c
View
@@ -23,7 +23,6 @@ void initialize_serial(ASerialPort_t* aPort)
aPort->mHandle = INVALID_HANDLE_VALUE;
aPort->mPortIndex = -1;
sprintf(aPort->mPortName,"%s",COM_PORT_NAME);
-
#ifdef DO_MINGW
// configure 9600,N81 for COM
aPort->mDCB.DCBlength = sizeof(DCB);
@@ -168,11 +167,12 @@ int check_serial(ASerialPort_t* aPort, int anIndex)
#endif
if(anIndex<=0||anIndex>MAX_COM_PORT)
return 0;
- sprintf(cCommPort,"%s%d",aPort->mPortName,anIndex);
#ifdef DO_MINGW
+ sprintf(cCommPort,"%s%d",aPort->mPortName,anIndex);
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)
View
@@ -3,20 +3,20 @@
//------------------------------------------------------------------------------
#include "my1comport.h"
//------------------------------------------------------------------------------
-#define VERSIONSTR "2011.3"
+#define VERSIONSTR "2012.2"
#define AUTHORSTR "azman@my1matrix.net"
//------------------------------------------------------------------------------
int initialize_port(ASerialPort_t* aPort, int anIndex)
{
initialize_serial(aPort);
- printf("\nSerial Port (%s) Library %s - by %s\n\n",
- aPort->mPortName, VERSIONSTR, AUTHORSTR);
if(anIndex==0) return 0;
return prepare_port(aPort,anIndex);
}
//------------------------------------------------------------------------------
int prepare_port(ASerialPort_t* aPort, int anIndex)
{
+ printf("\nSerial Port (%s) Library %s - by %s\n\n",
+ aPort->mPortName, VERSIONSTR, AUTHORSTR);
if(anIndex==0) anIndex = find_serial(aPort,0x0);
return set_serial(aPort,anIndex);
}
View
@@ -2,11 +2,7 @@
#ifndef __MY1COMPORTH
#define __MY1COMPORTH
//------------------------------------------------------------------------------
-#ifdef DO_MINGW
-#include "my1comw32.h"
-#else
-#include "my1comlnx.h"
-#endif
+#include "my1comlib.h"
//------------------------------------------------------------------------------
int initialize_port(ASerialPort_t* aPort, int anIndex);
int prepare_port(ASerialPort_t* aPort, int anIndex);
View
@@ -3,19 +3,15 @@
#include "my1cons.h"
//---------------------------------------------------------------------------
#include <stdio.h>
-#include <stdlib.h>
+#include <string.h>
//---------------------------------------------------------------------------
-#define PROGNAME "my1termu"
-#define FILENAME_LEN 80
+#define ERROR_GENERAL -1
+#define ERROR_PARAM_PORT -2
//---------------------------------------------------------------------------
-#define OPT_FASTPORT 0x00000001
#define OPT_LOCALECHO 0x00000002
#define OPT_SHOWHEX 0x00000004
#define OPT_LF4CR 0x00000008
#define OPT_RAWMODE 0x00000010
-#ifndef DO_MINGW
-#define OPT_USB2SER 0x00000100
-#endif
//---------------------------------------------------------------------------
typedef unsigned int termopts_t;
//---------------------------------------------------------------------------
@@ -24,17 +20,15 @@ void about(void)
printf("\n%s - A simple serial port (9600-8N1) interface.\n\n",PROGNAME);
printf("Use: %s [options]\n",PROGNAME);
printf("Options are:\n");
- printf(" [x] : port number between 1-%d.\n",MAX_COM_PORT);
-#ifndef DO_MINGW
- printf(" -x : Use USB2Serial Port (useful on Linux)\n");
-#endif
- printf(" -h : show this message. overrides other options.\n");
- printf(" -s : scan and list ports. overrides other options.\n");
- printf(" -f : use fast 115200-8N1 terminal settings.\n");
- printf(" -e : generate local echo.\n");
- printf(" -n : display number (hex) instead of text.\n");
- printf(" -r : raw keystroke. command function keys disabled.\n");
- printf(" -d : msdos terminal - send cr after lf.\n\n");
+ printf(" --port <number> : port number between 1-%d.\n",MAX_COM_PORT);
+ printf(" --tty <device> : alternate device name (useful in Linux).\n");
+ printf(" -h | --help : show this message. overrides other options.\n");
+ printf(" -s | --scan : scan and list ports. overrides other options.\n");
+ printf(" -f | --fast : use fast 115200-8N1 terminal settings.\n");
+ printf(" -e | --echo : generate local echo.\n");
+ printf(" -n | --num : display number (hex) instead of text.\n");
+ printf(" -r | --raw : raw keystroke. command function keys disabled.\n");
+ printf(" -d | --dos : msdos terminal - send cr after lf.\n\n");
}
//---------------------------------------------------------------------------
void scanlist(ASerialPort_t* aPort)
@@ -94,76 +88,112 @@ int main(int argc, char* argv[])
ASerialPort_t cPort;
my1key_t key;
termopts_t options = 0x0;
- int terminal = 0, test, loop, length, mask, drag;
+ int terminal = 0, test, loop, mask, drag;
char filename[FILENAME_LEN];
FILE* pfile;
- // initialize port early to use name!
+ // initialize port early to enable name change by parameter
initialize_port(&cPort,0);
// check program arguments
if(argc>1)
{
for(loop=1;loop<argc;loop++)
{
- length = 0;
- while(argv[loop][length]) length++;
if(argv[loop][0]=='-') // options!
{
- if(length!=2) // not an option!
+ if(argv[loop][1]!=0x0&&argv[loop][2]==0x0)
{
- printf("Unknown parameter %s!\n",argv[loop]);
- return -1;
+ switch(argv[loop][1])
+ {
+ case 'h':
+ about();
+ return 0;
+ case 's':
+ scanlist(&cPort);
+ return 0;
+ case 'f':
+ options |= OPT_FASTPORT; // use fast 115200
+ break;
+ case 'e':
+ options |= OPT_LOCALECHO; // local echo
+ break;
+ case 'n':
+ options |= OPT_SHOWHEX; // numbers not text
+ break;
+ case 'r':
+ options |= OPT_RAWMODE; // raw keystroke
+ break;
+ case 'd':
+ options |= OPT_LF4CR; // ms-dos lf cr
+ break;
+ default:
+ printf("Unknown option '%c'!\n",argv[loop][1]);
+ return ERROR_GENERAL;
+ }
}
- switch(argv[loop][1])
+ else if(!strcmp(argv[loop],"--port"))
{
- case 'h':
- about();
- return 0;
- case 's':
- scanlist(&cPort);
- return 0;
-#ifndef DO_MINGW
- case 'x':
- options |= OPT_USB2SER;
- sprintf(cPort.mPortName,"/dev/ttyUSB");
- break;
-#endif
- case 'f':
- options |= OPT_FASTPORT; // use fast 115200
- break;
- case 'n':
- options |= OPT_SHOWHEX; // numbers not text
- break;
- case 'e':
- options |= OPT_LOCALECHO; // local echo
- break;
- case 'd':
- options |= OPT_LF4CR; // ms-dos lf cr
- break;
- case 'r':
- options |= OPT_RAWMODE; // raw keystroke
- break;
- default:
- printf("Unknown option '%c'!\n",argv[loop][1]);
- return -1;
+ if(get_param_int(argc,argv,&loop,&test)<0)
+ {
+ printf("Cannot get port number!\n");
+ return ERROR_PARAM_PORT;
+ }
+ else if(test>MAX_COM_PORT)
+ {
+ printf("Invalid port number! (%d)\n", test);
+ return ERROR_PARAM_PORT;
+ }
+ terminal = test;
}
- }
- else
- {
- // port selection?
- test = atoi(argv[loop]);
- if(!test||test>MAX_COM_PORT)
+ else if(!strcmp(argv[loop],"--tty"))
{
- printf("Unsupported port %s!\n",argv[loop]);
- return -1;
+ if(!(ptty=get_param_str(argc,argv,&loop)))
+ {
+ printf("Error getting tty name!\n");
+ continue;
+ }
+ sprintf(cPort.mPortName,"%s",ptty);
+ }
+ else if(!strcmp(argv[loop],"--help"))
+ {
+ about();
+ return 0;
+ }
+ else if(!strcmp(argv[loop],"--scan"))
+ {
+ scanlist(&cPort);
+ return 0;
+ }
+ else if(!strcmp(argv[loop],"--fast"))
+ {
+ options |= OPT_FASTPORT; // use fast 115200
+ }
+ else if(!strcmp(argv[loop],"--echo"))
+ {
+ options |= OPT_LOCALECHO; // local echo
+ }
+ else if(!strcmp(argv[loop],"--num"))
+ {
+ options |= OPT_SHOWHEX; // numbers not text
+ }
+ else if(!strcmp(argv[loop],"--raw"))
+ {
+ options |= OPT_RAWMODE; // raw keystroke
}
- if(terminal>0)
+ else if(!strcmp(argv[loop],"--dos"))
{
- printf("Multiple ports requested [%d] & [%d]?\n",test,terminal);
- return -1;
+ options |= OPT_LF4CR; // ms-dos lf cr
}
- terminal = test;
+ else
+ {
+ printf("Unknown param '%s'!\n",argv[loop]);
+ }
+ }
+ else // not an option?
+ {
+ printf("Unknown parameter %s!\n",argv[loop]);
+ return ERROR_GENERAL;
}
}
}
@@ -173,7 +203,7 @@ int main(int argc, char* argv[])
{
about();
check(&cPort);
- return -1;
+ return ERROR_GENERAL;
}
// apply fastport option
check_fastport(&cPort,options&OPT_FASTPORT);
@@ -182,7 +212,7 @@ int main(int argc, char* argv[])
if(!open_port(&cPort))
{
printf("\n\nCannot open port '%s%d'!\n\n",cPort.mPortName,cPort.mPortIndex);
- return -1;
+ return ERROR_GENERAL;
}
// clear input buffer

0 comments on commit 65e551b

Please sign in to comment.