Liberty BASIC Community Forum
« Accessing the Serial Port »

Welcome Guest. Please Login or Register.
Nov 22nd, 2017, 01:32am


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Accessing the Serial Port  (Read 1537 times)
Stefan Pendl
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Computers are like babies, you must teach them what you like them to do ...


Homepage PM

Gender: Male
Posts: 5297
xx Accessing the Serial Port
« Thread started on: Sep 14th, 2013, 07:15am »

The article about Accessing the Serial Port uses the Windows API to open the serial port and obtain the windows handle of that port for use with the line controlling API afterwards.

Now the handle gets closed so that LB can successfully open it, which is mandatory.

I still think that the retrieved handle by the API can be different from the handle used by LB in certain circumstances, which results in the line control to fail.

Can anyone test if the following will work instead, since I don't have any serial port at hand.

Code:
'substitute your own port number
lpFileName$ = "Com2"

'Create a struct to recieve the incoming handshaking data
'this data contains CTS DSR RI and RLSD info.
'For more detail go to
'http://msdn.microsoft.com/en-us/library/aa363258(VS.85).aspx
struct modem,DSRCTS as long
 
'Now open the com port in Liberty and use the hFileHandle value in API calls
open lpFileName$;":9600,n,8,1,ds0,cs0,rs" for random as #com

