Thread Subject: Serial fprintf/fwrite hanging troubleshooting

Subject: Serial fprintf/fwrite hanging troubleshooting

From: Geoffrey Akien

Date: 26 May, 2009 11:02:01

Message: 1 of 4

I'm making a GUI at the moment to access several different types of serially-connected instruments, each having a different configuration, and it works quite nicely. I'd like to get it to auto-detect the thing attached to each port, but when you try and send a command to an object which requires hardware flow control, it just hangs.

The code is:

serialObj = pumpobj(1) % user-written function to generate a serial object with the correct properties (here on COM1)
fopen(serialObj)
fprintf(serialObj, 'someString', 'async')
response = fscanf(serialObj)

if isempty(response)
    tryNextInstrumentType
else
    assignSomeValue
    tryNextComPort
end

If flow control is not required, then it works nicely, but if flowcontrol is hardware (I don't have anything which requires software flowcontrol) it hangs on the fprintf (same with fwrite) when you connect to something which isn't there, e.g. a COM port with nothing attached, or a different instrument type.

Anybody got any suggestions to get round this? Perhaps fooling matlab into thinking flow control is working when it isn't?

Subject: Serial fprintf/fwrite hanging troubleshooting

From: Trent Jarvi

Date: 26 May, 2009 12:23:20

Message: 2 of 4


"Geoffrey Akien" <geoff.akien@gmail.com> wrote in message
news:gvgi79$l25$1@fred.mathworks.com...
> I'm making a GUI at the moment to access several different types of
> serially-connected instruments, each having a different configuration, and
> it works quite nicely. I'd like to get it to auto-detect the thing
> attached to each port, but when you try and send a command to an object
> which requires hardware flow control, it just hangs.
>
> The code is:
>
> serialObj = pumpobj(1) % user-written function to generate a serial object
> with the correct properties (here on COM1)
> fopen(serialObj)
> fprintf(serialObj, 'someString', 'async')
> response = fscanf(serialObj)
>
> if isempty(response)
> tryNextInstrumentType
> else
> assignSomeValue
> tryNextComPort
> end
>
> If flow control is not required, then it works nicely, but if flowcontrol
> is hardware (I don't have anything which requires software flowcontrol) it
> hangs on the fprintf (same with fwrite) when you connect to something
> which isn't there, e.g. a COM port with nothing attached, or a different
> instrument type.
>
> Anybody got any suggestions to get round this? Perhaps fooling matlab
> into thinking flow control is working when it isn't?

Hi Geoffrey,

Since hardware flow control uses RTS/CTS, you can look to see if the device
asserts CTS in a setup function.

 s.PinStatus.ClearToSend

Subject: Serial fprintf/fwrite hanging troubleshooting

From: Ankit Desai

Date: 26 May, 2009 15:55:03

Message: 3 of 4

"Geoffrey Akien" <geoff.akien@gmail.com> wrote in message <gvgi79$l25$1@fred.mathworks.com>...
> I'm making a GUI at the moment to access several different types of serially-connected instruments, each having a different configuration, and it works quite nicely. I'd like to get it to auto-detect the thing attached to each port, but when you try and send a command to an object which requires hardware flow control, it just hangs.
>
> The code is:
>
> serialObj = pumpobj(1) % user-written function to generate a serial object with the correct properties (here on COM1)
> fopen(serialObj)
> fprintf(serialObj, 'someString', 'async')
> response = fscanf(serialObj)
>
> if isempty(response)
> tryNextInstrumentType
> else
> assignSomeValue
> tryNextComPort

For the instruments that require hardware flow control, you can set the FlowControl property of the object to hardware.

You can search for FlowControl in the documentation for more help.

Hope this helps

-Ankit
> end
>
> If flow control is not required, then it works nicely, but if flowcontrol is hardware (I don't have anything which requires software flowcontrol) it hangs on the fprintf (same with fwrite) when you connect to something which isn't there, e.g. a COM port with nothing attached, or a different instrument type.
>
> Anybody got any suggestions to get round this? Perhaps fooling matlab into thinking flow control is working when it isn't?

Subject: Serial fprintf/fwrite hanging troubleshooting

From: Geoffrey Akien

Date: 9 Jun, 2009 11:12:01

Message: 4 of 4

Hadn't checked this in a while because I wasn't e-mailed when there were updates. Anyway, I got it to work in the end by turning flow control off, then manually flagging the DTR pin on for 50 ms then off again. This meant the device would happily start receiving commands and responding to them, which is enough for me to find out if its working or not. Thanks anyway.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
serial Ankit Desai 26 May, 2009 11:59:04
hang Geoffrey Akien 26 May, 2009 07:04:02
fwrite Geoffrey Akien 26 May, 2009 07:04:02
fprintf Geoffrey Akien 26 May, 2009 07:04:02
freeze Geoffrey Akien 26 May, 2009 07:04:02
serial Geoffrey Akien 26 May, 2009 07:04:02
rssFeed for this Thread

Contact us at files@mathworks.com