SerialPort.st
changeset 3220 6aed06c3fdf8
parent 3219 65db912c5ff8
child 3483 014fa9bca2da
--- a/SerialPort.st	Thu Apr 03 17:54:19 2014 +0200
+++ b/SerialPort.st	Fri Apr 04 10:19:20 2014 +0200
@@ -207,13 +207,19 @@
     DefaultPortName notNil ifTrue:[^ DefaultPortName ].
 
     OperatingSystem isMSDOSlike ifTrue:[
-	^ 'COM0'
+        ^ 'COM0'
     ].
-    ^ '/dev/cua0'
+    ^ '/dev/ttyS0'
 ! !
 
 !SerialPort methodsFor:'accessing'!
 
+pathName
+    "answer the port name - compatibilty with FileStream"
+
+    ^ portName
+!
+
 setBaudRate:baudRateOrNil
     baudRate := baudRateOrNil.
 !
@@ -295,7 +301,9 @@
     }
 %}.
     self primitiveFailed.
-!
+! !
+
+!SerialPort protectedMethodsFor:'low level'!
 
 closeFile
     "low level close"
@@ -331,20 +339,20 @@
     |errorSymbol errorNumber|
 
     handle notNil ifTrue:[
-	^ self errorAlreadyOpen
+        ^ self errorAlreadyOpen
     ].
 %{
     FILE *fp;
     SERIALPORT port;
     char *__portName;
     int __setBaudRate = 1,
-	__setDataBits = 1,
-	__setXOnChar = 1,
-	__setXOffChar = 1,
-	__setInFlowCtrl = 1,
-	__setOutFlowCtrl = 1,
-	__setStopBits = 1,
-	__setParityType = 1;
+        __setDataBits = 1,
+        __setXOnChar = 1,
+        __setXOffChar = 1,
+        __setInFlowCtrl = 1,
+        __setOutFlowCtrl = 1,
+        __setStopBits = 1,
+        __setParityType = 1;
     int __baudRate, __dataBits;
     int __xOnChar, __xOffChar;
     int __inFlowCtrl, __outFlowCtrl;
@@ -365,119 +373,119 @@
 #   define FLG_PARITY_NONE    3
 
     if (__isStringLike(__INST(portName))) {
-	__portName = __stringVal(__INST(portName));
+        __portName = __stringVal(__INST(portName));
     } else {
-	errorSymbol = @symbol(portName);
-	goto getOutOfhere;
+        errorSymbol = @symbol(portName);
+        goto getOutOfhere;
     }
 
     if (__isSmallInteger(__INST(baudRate))) {
-	__baudRate = __intVal(__INST(baudRate));
+        __baudRate = __intVal(__INST(baudRate));
     } else if (__INST(baudRate) == nil) {
-	__setBaudRate = 0;
+        __setBaudRate = 0;
     } else {
-	errorSymbol = @symbol(baudRate);
-	goto getOutOfhere;
+        errorSymbol = @symbol(baudRate);
+        goto getOutOfhere;
     }
 
     if (__isSmallInteger(__INST(dataBits))) {
-	__dataBits = __intVal(__INST(dataBits));
+        __dataBits = __intVal(__INST(dataBits));
     } else if (__INST(dataBits) == nil) {
-	__setDataBits = 0;
+        __setDataBits = 0;
     } else {
-	errorSymbol = @symbol(dataBits);
-	goto getOutOfhere;
+        errorSymbol = @symbol(dataBits);
+        goto getOutOfhere;
     }
 
     if (__isSmallInteger(__INST(xOnChar))) {
-	__xOnChar = __intVal(__INST(xOnChar));
+        __xOnChar = __intVal(__INST(xOnChar));
     } else if (__isCharacter(__INST(xOnChar))) {
-	__xOnChar = __intVal(__characterVal(__INST(xOnChar)));
+        __xOnChar = __intVal(__characterVal(__INST(xOnChar)));
     } else if (__INST(xOnChar) == nil) {
-	__setXOnChar = 0;
+        __setXOnChar = 0;
     } else {
-	errorSymbol = @symbol(xOnChar);
-	goto getOutOfhere;
+        errorSymbol = @symbol(xOnChar);
+        goto getOutOfhere;
     }
 
     if (__isSmallInteger(__INST(xOffChar))) {
-	__xOffChar = __intVal(__INST(xOffChar));
+        __xOffChar = __intVal(__INST(xOffChar));
     } else if (__isCharacter(__INST(xOffChar))) {
-	__xOffChar = __intVal(__characterVal(__INST(xOffChar)));
+        __xOffChar = __intVal(__characterVal(__INST(xOffChar)));
     } else if (__INST(xOffChar) == nil) {
-	__setXOffChar = 0;
+        __setXOffChar = 0;
     } else {
-	errorSymbol = @symbol(xOffChar);
-	goto getOutOfhere;
+        errorSymbol = @symbol(xOffChar);
+        goto getOutOfhere;
     }
 
     if (__INST(inFlowCtrlType) == @symbol(xOnOff)) {
-	__inFlowCtrl = FLG_FLOW_XONOFF;
+        __inFlowCtrl = FLG_FLOW_XONOFF;
     } else if (__INST(inFlowCtrlType) == @symbol(hardware)) {
-	__inFlowCtrl = FLG_FLOW_HARDWARE;
+        __inFlowCtrl = FLG_FLOW_HARDWARE;
     } else if (__INST(inFlowCtrlType) == @symbol(none)) {
-	__inFlowCtrl = FLG_FLOW_NONE;
+        __inFlowCtrl = FLG_FLOW_NONE;
     } else if (__INST(inFlowCtrlType) == nil) {
-	__setInFlowCtrl = 0;
+        __setInFlowCtrl = 0;
     } else {
-	errorSymbol = @symbol(inFlowCtrlType);
-	goto getOutOfhere;
+        errorSymbol = @symbol(inFlowCtrlType);
+        goto getOutOfhere;
     }
 
     if (__INST(outFlowCtrlType) == @symbol(xOnOff)) {
-	__outFlowCtrl = FLG_FLOW_XONOFF;
+        __outFlowCtrl = FLG_FLOW_XONOFF;
     } else if (__INST(outFlowCtrlType) == @symbol(hardware)) {
-	__outFlowCtrl = FLG_FLOW_HARDWARE;
+        __outFlowCtrl = FLG_FLOW_HARDWARE;
     } else if (__INST(outFlowCtrlType) == @symbol(none)) {
-	__outFlowCtrl = FLG_FLOW_NONE;
+        __outFlowCtrl = FLG_FLOW_NONE;
     } else if (__INST(outFlowCtrlType) == nil) {
-	__setOutFlowCtrl = 0;
+        __setOutFlowCtrl = 0;
     } else {
-	errorSymbol = @symbol(outFlowCtrlType);
-	goto getOutOfhere;
+        errorSymbol = @symbol(outFlowCtrlType);
+        goto getOutOfhere;
     }
 
     if (__INST(stopBitsType) == @symbol(stop1)) {
-	__stopBits = FLG_STOP_1;
+        __stopBits = FLG_STOP_1;
     } else if (__INST(stopBitsType) == @symbol(stop2)) {
-	__stopBits = FLG_STOP_2;
+        __stopBits = FLG_STOP_2;
     } else if (__INST(stopBitsType) == @symbol(stop1_5)) {
-	__stopBits = FLG_STOP_1_5;
+        __stopBits = FLG_STOP_1_5;
     } else if (__INST(stopBitsType) == nil) {
-	__setStopBits = 0;
+        __setStopBits = 0;
     } else {
-	errorSymbol = @symbol(stopBitsType);
-	goto getOutOfhere;
+        errorSymbol = @symbol(stopBitsType);
+        goto getOutOfhere;
     }
 
     if (__INST(parityType) == @symbol(odd)) {
-	__parityType = FLG_PARITY_ODD;
+        __parityType = FLG_PARITY_ODD;
     } else if (__INST(parityType) == @symbol(even)) {
-	__parityType = FLG_PARITY_EVEN;
+        __parityType = FLG_PARITY_EVEN;
     } else if (__INST(parityType) == @symbol(none)) {
-	__parityType = FLG_PARITY_NONE;
+        __parityType = FLG_PARITY_NONE;
     } else if (__INST(parityType) == nil) {
-	__setParityType = 0;
+        __setParityType = 0;
     } else {
-	errorSymbol = @symbol(parityType);
-	goto getOutOfhere;
+        errorSymbol = @symbol(parityType);
+        goto getOutOfhere;
     }
 
 #ifdef WIN32
     port = CreateFile(__portName,
-	      GENERIC_READ | GENERIC_WRITE,
-	      0,             /* comm devices must be opened with exclusive access */
-	      NULL,          /* no security attrs */
-	      OPEN_EXISTING, /* comm devices must use OPEN_EXISTING */
-	      0,             /* no overlapped I/O */
-	      NULL           /* hTemplate must be NULL for comm devices */
-	   );
+              GENERIC_READ | GENERIC_WRITE,
+              0,             /* comm devices must be opened with exclusive access */
+              NULL,          /* no security attrs */
+              OPEN_EXISTING, /* comm devices must use OPEN_EXISTING */
+              0,             /* no overlapped I/O */
+              NULL           /* hTemplate must be NULL for comm devices */
+           );
 
     if (port == INVALID_HANDLE_VALUE) {
-	console_fprintf(stderr, "Win32OS [info]: serial port open failed\n");
-	errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) );
-	errorSymbol = @symbol(openFailed);
-	goto getOutOfhere;
+        console_fprintf(stderr, "Win32OS [info]: serial port open failed\n");
+        errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) );
+        errorSymbol = @symbol(openFailed);
+        goto getOutOfhere;
     }
 
     /* Flush the driver */