' get the windows handle of the port, we will use this handle later in Liberty
hFileHandle = hwnd(#com)
 
'now set reset and read the handshake lines like this
print "setting DTR"
CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, _SETDTR as long,_
result as long
 
 
print "re-setting DTR"
CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, _CLRDTR as long,_
result as long
 
print "re-setting RTS"
CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, _CLRRTS as long,_
result as long
 
print "setting RTS"
CALLDLL #kernel32, "EscapeCommFunction", hFileHandle as ulong, _SETRTS as long,_
result as long
 
print "reading handshake lines"
CALLDLL #kernel32, "GetCommModemStatus", hFileHandle as ulong,modem as struct,_
result as void
print "DSR/CTS Byte = ";modem.DSRCTS.struct
'Use AND to determine which signal lines are on eg:
if modem.DSRCTS.struct and _MS_CTS_ON then print "CTS pin is on"
if modem.DSRCTS.struct and _MS_DSR_ON then print "DSR pin is on"
if modem.DSRCTS.struct and _MS_RING_ON then print "RI pin is on"
if modem.DSRCTS.struct and _MS_RLSD_ON then print "DCD pin is on"
 
Print "Closing com port"
close #com
end
 
« Last Edit: Sep 14th, 2013, 07:16am by Stefan Pendl » User IP Logged

Stefan

Make sure to read and follow the Forum Guidelines

Liberty BASIC Pro 4.04, Windows 10 Professional x64, Intel Core i7-4710MQ 2.5GHz, 16GB RAM
RichardRussell
Guest
xx Re: Accessing the Serial Port
« Reply #1 on: Sep 14th, 2013, 10:05am »

on Sep 14th, 2013, 07:15am, Stefan Pendl wrote:
Code:
' get the windows handle of the port, we will use this handle later in Liberty
hFileHandle = hwnd(#com) 

In LB4 hwnd() works only with window handles. It does also work with file and port handles in a certain similar language that I cannot name (I hope I will be forgiven for alluding to it in the circumstances).

Richard.
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5567
xx Re: Accessing the Serial Port
« Reply #2 on: Sep 14th, 2013, 11:11am »

Yep, I couldn't get to the handle inside Liberty. Something that might be fixed in the future. An alternative solution would be to stay in API mode and add some code to the article showing how to send and receive string data via API to and from the port.
User IP Logged

Me99
New Member
Image


member is offline

Avatar




PM


Posts: 19
xx Re: Accessing the Serial Port
« Reply #3 on: Sep 14th, 2013, 11:37am »

The test program does not work (see screen shot below), in fact
opening the serial port does not work at all.
Even tried this simple test and nothing.
(I have an internal modem on COM3).

Code:
open "com3:9600,n,8,1" for random as #commHandle
print #commHandle, "ATH1"
close #commHandle
end

 


I did mention it in another post here:
http://libertybasic.conforums.com/index.cgi?board=comport&action=display&num=1378547300

User Image

Shortened contents of error.log:

Code:
Runtime error: HWND() error: #com is not a window handle

Error(Exception)>>defaultAction
Error(Exception)>>activateHandler: <anUndefinedObject> 
Error(Exception)>>handle
Error(Exception)>>signal
Error class(Exception class)>>signal: <'HWND() error: #com i...'> 
BasicProgram(Object)>>error: <'HWND() error: #com i...'> 
BasicProgram>>terminateRun: <anError> 
 
« Last Edit: Sep 14th, 2013, 11:39am by Me99 » User IP Logged

RichardRussell
Guest
xx Re: Accessing the Serial Port
« Reply #4 on: Sep 14th, 2013, 3:01pm »

on Sep 14th, 2013, 11:37am, Me99 wrote:
The test program does not work (see screen shot below), in fact opening the serial port does not work at all. Even tried this simple test and nothing. (I have an internal modem on COM3).

I wouldn't be surprised if closing the serial port immediately after the ATH1 command has been sent - before the modem has even acknowledged it - may prevent it being actioned. You could try this modification:

Code:
open "com3:9600,n,8,1" for random as #commHandle
print #commHandle, "ATH1"
timer 2000, [done]
wait
[done]
timer 0
close #commHandle
end 

Richard.
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5567
xx Re: Accessing the Serial Port
« Reply #5 on: Sep 14th, 2013, 3:22pm »

Yes, you should be able to open your modem link easily and it will take several seconds to reset and respond. The issue being discussed is how to obtain the handle, the numeric identity, of a specific port. This can be fixed if it is a hardware port but can change if it is a virtual port typically connected via USB.
User IP Logged

Me99
New Member
Image


member is offline

Avatar




PM


Posts: 19
xx Re: Accessing the Serial Port
« Reply #6 on: Sep 14th, 2013, 7:12pm »

on Sep 14th, 2013, 3:01pm, RichardRussell wrote:
I wouldn't be surprised if closing the serial port immediately after the ATH1 command has been sent - before the modem has even acknowledged it - may prevent it being actioned. You could try this modification:

Code:
open "com3:9600,n,8,1" for random as #commHandle
print #commHandle, "ATH1"
timer 2000, [done]
wait
[done]
timer 0
close #commHandle
end 

Richard.


Good catch, forgot that, however tried it again and nothing.

The modem is internal and is always at COM3 and using a Terminal Program works.
Can anyone confirm that LB4.04 under Win 7 64 bit Ultimate works when opening a serial port as I find it very strange that I can add any nonsense as part of the port parameters and LB does not report an error.
For example:

Code:
open "Com3:9600,n,8,1,ds0,cs0,rs, AnyThingElseHere" for random as #com 


I would have thought that LB would check for nonsensical options.
User IP Logged

Rod
Global Moderator
ImageImageImageImageImage


member is offline

Avatar

Graphics = goosebumps!


PM

Gender: Male
Posts: 5567
xx Re: Accessing the Serial Port
« Reply #7 on: Sep 15th, 2013, 02:36am »

You are hijacking Stefan's discussion. If you still have problems better to start a new discussion.

I have no problems opening modems or any serial link on Win8 or Win7 64 bit or any previous version of the OS.

Why not work through the example in the help file that specifically talks to the modem. Do this in debug mode (the ladybug) so that you step through the program a line at a time to see what is going on.

If the serial port fails to open you will get an error message. Once the modem command has been sent wait for a second or two before proceeding, then read the buffer and get the response. When sending the modem command end the string with a ;

The ; prevents Liberty adding a CR LF pair to the end of the message.

Also browse the wiki article.

Command line style will allow nonsence, command switches can be in any order and may or may not be able to be handled by specific hardware so generally speaking switches that are not understood are ignored.
« Last Edit: Sep 15th, 2013, 02:39am by Rod » User IP Logged

Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls