Skip to content
Browse files

Update Commit

- mainly, control line access have been tested!
- releasing this as 1.4.0!
  • Loading branch information...
1 parent 8bb93ff commit 45c312adfc09a1ec7bf8170a00385625fe18e9f9 @azman committed Nov 20, 2012
Showing with 202 additions and 87 deletions.
  1. +15 −0 CHANGELOG
  2. +3 −3 TODO
  3. +1 −1 VERSION
  4. +1 −1 makefile
  5. +77 −44 src/my1comlib.c
  6. +6 −6 src/my1comlib.h
  7. +38 −10 src/my1cons.h
  8. +61 −22 src/my1termu.c
View
15 CHANGELOG
@@ -1,4 +1,19 @@
**************
+my1termu-1.4.0
+**************
+
+Update:
+- featuring tested control line access on both linux & win32
+- added some ctrl+<char> key definition
+- (win32) using constants instead of value for baudrate
+
+Bugfix:
+- (linux) the local flag now cleared entirely (may caused the prev issue?)
+
+Source:
+- make will now only build my1termu by default
+
+**************
my1termu-1.3.5
**************
View
6 TODO
@@ -1,9 +1,9 @@
2012113
-- add DTR/RTS/DSR/CTS utility in my1termu
+- [DONE] add DTR/RTS/DSR/CTS utility in my1termu
20111006
-- define ALL keystrokes
-- detect SHIFT/CTRL/ALT/CAPS/NUM keys as well?
+- [NOT] define ALL keystrokes
+- [NOT] detect SHIFT/CTRL/ALT/CAPS/NUM keys as well?
20110318
- [DONE] rewrite everything to use pure c library (no c++)
View
2 VERSION
@@ -1 +1 @@
-1.3.5
+1.4.0
View
2 makefile
@@ -50,7 +50,7 @@ all: main test
test: $(TESTPRO)
-new: clean all
+new: clean main
debug: new
View
121 src/my1comlib.c
@@ -9,12 +9,21 @@
/*----------------------------------------------------------------------------*/
#ifdef DO_MINGW
#define COM_PORT_NAME "COM"
+#ifndef CBR_230400
+#define CBR_230400 230400
+#endif
+#ifndef CBR_460800
+#define CBR_460800 460800
+#endif
+#ifndef CBR_921600
+#define CBR_921600 921600
+#endif
#else
#define COM_PORT_NAME "/dev/ttyS"
#define USLEEP_FOR_FLUSH 200000
-#define MASK_LFLAG (ICANON|ECHO|ECHOE|ISIG)
+/** #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) */
#define INVALID_HANDLE_VALUE -1
#endif
/*----------------------------------------------------------------------------*/
@@ -27,7 +36,7 @@ void initialize_serial(ASerialPort_t* aPort)
#ifdef DO_MINGW
/* configure 9600,N81 for COM */
aPort->mDCB.DCBlength = sizeof(DCB);
- aPort->mDCB.BaudRate = 9600;
+ aPort->mDCB.BaudRate = CBR_9600;
aPort->mDCB.ByteSize = 8;
aPort->mDCB.Parity = 0;
aPort->mDCB.StopBits = 0;
@@ -37,7 +46,8 @@ void initialize_serial(ASerialPort_t* aPort)
/* 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_lflag = 0; /* raw local? */
+ /** aPort->mTIO.c_lflag &= ~MASK_LFLAG; / * local flag? */
aPort->mTIO.c_cflag = 0; /* clear everything first */
/* configure 9600,N81 for ttyS */
cfsetispeed(&aPort->mTIO, cBaudRate);
@@ -223,29 +233,29 @@ int set_serialconfig(ASerialPort_t* aPort, ASerialConf_t* aConfig)
switch(aConfig->mBaudRate)
{
case MY1BAUD19200:
- aPort->mDCB.BaudRate = 19200;
+ aPort->mDCB.BaudRate = CBR_19200;
break;
case MY1BAUD38400:
- aPort->mDCB.BaudRate = 38400;
+ aPort->mDCB.BaudRate = CBR_38400;
break;
case MY1BAUD57600:
- aPort->mDCB.BaudRate = 57600;
+ aPort->mDCB.BaudRate = CBR_57600;
break;
case MY1BAUD115200:
- aPort->mDCB.BaudRate = 115200;
+ aPort->mDCB.BaudRate = CBR_115200;
break;
case MY1BAUD230400:
- aPort->mDCB.BaudRate = 230400;
+ aPort->mDCB.BaudRate = CBR_230400;
break;
case MY1BAUD460800:
- aPort->mDCB.BaudRate = 460800;
+ aPort->mDCB.BaudRate = CBR_460800;
break;
case MY1BAUD921600:
- aPort->mDCB.BaudRate = 921600;
+ aPort->mDCB.BaudRate = CBR_921600;
break;
case MY1BAUD9600:
default:
- aPort->mDCB.BaudRate = 9600;
+ aPort->mDCB.BaudRate = CBR_9600;
break;
}
if(aConfig->mByteSize<0x9&&aConfig->mByteSize>0x3)
@@ -348,29 +358,29 @@ int get_serialconfig(ASerialPort_t* aPort, ASerialConf_t* aConfig)
#ifdef DO_MINGW
switch(aPort->mDCB.BaudRate)
{
- case 19200:
+ case CBR_19200:
aConfig->mBaudRate = MY1BAUD19200;
break;
- case 38400:
+ case CBR_38400:
aConfig->mBaudRate = MY1BAUD38400;
break;
- case 57600:
+ case CBR_57600:
aConfig->mBaudRate = MY1BAUD57600;
break;
- case 115200:
+ case CBR_115200:
aConfig->mBaudRate = MY1BAUD115200;
break;
- case 230400:
+ case CBR_230400:
aConfig->mBaudRate = MY1BAUD230400;
break;
- case 460800:
+ case CBR_460800:
aConfig->mBaudRate = MY1BAUD460800;
break;
- case 921600:
+ case CBR_921600:
aConfig->mBaudRate = MY1BAUD921600;
break;
default:
- case 9600:
+ case CBR_9600:
aConfig->mBaudRate = MY1BAUD9600;
break;
}
@@ -558,22 +568,12 @@ int assert_pin(ASerialPort_t* aPort, int pin, int assert)
#ifdef DO_MINGW
switch(pin)
{
- case MY1PIN_DTR:
- {
- aPort->mDCB.fDtrControl = assert ?
- DTR_CONTROL_ENABLE : DTR_CONTROL_DISABLE;
- break;
- }
- case MY1PIN_RTS:
- {
- aPort->mDCB.fRtsControl = assert ?
- RTS_CONTROL_ENABLE : RTS_CONTROL_DISABLE;
- break;
- }
+ case MY1PIN_DTR: status = assert ? SETDTR : CLRDTR ; break;
+ case MY1PIN_RTS: status = assert ? SETRTS : CLRRTS ; break;
}
if(status)
{
- status = SetCommState(aPort->mHandle,&aPort->mDCB) ? 0 : 1;
+ status = EscapeCommFunction(aPort->mHandle,status) ? 0 : 1;
}
#else
switch(pin)
@@ -583,7 +583,7 @@ int assert_pin(ASerialPort_t* aPort, int pin, int assert)
}
if(status)
{
- assert = assert ? TIOCMBIC : TIOCMBIS;
+ assert = assert ? TIOCMBIS : TIOCMBIC;
status = ioctl(aPort->mHandle, assert, &status);
}
#endif
@@ -592,32 +592,65 @@ int assert_pin(ASerialPort_t* aPort, int pin, int assert)
/*----------------------------------------------------------------------------*/
int detect_pin(ASerialPort_t* aPort, int pin, int *detect)
{
- int status = 0;
+ int status = 0, result = 1; /* non-zero result is failure! */
+ *detect = 0; /* detection value default to 0! */
#ifdef DO_MINGW
switch(pin)
{
+ case MY1PIN_DTR: status = pin; break;
+ case MY1PIN_RTS: status = pin; break;
case MY1PIN_DSR: status = MS_DSR_ON; break;
case MY1PIN_CTS: status = MS_CTS_ON; break;
}
if(status)
{
- DWORD check;
- *detect = status;
- status = GetCommModemStatus(aPort->mHandle,&check) ? 0 : 1;
- if(!status) *detect &= check;
+ if(status==MS_DSR_ON||status==MS_CTS_ON)
+ {
+ DWORD check;
+ result = GetCommModemStatus(aPort->mHandle,&check) ? 0 : 1;
+ if(!result) *detect = check & status;
+ }
+ else
+ {
+ result = GetCommState(aPort->mHandle,&aPort->mDCB) ? 0 : 1;
+ if(!result)
+ {
+ switch(status)
+ {
+ case MY1PIN_DTR:
+ {
+ if(aPort->mDCB.fDtrControl==DTR_CONTROL_ENABLE)
+ *detect = 1;
+ else if(aPort->mDCB.fDtrControl!=DTR_CONTROL_DISABLE)
+ result = 1;
+ break;
+ }
+ case MY1PIN_RTS:
+ {
+ if(aPort->mDCB.fRtsControl==RTS_CONTROL_ENABLE)
+ *detect = 1;
+ else if(aPort->mDCB.fRtsControl!=RTS_CONTROL_DISABLE)
+ result = 1;
+ break;
+ }
+ }
+ }
+ }
}
#else
switch(pin)
{
- case MY1PIN_DSR: status = TIOCM_DSR; break;
- case MY1PIN_CTS: status = TIOCM_CTS; break;
+ case MY1PIN_DTR: status = TIOCM_DTR; break; /* pin 4/9 */
+ case MY1PIN_RTS: status = TIOCM_RTS; break; /* pin 7/9 */
+ case MY1PIN_DSR: status = TIOCM_DSR; break; /* pin 6/9 */
+ case MY1PIN_CTS: status = TIOCM_CTS; break; /* pin 8/9 */
}
if(status)
{
- *detect = status;
- status = ioctl(aPort->mHandle, TIOCMGET, detect);
+ result = ioctl(aPort->mHandle, TIOCMGET, detect);
+ *detect = *detect & status;
}
#endif
- return status;
+ return result;
}
/*----------------------------------------------------------------------------*/
View
12 src/my1comlib.h
@@ -25,10 +25,10 @@
#define MY1BAUD460800 6
#define MY1BAUD921600 7
/*----------------------------------------------------------------------------*/
-#define MY1PIN_RTS 0
-#define MY1PIN_CTS 1
-#define MY1PIN_DTR 2
-#define MY1PIN_DSR 3
+#define MY1PIN_RTS 1
+#define MY1PIN_CTS 2
+#define MY1PIN_DTR 4
+#define MY1PIN_DSR 8
/*----------------------------------------------------------------------------*/
typedef unsigned char byte_t;
/*----------------------------------------------------------------------------*/
@@ -72,9 +72,9 @@ void purge_serial(ASerialPort_t* aPort);
void flush_serial(ASerialPort_t* aPort);
void wait_outgoing(ASerialPort_t* aPort);
int check_incoming(ASerialPort_t* aPort);
-/** Functions to control DTR/DSR/RTS/CTS: */
+/** Functions to control/check DTR/DSR/RTS/CTS: */
/** e.g. assert_pin(&cPort,MY1PIN_DTR,1); */
-/** Note: assert is CLEAR BIT but gets a HIGH at pin! */
+/** Note: assert non-zero is a +12V at pin! 0 => -12V */
int assert_pin(ASerialPort_t* aPort, int pin, int assert);
int detect_pin(ASerialPort_t* aPort, int pin, int *detect);
/*----------------------------------------------------------------------------*/
View
48 src/my1cons.h
@@ -8,6 +8,12 @@
#define KEY_SPACE 0x00000020
#define KEY_BSPACE 0x0000007F
#define KEY_TAB 0x00000009
+/** control alpha-keys - on linux, also work for uppercase! */
+#define KEY_CTRL_a 0x00000001
+#define KEY_CTRL_d 0x00000004
+#define KEY_CTRL_r 0x00000012
+#define KEY_CTRL_t 0x00000014
+#define KEY_CTRL_z 0x0000001A
/* platform dependent */
#ifdef DO_MINGW
#define KEY_ENTER 0x0000000D
@@ -34,13 +40,32 @@
#define KEY_PAGEDOWN 0x001BE051
#define KEY_INSERT 0x001BE052
#define KEY_DELETE 0x001BE053
+#define KEY_ALTF1 0x00001B68
+#define KEY_ALTF2 0x00001B69
+#define KEY_ALTF3 0x00001B6A
+#define KEY_ALTF4 0x00001B6B
+#define KEY_ALTF5 0x00001B6C
+#define KEY_ALTF6 0x00001B6D
+#define KEY_ALTF7 0x00001B6E
+#define KEY_ALTF8 0x00001B6F
+#define KEY_ALTF9 0x00001B70
+#define KEY_ALTF10 0x00001B71
+#define KEY_ALTF11 0x00001B8B
+#define KEY_ALTF12 0x00001B8C
#else
#define KEY_ENTER 0x0000000A
-/* 3-bytes */
#define KEY_F1 0x00004F50
#define KEY_F2 0x00004F51
#define KEY_F3 0x00004F52
#define KEY_F4 0x00004F53
+#define KEY_F5 0x5B31357E
+#define KEY_F6 0x5B31377E
+#define KEY_F7 0x5B31387E
+#define KEY_F8 0x5B31397E
+#define KEY_F9 0x5B32307E
+#define KEY_F10 0x5B32317E
+#define KEY_F11 0x5B32337E
+#define KEY_F12 0x5B32347E
#define KEY_UP 0x00005B41
#define KEY_DOWN 0x00005B42
#define KEY_RIGHT 0x00005B43
@@ -51,15 +76,18 @@
#define KEY_PAGEDOWN 0x005B367E
#define KEY_INSERT 0x005B327E
#define KEY_DELETE 0x005B337E
-/* 5-bytes */
-#define KEY_F5 0x5B31357E
-#define KEY_F6 0x5B31377E
-#define KEY_F7 0x5B31387E
-#define KEY_F8 0x5B31397E
-#define KEY_F9 0x5B32307E
-#define KEY_F10 0x5B32317E
-#define KEY_F11 0x5B32337E
-#define KEY_F12 0x5B32347E
+#define KEY_ALTF1 0x004F3350
+#define KEY_ALTF2 0x004F3351
+#define KEY_ALTF3 0x004F3352
+#define KEY_ALTF4 0x004F3353
+#define KEY_ALTF5 0x353B337E
+#define KEY_ALTF6 0x373B337E
+#define KEY_ALTF7 0x383B337E
+#define KEY_ALTF8 0x393B337E
+#define KEY_ALTF9 0x303B337E
+#define KEY_ALTF10 0x313B337E
+#define KEY_ALTF11 0x333B337E
+#define KEY_ALTF12 0x343B337E
#endif
/*----------------------------------------------------------------------------*/
typedef unsigned int my1key_t; /* must be at least 32-bit! */
View
83 src/my1termu.c
@@ -144,6 +144,7 @@ int main(int argc, char* argv[])
my1key_t key;
termopts_t options = 0x0;
int terminal = 1, baudrate = 0, scan = 0, test, loop, mask, drag;
+ int pin_dtr = 1, pin_rts = 1;
char filename[FILENAME_LEN], *ptty = 0x0;
FILE* pfile;
@@ -280,6 +281,12 @@ int main(int argc, char* argv[])
return ERROR_GENERAL;
}
+ /* check control pin DTR/RTS status */
+ if(detect_pin(&cPort,MY1PIN_DTR,&pin_dtr))
+ printf("\nCannot read DTR pin status!");
+ if(detect_pin(&cPort,MY1PIN_RTS,&pin_rts))
+ printf("\nCannot read RTS pin status!");
+
/* clear input buffer */
purge_serial(&cPort);
/* start main loop */
@@ -335,17 +342,20 @@ int main(int argc, char* argv[])
printf("\n\n------------\n");
printf("Command Keys\n");
printf("------------\n");
- printf("<F1> - Show this help\n");
- printf("<F2> - Send file to terminal\n");
- printf("<F4> - Show current settings\n");
- printf("<F5> - Toggle local echo\n");
- printf("<F6> - Toggle LF4CR option (Win32)\n");
- printf("<F7> - Toggle Hex display mode\n");
- printf("<F8> - Decrease baudrate\n");
- printf("<F9> - Increase baudrate\n");
- printf("<F10> - Exit this program\n");
- printf("<F11> - Clear input/output buffer\n");
- printf("<F12> - Toggle command/raw mode\n\n");
+ printf("<F1> - Show this help\n");
+ printf("<F2> - Send file to terminal\n");
+ printf("<F4> - Show current settings\n");
+ printf("<F5> - Toggle local echo\n");
+ printf("<F6> - Toggle LF4CR option (Win32)\n");
+ printf("<F7> - Toggle Hex display mode\n");
+ printf("<F8> - Decrease baudrate\n");
+ printf("<F9> - Increase baudrate\n");
+ printf("<F10> - Exit this program\n");
+ printf("<F11> - Clear input/output buffer\n");
+ printf("<F12> - Toggle command/raw mode\n");
+ printf("<CTRL+d> - Toggle DTR Line\n");
+ printf("<CTRL+r> - Toggle RTS Line\n");
+ printf("<CTRL+t> - Check DSR/CTS Status\n\n");
}
else if(key==KEY_F2) /* send file */
{
@@ -376,14 +386,7 @@ int main(int argc, char* argv[])
while(!feof(pfile))
{
test = fgetc(pfile);
- /* send through serial */
- put_byte_serial(&cPort, test);
- /* echo locally */
- if(options&OPT_LOCALECHO)
- {
- if(options&OPT_SHOWHEX) printf("[%02X]",test);
- else putchar(test);
- }
+ sendprint(&cPort,test,options);
}
fclose(pfile);
printf("\nFile \'%s\' sent!\n\n",filename);
@@ -402,11 +405,11 @@ int main(int argc, char* argv[])
else printf("\nLocal echo OFF!");
if(options&OPT_LF4CR) printf("\nLF4CR option ON!");
else printf("\nLF4CR option OFF!");
- if(options&OPT_SHOWHEX) printf("\nHex Display Mode ON!\n");
- else printf("\nHex Display Mode OFF!\n");
+ if(options&OPT_SHOWHEX) printf("\nHex Display Mode ON!");
+ else printf("\nHex Display Mode OFF!");
get_serialconfig(&cPort,&cConf);
baudrate = get_actual_baudrate(cConf.mBaudRate);
- printf("Effective baudrate: %d\n\n",baudrate);
+ printf("\nEffective baudrate: %d\n\n",baudrate);
}
else if(key==KEY_F5) /* toggle local echo */
{
@@ -446,6 +449,42 @@ int main(int argc, char* argv[])
printf("Flushing output buffer!\n\n");
flush_serial(&cPort);
}
+ else if(key==KEY_CTRL_d) /* toggle dtr */
+ {
+ pin_dtr = !pin_dtr;
+ if(assert_pin(&cPort,MY1PIN_DTR,pin_dtr))
+ printf("\nCannot set DTR pin to %d!\n",pin_dtr);
+ else
+ printf("\nDTR pin set to %d!\n",pin_dtr);
+ }
+ else if(key==KEY_CTRL_r) /* toggle rts */
+ {
+ pin_rts = !pin_rts;
+ if(assert_pin(&cPort,MY1PIN_RTS,pin_rts))
+ printf("\nCannot set RTS pin to %d!\n",pin_rts);
+ else
+ printf("\nRTS pin set to %d!\n",pin_rts);
+ }
+ else if(key==KEY_CTRL_t) /* dsr/cts status */
+ {
+ if(detect_pin(&cPort,MY1PIN_DSR,&test))
+ printf("\nCannot read DSR pin status!");
+ else
+ printf("\nDSR pin status: %08X!",test);
+ if(detect_pin(&cPort,MY1PIN_CTS,&test))
+ printf("\nCannot read CTS pin status!");
+ else
+ printf("\nCTS pin status: %08X!",test);
+ if(detect_pin(&cPort,MY1PIN_DTR,&test))
+ printf("\nCannot read DTR pin status!");
+ else
+ printf("\nDTR pin status: %08X!",test);
+ if(detect_pin(&cPort,MY1PIN_RTS,&test))
+ printf("\nCannot read RTS pin status!");
+ else
+ printf("\nRTS pin status: %08X!",test);
+ printf("\n");
+ }
else if((key&~0xFF)==0x00) /* print non-control keys */
{
sendprint(&cPort,key,options);

0 comments on commit 45c312a

Please sign in to comment.
Something went wrong with that request. Please try again.