@@ -505,81 +513,80 @@
     if (__setXOffChar) dcb.XoffChar = __xOffChar;
 
     if (__setStopBits) {
-	/* set stop bits */
-	switch(__stopBits) {
-	    case FLG_STOP_1_5: dcb.StopBits = 1; break; /* 1.5 stop bits */
-	    case FLG_STOP_1: dcb.StopBits = 0; break; /* 1 stop bit */
-	    case FLG_STOP_2: dcb.StopBits = 2; break; /* 2 stop bits */
-	    default:
-		errorSymbol = @symbol(stopBits);
-		goto errExit;
-	}
+        /* set stop bits */
+        switch(__stopBits) {
+            case FLG_STOP_1_5: dcb.StopBits = 1; break; /* 1.5 stop bits */
+            case FLG_STOP_1: dcb.StopBits = 0; break; /* 1 stop bit */
+            case FLG_STOP_2: dcb.StopBits = 2; break; /* 2 stop bits */
+            default:
+                errorSymbol = @symbol(stopBits);
+                goto errExit;
+        }
     }
 
     if (__setParityType) {
-	/* set parity */
-	switch(__parityType) {
-	    case FLG_PARITY_NONE: dcb.Parity = NOPARITY; break;
-	    case FLG_PARITY_ODD: dcb.Parity = ODDPARITY; break;
-	    case FLG_PARITY_EVEN: dcb.Parity = EVENPARITY; break;
-	    default:
-		errorSymbol = @symbol(parityType);
-		goto errExit;
-	}
+        /* set parity */
+        switch(__parityType) {
+            case FLG_PARITY_NONE: dcb.Parity = NOPARITY; break;
+            case FLG_PARITY_ODD: dcb.Parity = ODDPARITY; break;
+            case FLG_PARITY_EVEN: dcb.Parity = EVENPARITY; break;
+            default:
+                errorSymbol = @symbol(parityType);
+                goto errExit;
+        }
     }
 
     if (__setInFlowCtrl) {
-	/* set control flow */
-	dcb.fInX = FALSE;
-	dcb.fDtrControl = FALSE;
-	if (__inFlowCtrl == FLG_FLOW_XONOFF) dcb.fInX = TRUE;  /* XOn/XOff handshaking */
-	if (__inFlowCtrl == FLG_FLOW_HARDWARE) dcb.fDtrControl = TRUE;  /* hardware handshaking */
+        /* set control flow */
+        dcb.fInX = FALSE;
+        dcb.fDtrControl = FALSE;
+        if (__inFlowCtrl == FLG_FLOW_XONOFF) dcb.fInX = TRUE;  /* XOn/XOff handshaking */
+        if (__inFlowCtrl == FLG_FLOW_HARDWARE) dcb.fDtrControl = TRUE;  /* hardware handshaking */
     }
     if (__setOutFlowCtrl) {
-	dcb.fOutX = FALSE;
-	dcb.fOutxCtsFlow = FALSE;
+        dcb.fOutX = FALSE;
+        dcb.fOutxCtsFlow = FALSE;
 
-	if (__outFlowCtrl == FLG_FLOW_XONOFF) dcb.fOutX = TRUE;  /* XOn/XOff handshaking */
-	if (__outFlowCtrl == FLG_FLOW_HARDWARE) dcb.fOutxCtsFlow = TRUE;  /* hardware handshaking */
+        if (__outFlowCtrl == FLG_FLOW_XONOFF) dcb.fOutX = TRUE;  /* XOn/XOff handshaking */
+        if (__outFlowCtrl == FLG_FLOW_HARDWARE) dcb.fOutxCtsFlow = TRUE;  /* hardware handshaking */
     }
 
     if (! SetCommState(port, &dcb)) {
-	console_fprintf(stderr, "Win32OS [info]: serial port comm-setup failed\n");
-	errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) );
-	goto errExit;
+        console_fprintf(stderr, "Win32OS [info]: serial port comm-setup failed\n");
+        errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) );
+        goto errExit;
     }
