September 26, 2020, 07:13:07 pm

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Serial Port Help

Started by marsy28uk, March 22, 2010, 05:23:27 pm

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

marsy28uk

Hi I wondered if anyone could help me please. I used to use this old IBASIC code below to read the pins of a serial port, but I understand that this doesn't work with Windows XP, has anybody got anything similar for reading the pins in a comm port but on Win XP? Any help would be greatly appreciated.

Thanks, Steve

openconsole
' test program for Pinout.dll reading a serial port ...
def port,result:int

DECLARE "pinout", Pin(port:int),int

' read Com2 - Clear to Send (CTS) port ...
' this is Pin 8 of a 9-pin serial connector, or Pin 5 of a 25-pin connector.
' Signal ground is Pin 5 of a 9-pin connector, or Pin 7 of a 24-pin connector.

port = 0x2FE:'  (The port setting if using Com1 is 0x3FE)

' this will print continuous zeroes while the voltage on CTS is zero, and a
' positive number when the voltage is raised to between 9 and 12 volts max.
do
   result = Pin(port)
   print result
until inkey$<>""

closeconsole
end

Johnny

You're probably better off by using the windows API functions...
This is only the basic idea, and not a working example, you still have to fit it into your own program.

Code Select

' Declare the used API functions:
DECLARE "Kernel32",CreateFileA(name:STRING,access:INT,share:INT,security:INT,create:INT,flags:INT,handle:INT),INT
DECLARE "Kernel32",CloseHandle(handle:INT),INT
DECLARE "Kernel32",GetCommModemStatus(handle:INT, lpModemStat:POINTER),INT
DECLARE "kernel32",EscapeCommFunction(handle:INT, nFunc:INT),INT

DEF hcom,ModemStat:INT

' Open the COM port you like to use:
hcom = CreateFileA("COM1",0,0,0,3,0,0)

' Read the status of the lines:
GetCommModemStatus(hcom,ModemStat)
Print ModemStat:' 16 = CTS, 32 = DSR, 64 = RI, 128 = CD

' You can also drive some lines:
EscapeCommFunction(hcom,3):' SETRTS   Sends the RTS (request-to-send) signal.
EscapeCommFunction(hcom,4):' CLRRTS   Clears the RTS (request-to-send) signal.
EscapeCommFunction(hcom,5):' SETDTR   Sends the DTR (data-terminal-ready) signal.
EscapeCommFunction(hcom,6):' CLRDTR   Clears the DTR (data-terminal-ready) signal.

' Don't forget to close the COM port before ending your program:
CloseHandle(hcom)


I used this method a few times with success, so it should work for you too...
Hope this helps!  :)
Johnny

marsy28uk

Thanks loads for your help Johnny that's worked fine  :) :) :)