Serial Port Locking Up Program

For issues with communication ports, protocols, etc.
Post Reply
k7
Posts: 25
Joined: Aug 10, 2007 8:40
Location: Tasmania, Australia
Contact:

Serial Port Locking Up Program

Post by k7 »

Hi all. It has been a while since a last posted on this forum. I've come back to FB to experiment with serial port reading which I need to know about for my job. The other guys generally use LabView or Visual BASIC for their programming but I'd like to show them that a language that doesn't cost a fortune can do the same job and with less mess.
So far I have been able to successfully read characters from the serial port and organize them into strings. The reading part isn't a problem, I am currently having no issues with that. Where I am stuck is the issue of a connection dropout. If the device connected to the serial port (a GPS simulator in this case) is turned off or disconnected, my program will halt on the line that waits for a character from the serial port. I am using character = input(1, #1) to read the character but if there is nothing there the program will just stop and wait for character to appear at the buffer.

Initially I thought that setting a timeout when I open the COM port with the option 'OPn' (n is time in ms) which is used in the OPEN COM function would help. This didn't help at all. A workmate told me that in Visual BASIC he would create another program thread which would monitor progress of the read function and determine whether it is taking too long by using timers. I then discovered that I could do this in FB. I set it up so that two threads are running at the same time. One is used for reading characters and operating on them, called Main, and one checks how long each read is taking, called TimeOut.

After each character is read a global variable which holds the time is reset. In the other thread a loop checks every 50ms whether this timer has exceeded 1s. If so an END command is issued to stop the program. For some reason the program doesn't stop. It appears that once the Main thread is waiting for a character in the buffer, the whole program has to stop. If the whole program halts then the TimeOut thread can't check whether 1 second has passed because it stops looping. I can't work out how to fix this, I have investigated mutex's but I can't see how they can help me.

I won't put all my code on here, just the relevant stuff:

Code: Select all

dim thread1 as Any Ptr
dim thread2 as Any Ptr

dim shared startTime as double

declare sub TimeOut(param as Any Ptr)
declare sub Main(param as Any Ptr)

if (Open Com ("COM1:4800,N,8,2,CS0,DS0,OP1000" As #1) = 0) then
    print "Serial Connection Successful"
    print 
else
    print "Serial Connection Failed"
    sleep
    end
end if

thread2 = ThreadCreate(@TimeOut)
thread1 = ThreadCreate(@Main)

threadwait(thread2)

print "END OF PROGRAM"
sleep
end

sub Main(param As Any Ptr)
    dim serialInput as string
    
    do
        
        serialInput = ""
        
        do
            
            character = ""
            
            character = input(1, #1)
            
            startTime = timer
            
            if (character <> chr(10)) then serialInput += character

        loop until character = chr(10) or character = ""

        print serialInput

        sleep 10
        
    loop until inkey <> ""
    
    print "FINISHED"
end sub

sub TimeOut(param As Any Ptr)
    startTime = timer
    
    print "Thread 2 Started"
    
    do
        print "Thread 2 Running..."
        
        sleep 50
        
    loop until timer > startTime + 1.0
   
    print "ERROR"

    end
end sub
I realize that this code probably isn't healthy but at the moment I just need to find a way to end the program if nothing is read after 1 second.
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

The best way would be to check your receive buffer to see if there are any characters within a certain amount of time.

Code: Select all

dim as double t,timeout
t = timer
timeout = 1
dim receivedata as string 

open com "com1:9600,n,8,1,cs0,cd0,ds0,rs" as #1

while loc(1) = 0
if timer - t > timeout then
print "Timeout on serial port"
sleep
end
end if
sleep 1
wend

receivedata = input((1,#1)

' rest of code



vdecampo
Posts: 2992
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Post by vdecampo »

I havent done much serial port programming with FB, but I would recommend using File I/O commands (Get/Put) to read character data, and use LOF and LOC to determine if there are any chars in the buffer.

-Vince
k7
Posts: 25
Joined: Aug 10, 2007 8:40
Location: Tasmania, Australia
Contact:

Post by k7 »

I thought threads were a bit of an overkill :)
I am using LOF to determine whether any characters exist in the buffer.

Phishguy, no offence but your code is rubbish. Did you even test it?
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

It may have not been a complete example. However, it was to demonstrate looking at the receive buffer using a timeout. No, I didn't try it. But, I have used this method many times in the past and it works quite well. I don't believe that lof works properly for serial ports. Use loc.
k7
Posts: 25
Joined: Aug 10, 2007 8:40
Location: Tasmania, Australia
Contact:

Post by k7 »

I found that EOF was the best to use. It works okay now. I am still using threads; one for the serial port reading and one for checking the time. Unfortunatly it seems that I will have to use Visual BASIC anyway. I don't mind that but they're only at version 6 and no one knows anything about .NET.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Post by D.J.Peters »

only for info

thread2 = ThreadCreate(@TimeOut)
thread1 = ThreadCreate(@Main)

on windows only

sometimes ThreadCreate(@Main) will be created and running before ThreadCreate(@TimeOut) is running

take a look: http://www.freebasic.net/forum/viewtopic.php?t=5091

Joshy
k7
Posts: 25
Joined: Aug 10, 2007 8:40
Location: Tasmania, Australia
Contact:

Post by k7 »

Interesting, I was not aware threads behaved like that. FreeBASIC is the first language I have used threads in. Do they go by another name in some languages? Are there any other BASIC's that support threads?
Post Reply