-# endif /* WIN32 */
 
     /*
      * make it a FILE *
      */
-# ifdef WIN32
     {
-	int _fd;
-	__stxWrapApiEnterCritical();
-	_fd = _open_osfhandle((long)port, 0);
-	fp = fdopen(_fd, "r+");
-	__stxWrapApiLeaveCritical();
+        int _fd;
+        __stxWrapApiEnterCritical();
+        _fd = _open_osfhandle((long)port, 0);
+        fp = fdopen(_fd, "r+");
+        __stxWrapApiLeaveCritical();
     }
-# else
-    fp = fdopen(port, "r+");
-# endif
+# else /* ! WIN32 */
+    fp = fopen(__portName, "r+");
+    /* add code for unix ioctl here ... */
+# endif /* !WIN32 */
+
     if (! fp) {
-	console_fprintf(stderr, "Win32OS [info]: fdopen failed\n");
-	errorNumber = __MKSMALLINT(errno);
+        console_fprintf(stderr, "SerialPort [info]: fdopen failed\n");
+        errorNumber = __MKSMALLINT(errno);
+
     errExit: ;
 # ifdef WIN32
-	CloseHandle(port);
+        CloseHandle(port);
 # endif
-	goto getOutOfhere;
+        goto getOutOfhere;
     }
 
     {
-	OBJ t;
-
-	t = __MKEXTERNALADDRESS(fp);
-	__INST(handle) = t;
-	__STORE(self, t);
+        OBJ t = __MKEXTERNALADDRESS(fp);
+        __INST(handle) = t;
+        __STORE(self, t);
     }
 
 getOutOfhere: ;
