ext/rs_232/windows/Port.c in rs_232-3.0.0.pre4 vs ext/rs_232/windows/Port.c in rs_232-3.0.0.pre5
- old
+ new
@@ -165,20 +165,19 @@
static int queryStatusIO(VALUE self)
{
PortDescriptor *port = NULL;
-
Data_Get_Struct(self, PortDescriptor, port);
return port->status;
}
VALUE isOpenIO(VALUE self)
{
- return queryStatusIO(self) != PORT_OPEN ? Qtrue : Qfalse;
+ return queryStatusIO(self) != PORT_OPEN ? Qfalse : Qtrue;
}
VALUE flushIO(VALUE self)
{
@@ -226,19 +225,16 @@
threading,
NULL);
if (port->fd == INVALID_HANDLE_VALUE)
{
-
port->status = PORT_CLOSED;
rb_raise(rb_eIOError, "Unable to open comport: `%s`", port->settings.ComPort);
- } else
- {
-
+ } else {
port->status = PORT_OPEN;
- rb_iv_set(self, "@open", INT2FIX(port->status));
+ rb_iv_set(self, "@open", isOpenIO(self));
GetCommConfig(port->fd, &port->commConfig, &conf_length);
GetCommState(port->fd, &(port->commConfig.dcb));
port->commConfig.dcb.fBinary = 1;
@@ -259,11 +255,10 @@
}
VALUE writeIO(VALUE self, VALUE message)
{
-
int recv;
int len;
PortDescriptor *port = NULL;
Data_Get_Struct(self, PortDescriptor, port);
@@ -273,57 +268,47 @@
len = RSTRING_LEN(message);
char cStr[len];
strcpy(cStr, RSTRING_PTR(message));
if (!WriteFile(port->fd, cStr, len, (LPDWORD)((void *) &recv), NULL))
- {
- rb_raise(rb_eIOError, "IO: writing of the %d bytes has been failed", len);
- }
+ rb_raise(rb_eIOError, "IO: writing of the %d bytes has been failed. Error #%d", len, (int) GetLastError());
- return (INT2FIX(recv));
-
+ return INT2FIX(recv);
}
-
VALUE readIO(VALUE self, VALUE rb_int)
{
-
- {
- Check_Type(rb_int, T_FIXNUM);
- }
-
+ Check_Type(rb_int, T_FIXNUM);
PortDescriptor *port = NULL;
-
Data_Get_Struct(self, PortDescriptor, port);
int n;
int len = FIX2INT(rb_int);
char buf[len];
- ReadFile(port->fd, &buf, len, (LPDWORD)((void *) &n), NULL);
+ if(!ReadFile(port->fd, &buf, len, (LPDWORD)((void *) &n), NULL))
+ rb_raise(rb_eIOError, "IO: reading of %d bytes has been failed. Error #%d", len, (int) GetLastError());
if (n > 0)
return rb_str_new(buf, n);
return Qnil;
}
VALUE closeIO(VALUE self)
{
-
PortDescriptor *port = NULL;
-
Data_Get_Struct(self, PortDescriptor, port);
- flushIO(self);
- port->status = CloseHandle(port->fd);
- port->fd = INVALID_HANDLE_VALUE;
+ if (port->fd != INVALID_HANDLE_VALUE) {
+ flushIO(self);
+ port->status = CloseHandle(port->fd);
+ port->fd = INVALID_HANDLE_VALUE;
+ rb_iv_set(self, "@open", isOpenIO(self));
+ }
- rb_iv_set(self, "@open", INT2FIX(port->status));
-
- return INT2FIX(port->status);
-
+ return !isOpenIO(self);
}
VALUE initializeStruct(VALUE self, VALUE portName)
{