# Serial port

**URL:** <https://www.ruby-forum.com/t/serial-port/95672>\
**Category:** Ruby\
**Created:** [June 8, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/serial-port/95672 "2006-06-08T17:00:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![joakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joakim](https://www.ruby-forum.com/u/joakim)\
**Post date:** [June 8, 2006, 5:00pm UTC](https://www.ruby-forum.com/t/serial-port/95672/1 "2006-06-08T17:00:22Z")

</div>

Hello

I have been trying to come up with a simple way of utilizing the COM1  
port on my Windows XP Machine. The result is at the end of this entry  
(sorry for the linebreaks)

This program requires that the tx and the rx ports on the COM-port are  
connected. The program is just talking to itself.

The second thread in the program listed below is the listner and if I  
remove the sleep function the whole things goes havoc. What I would like  
is if someone could tell me why or even better, come up with a really  
nice and functional serial communication class that works with win32

## /Joakim

class Serial\<File

```
def 

```

initialize(device=“COM1”,modeString=“r+”,baud=“9600”,parity=“n”,data=“8”,stop=‘1’,retryy=“n”)  
@device = device  
@baud = baud  
@parity = parity  
@data = data  
@stop = stop  
@retryy = retryy  
modestr = “mode “+@device+” baud=”+@baud+" parity="+@parity+"  
data="+@data+" stop="+@stop+" retry="+@retryy  
system(modestr)  
super(device,modeString)  
end #initialize

end

test1=Serial.new(“COM1:”, “r+”,“14400”)

t1=Thread.new{  
loop{  
test1.write(“abcdefghijklmnopdqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ”)  
sleep(1)  
}  
}  
t2=Thread.new{  
loop{  
res=test1.read  
p res if(res.length\>1)  
sleep(0.015)  
}  
}

t1.join  
t2.join

---

<div class="post-metadata">

**Author:** ![joakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joakim](https://www.ruby-forum.com/u/joakim)\
**Post date:** [June 9, 2006, 11:55pm UTC](https://www.ruby-forum.com/t/serial-port/95672/2 "2006-06-09T23:55:43Z")

</div>

On 6/8/06, Joakim O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> remove the sleep function the whole things goes havoc. What I would like  
> @baud = baud  
> end  
> loop{  
> res=test1.read  
> p res if(res.length\>1)  
> sleep(0.015)  
> }  
> }
> 
> t1.join  
> t2.join

By havoc I assume you get garbage back?  
I’d suggest synchronising the threads and seeing what happens. It may  
be a thread safety issue with reading and writing the same “file” at  
the same time.

Since you probably won’t have that problem when actually communicating  
with other devices, your class may already be functional.

Les

---

<div class="post-metadata">

**Author:** ![joakim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dec6dc/32.png) [@joakim](https://www.ruby-forum.com/u/joakim)\
**Post date:** [June 12, 2006, 2:10pm UTC](https://www.ruby-forum.com/t/serial-port/95672/3 "2006-06-12T14:10:33Z")

</div>

Yepp, you were absolutely right.

Introducing the mutex class was the solution. Thank you very much.

/Joakim