@@ -596,13 +603,10 @@
 %}.
     "all ok?"
     handle notNil ifTrue:[
-	Lobby register:self.
+        handleType := #filePointer.
+        Lobby register:self.
     ] ifFalse:[
-	errorNumber isNil ifTrue:[
-	    self error:'invalid argument(s)'.
-	] ifFalse:[
-	    ((OperatingSystem errorHolderForNumber:errorNumber) parameter:portName) reportError
-	].
+        self openError:(errorNumber ? 'invalid argument(s)').
     ].
 
     "Modified: / 05-08-2011 / 14:14:08 / cg"
@@ -613,8 +617,10 @@
 printOn:aStream
     aStream nextPutAll:'SerialPort('.
     portName printOn:aStream.
-    aStream nextPut:$/.
-    baudRate printOn:aStream.
+    baudRate notNil ifTrue:[
+        aStream nextPut:$/.
+        baudRate printOn:aStream.
+    ].
     aStream nextPut:$).
 ! !
 
@@ -629,10 +635,10 @@
 !SerialPort class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/SerialPort.st,v 1.24 2014-04-03 15:54:19 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/SerialPort.st,v 1.25 2014-04-04 08:19:20 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic2/SerialPort.st,v 1.24 2014-04-03 15:54:19 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/SerialPort.st,v 1.25 2014-04-04 08:19:20 stefan Exp $'
 ! !