Opening a serialport without stopping the main app?

Is there a way to open a serialport in the background, so that other windows will still be responding? I tried to put the open command in a thread, but that stops the main thread from opening / responding to window events even more.

Opening a serial port should not hang your application, it is an operation that should take only a few milliseconds.

What OS are you on?

Could it be you might be trying to open an invalid port?

Have you checked the Serial.LastErrorCode?

Do you receive back TRUE when you serial.open?

Is the actual delay reading the available serial ports on the system?

Can you post some code?

Also, see feedback case 8212, if that is applicable (not sure it is).

Thanks for your reply, Joseph!

I’m on a Mac, the serial port exists and opens correctly. But you’re right: I fetched the name of a serial port and use it to re-open it on another app start, so I check all ports for name identity. Hadn’t thought the portscan could be the problem, but I’ll have a look at it.

In fact I estimated scanning the few ports would take as good as no time, so I didn’t even include a loop break when the port names match:

[code] dim success as boolean
for i as integer = 0 to System.SerialPortCount - 1
if System.SerialPort( i ).Name = SerialPortName then
me.SerialPort = System.SerialPort( i )
success = true
end if
next

if success then
success = me.Open
…[/code]

It doesn’t hang the system for minutes like written in 8212, only one or two seconds, but it nags me a lot to see the beach ball. Putting the above into a thread even worsened the situation: Windows (opened before port) didn’t show up until the port had been opened. …

dim success as boolean for i as integer = 0 to System.SerialPortCount - 1 if System.SerialPort( i ).Name = SerialPortName then me.SerialPort = System.SerialPort( i ) success = true exit // <<<<<<<<<<<< found it no point in looking further end if next

Never, Norman! :wink:
In cases like this my old school training kicks in, which tells me to avoid brutal skips whenever possible.

But even with this

dim success as boolean, i as integer do if System.SerialPort( i ).Name = SerialPortName then me.SerialPort = System.SerialPort( i ) success = true app.phones(phonesID).log(system.LogLevelNotice,"Serialport found") end if i.add loop until success or i = System.SerialPortCount
there are some hangs which I’d like to avoid. It’s not much, and I guess I’ll have to live with it, but I thought there could be avoid to make the whole app being stopped while the port is being searched for and opened.

please insert "a way to " before avoid.
And the log was simply for a rough estimation on how much time this takes.

I have no experience on Mac so not sure this would work but I have an idea…

It looks like you are storing the port name after successful open to use the next time the application starts. But, you are rescanning all of the ports to identify the port number from the previously stored name. Instead why don’t you just store the port number and just try to reopen that when the application starts. IF it doesn’t open THEN scan for available ports.

This way the delay of port scanning is only on the first launch of application OR if the port changes. On a normal restart of app it should be fast because it eliminates the scanning.

Hope this helps…

Thanks, Joseph. But how do I address the portnumber? On a Mac I only have the properties Inputdrivername, Outputdrivername and Name as strings and maximum speed as well as rated speed as integers for a serialport.

You should be able to use the name (path on Mac) directly.

me.SerialPort = System.SerialPort(SerialPortName)

Well, that was easy, Tim – too easy for me, it seems. And it works. And gets me rid of the delay. Thanks a lot!

Err – no, it gets me rid of the delay, but the port is not opened correctly this way. Will have a look at it later.

Once you have opened the port you know the number correct?

Why not store the port NUMBER for next time instead of the name? It looks like you store the name then scan for the name on next startup of app to obtain the port number, correct?

I must be missing something here… Does the relationship of the port NUMBER and NAME change between application starts on a Mac?

Port numbers can change.

Yes, but it occurred to me (like you’ve written, Joseph) that I can store the portnumber of course and then check if the port still has the correct name. If not, ok, I’ll search for it, and if it’s correct, I just open it. I guess this will be the fastest and most secure way. Quite easy, but I was stuck with using one parameter only in my concept. So thank you all again, I guess this solution will be good enough for my purposes.

@Joseph: On a Mac, you usually only get the active ports – which means on most Macs initially there will be only a USB- and a Bluetooth-Port available unless the user plugs in another device. Which then gets the next free serial port number. Hence, when device #3 is unplugged, former port #4 will be #3 and so on …