SerialPort.st
changeset 1596 0296c521f18e
parent 1595 eb26b0dfa8f6
child 1597 82fcfa2fc223
--- a/SerialPort.st	Thu Feb 02 17:46:12 2006 +0100
+++ b/SerialPort.st	Thu Feb 02 18:19:40 2006 +0100
@@ -125,6 +125,16 @@
     serialPort open.
     serialPort close.
                                                                         [exEnd]
+
+                                                                        [exBegin]
+    |serialPort|
+
+    serialPort := SerialPort new setPortName:'COM5'.
+    serialPort open.
+    serialPort nextPutAll:'hello world'.
+    serialPort cr.
+    serialPort close.
+                                                                        [exEnd]
 "
 ! !
 
@@ -175,7 +185,7 @@
     DefaultPortName notNil ifTrue:[^ DefaultPortName ].
 
     OperatingSystem isMSDOSlike ifTrue:[
-        ^ 'COM0'
+	^ 'COM0'
     ].
     ^ '/dev/cua0'
 ! !
@@ -290,7 +300,7 @@
 !SerialPort methodsFor:'opening'!
 
 open
-    |errorNumber|
+    |errorSymbol errorNumber|
 
     filePointer notNil ifTrue:[
 	^ self errorAlreadyOpen
@@ -328,7 +338,8 @@
     if (__isString(__INST(portName))) {
 	__portName = __stringVal(__INST(portName));
     } else {
-	goto failure;
+	errorSymbol = @symbol(portName);
+	goto getOutOfhere;
     }
 
     if (__isSmallInteger(__INST(baudRate))) {
@@ -336,15 +347,17 @@
     } else if (__INST(baudRate) == nil) {
 	__setBaudRate = 0;
     } else {
-	goto failure;
+	errorSymbol = @symbol(baudRate);
+	goto getOutOfhere;
     }
 
     if (__isSmallInteger(__INST(dataBits))) {
 	__dataBits = __intVal(__INST(dataBits));
-    } else if (__INST(dataBits == nil)) {
+    } else if (__INST(dataBits) == nil) {
 	__setDataBits = 0;
     } else {
-	goto failure;
+	errorSymbol = @symbol(dataBits);
+	goto getOutOfhere;
     }
 
     if (__isSmallInteger(__INST(xOnChar))) {
@@ -354,7 +367,8 @@
     } else if (__INST(xOnChar) == nil) {
 	__setXOnChar = 0;
     } else {
-	goto failure;
+	errorSymbol = @symbol(xOnChar);
+	goto getOutOfhere;
     }
 
     if (__isSmallInteger(__INST(xOffChar))) {
@@ -364,7 +378,8 @@
     } else if (__INST(xOffChar) == nil) {
 	__setXOffChar = 0;
     } else {
-	goto failure;
+	errorSymbol = @symbol(xOffChar);
+	goto getOutOfhere;
     }
 
     if (__INST(inFlowCtrlType) == @symbol(xOnOff)) {
@@ -374,7 +389,8 @@
     } else if (__INST(inFlowCtrlType) == nil) {
 	__setInFlowCtrl = 0;
     } else {
-	goto failure;
+	errorSymbol = @symbol(inFlowCtrlType);
+	goto getOutOfhere;
     }
 
     if (__INST(outFlowCtrlType) == @symbol(xOnOff)) {
@@ -384,7 +400,8 @@
     } else if (__INST(outFlowCtrlType) == nil) {
 	__setOutFlowCtrl = 0;
     } else {
-	goto failure;
+	errorSymbol = @symbol(outFlowCtrlType);
+	goto getOutOfhere;
     }
 
     if (__INST(stopBitsType) == @symbol(stop1)) {
@@ -396,7 +413,21 @@
     } else if (__INST(stopBitsType) == nil) {
 	__setStopBits = 0;
     } else {
-	goto failure;
+	errorSymbol = @symbol(stopBitsType);
+	goto getOutOfhere;
+    }
+
+    if (__INST(parityType) == @symbol(odd)) {
+	__parityType = ODD;
+    } else if (__INST(parityType) == @symbol(even)) {
+	__parityType = EVEN;
+    } else if (__INST(parityType) == @symbol(none)) {
+	__parityType = NONE;
+    } else if (__INST(parityType) == nil) {
+	__setParityType = 0;
+    } else {
+	errorSymbol = @symbol(parityType);
+	goto getOutOfhere;
     }
 
 #ifdef WIN32
@@ -412,7 +443,8 @@
     if (port == INVALID_HANDLE_VALUE) {
 	fprintf(stderr, "Win32OS [info]: serial port open failed\n");
 	errorNumber = __mkSmallInteger( __WIN32_ERR(GetLastError()) );
-	goto failure;
+	errorSymbol = @symbol(openFailed);
+	goto getOutOfhere;
     }
 
     /* Flush the driver */
@@ -445,7 +477,9 @@
 	    case STOP1_5: dcb.StopBits = 1; break; /* 1.5 stop bits */
 	    case STOP1: dcb.StopBits = 0; break; /* 1 stop bit */
 	    case STOP2: dcb.StopBits = 2; break; /* 2 stop bits */
-	    default: goto errExit;
+	    default:
+		errorSymbol = @symbol(stopBits);
+		goto errExit;
 	}
     }
 
@@ -455,7 +489,9 @@
 	    case NONE: dcb.Parity = NOPARITY; break;
 	    case ODD: dcb.Parity = ODDPARITY; break;
 	    case EVEN: dcb.Parity = EVENPARITY; break;
-	    default: goto errExit;
+	    default:
+		errorSymbol = @symbol(parityType);
+		goto errExit;
 	}
     }
 
@@ -493,8 +529,11 @@
     fp = fdopen(port, "r+");
 # endif
     if (! fp) {
+	fprintf(stderr, "Win32OS [info]: fdopen failed\n");
 	errorNumber = __MKSMALLINT(errno);
-	goto errExit;
+    errExit: ;
+	CloseHandle(port);
+	goto getOutOfhere;
     }
 
     {
@@ -505,12 +544,7 @@
 	__STORE(self, t);
     }
 
-	RETURN( true );
-
-errExit: ;
-    CloseHandle(port);
-
-failure: ;
+getOutOfhere: ;
 #   undef XONOFF
 #   undef HARDWARE
 #   undef STOP1
@@ -552,5 +586,5 @@
 !SerialPort class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/SerialPort.st,v 1.2 2006-02-02 16:46:12 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/SerialPort.st,v 1.3 2006-02-02 17:19:40 cg Exp $'
 ! !