How to reset a COM port by code - basic.visual
This is a discussion on How to reset a COM port by code - basic.visual ; Hello
I'd like to open the serial port 'COM1' within Visual Basic. I do this
with
...
lngHandle = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0, _
ByVal 0&, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
...
It always works perfect except if 'COM1' is already ...
-
How to reset a COM port by code
Hello
I'd like to open the serial port 'COM1' within Visual Basic. I do this
with
...
lngHandle = CreateFile("COM1", GENERIC_READ Or GENERIC_WRITE, 0, _
ByVal 0&, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
...
It always works perfect except if 'COM1' is already in use.
How can I reset 'COM1' so that I can open it within Visual Basic?
Is there a way to reset the COM port by code?
Regards
Stefan
PS: Because I use a USB-RS232 converter I can unplug and plug it again
to reset 'COM1'. However how can I do this reset if I can't unplug the
COM port (e.g. internal COM port)?
-
Re: How to reset a COM port by code
Posting the same question over and over in new messages isn't going to
change the answer.
As previously replied:
If you (your process) didn't open a port (or a file, or any resource),
then you can't control it.
If you did open it, then just make sure that you call CloseHandle on
'lngHandle' when you're done using it.
--
Jim Mack
MicroDexterity Inc
www.microdexterity.com
-
Re: How to reset a COM port by code
"Jim Mack" <no-uce-ube@mdxi.com> wrote in message
news
vWdnf4SgPsuf5jUnZ2dnUVZ_sbinZ2d@giganews.com...
> Posting the same question over and over in new messages isn't going to
> change the answer.
>
> As previously replied:
>
> If you (your process) didn't open a port (or a file, or any resource),
> then you can't control it.
>
> If you did open it, then just make sure that you call CloseHandle on
> 'lngHandle' when you're done using it.
>
lol
Give it up Jim. Your response is apparently not acceptable.
-ralph
-
Re: How to reset a COM port by code
Hello Jim
I'm sorry that I've posted the same question over and over again. I
did it really not on purpose. My problem was that I've never seen my
post in the newsgroup so I thought it hadn't worked. Sorry for that!
Thanks for your reply.
Does that mean that I always have to unplug and plug my USB-RS232
converter again to reset e.g. 'COM1'? Hmmm, and how should I do this
with an internal COM port? Do I really have to reboot the pc?
Regards
Stefan
PS: My problem is that when I'm programming and testing my software I
open the COM port and sometimes I forget to close it. Afterwards I
don't know its handle so I'm not able to close the COM port again.
-
Re: How to reset a COM port by code
"Stefan Mueller" <seekware@yahoo.com> wrote in message
news:243befb1-4117-4079-b130-d320b49a7aec@q9g2000hsb.googlegroups.com...
> Hello Jim
>
> I'm sorry that I've posted the same question over and over again. I
> did it really not on purpose. My problem was that I've never seen my
> post in the newsgroup so I thought it hadn't worked. Sorry for that!
>
> Thanks for your reply.
>
> Does that mean that I always have to unplug and plug my USB-RS232
> converter again to reset e.g. 'COM1'? Hmmm, and how should I do this
> with an internal COM port? Do I really have to reboot the pc?
>
> Regards
> Stefan
>
> PS: My problem is that when I'm programming and testing my software I
> open the COM port and sometimes I forget to close it. Afterwards I
> don't know its handle so I'm not able to close the COM port again.
>
That is pretty much exactly what he is saying. COM ports for obvious reasons
are 'protected' by the O/S. The owner opens it, the owner closes it. You
didn't open, you can't close.
Now after all that is said - what in programming is truly 'impossible'. But
I highly doubt that it can be done from a 'program' or 'process' level
through the WinAPI. You probably should seek help in a device driver or
hardware newsgroup.
-ralph
-
Re: How to reset a COM port by code
"Ralph" <nt_consulting64@yahoo.com> wrote in message
news:0YOdnW8ZtJhXV5XUnZ2dnUVZ_tudnZ2d@arkansas.net...
>
> "Stefan Mueller" <seekware@yahoo.com> wrote in message
> news:243befb1-4117-4079-b130-d320b49a7aec@q9g2000hsb.googlegroups.com...
>> Hello Jim
>>
>> I'm sorry that I've posted the same question over and over again. I
>> did it really not on purpose. My problem was that I've never seen my
>> post in the newsgroup so I thought it hadn't worked. Sorry for that!
>>
>> Thanks for your reply.
>>
>> Does that mean that I always have to unplug and plug my USB-RS232
>> converter again to reset e.g. 'COM1'? Hmmm, and how should I do this
>> with an internal COM port? Do I really have to reboot the pc?
>>
>> Regards
>> Stefan
>>
>> PS: My problem is that when I'm programming and testing my software I
>> open the COM port and sometimes I forget to close it. Afterwards I
>> don't know its handle so I'm not able to close the COM port again.
>>
>
> That is pretty much exactly what he is saying. COM ports for obvious
> reasons
> are 'protected' by the O/S. The owner opens it, the owner closes it. You
> didn't open, you can't close.
>
> Now after all that is said - what in programming is truly 'impossible'.
> But
> I highly doubt that it can be done from a 'program' or 'process' level
> through the WinAPI. You probably should seek help in a device driver or
> hardware newsgroup.
>
Or stop opening Com1 as a file, and stop forgetting to close it.
Use the Comm control, and Do the coding asynch, the windows way.
And, for Pete's sake, write comm close code in the window.queryunload
event.
--
Dag.
-
Re: How to reset a COM port by code
Wow, sounds very interesting.
Today I open the COM port with
PortHandle = CreateFile("\\.\COM1", GENERIC_READ Or GENERIC_WRITE,
0, ByVal 0&, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
Call SetupComm(PortHandle, 1024, 1024)
Call PurgeComm(PortHandle, PURGE_TXABORT Or PURGE_RXABORT Or
PURGE_TXCLEAR Or PURGE_RXCLEAR)
Call SetCommTimeouts(PortHandle, udtCOMTimeouts)
Call GetCommState(PortHandle, PortDCB)
Call BuildCommDCB("9600, even, 8, 1", PortDCB)
Call SetCommState(PortHandle, PortDCB)
I write data to the COM port with
Call WriteFile(PortHandle, PortData, DataToSendLength,
DataWrittenLength, COMOverlap)
And I close the COM port with
Call CloseHandle(PortHandle)
How do I have to open, send data to and close a COM port using the
Comm Control?
Stefan