Forum: Ruby serial port

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7f3f938311545a9e4ae7c1cd8a0a79fd?d=identicon&s=25 Joakim Oman (joakim)
on 2006-06-08 17:00
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
37ee5fa90f5eaeef62553629382497f7?d=identicon&s=25 Leslie Viljoen (Guest)
on 2006-06-09 23:55
(Received via mailing list)
On 6/8/06, Joakim Oman <joakim@verse.se> 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
7f3f938311545a9e4ae7c1cd8a0a79fd?d=identicon&s=25 Joakim Oman (joakim)
on 2006-06-12 14:10
Yepp, you were absolutely right.

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

/Joakim
This topic is locked and can not be replied to.