Serial port


#1

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


#2

On 6/8/06, Joakim O. 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


#3

Yepp, you were absolutely right.

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

/Joakim