May 09, 2024, 06:55:10 AM

News:

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


Asked function IsSerialPort()

Started by Techno, September 20, 2015, 11:41:21 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Techno

Hi all

For a project whose use is made of the I2C protocol over RS232 I need a function that returns
a return value if the given serialport is valid and correctly initialized


SUB IsSerialPort(iPort AS INT), INT
    '===============================================
    'Tests if the given serialport is valid and correctly initialized               '
    'Parameters : iPort : The serial port to test                                   '
    'Return value :                                                                 '
    '   Nonzero if the serialport is valid                                          '
    '   Zero : if the serialport non valid                                          '
    'Remarks : This function is bulletproof and may be used with any value.         '
    '          This is the correct way to ensure a serial port is ready to use.     '
    '===============================================
ENDSUB

Andy

Have a look at the commread example that comes with IWB.

Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Techno

Quote from: andy1966 on September 21, 2015, 12:15:41 AM
Have a look at the commread example that comes with IWB.



I found it but it's no a function:

IF hcom <> -1
   SetCommTimeouts(hcom,timeout)
   IF GetCommState(hcom,dcb1)
      PRINT "Press Spacebar to stop"
      PRINT "Com port open, Current baud = ",dcb1.BaudRate
      dcb1.BaudRate = 9600
      dcb1.ByteSize = 8
      dcb1.StopBits = @ONESTOPBIT
      dcb1.Parity = @NOPARITY
      IF SetCommState(hcom,dcb1) = 0
         PRINT "Error setting com parameters"
      ELSE
         PRINT "Baud rate changed to ",dcb1.BaudRate
      ENDIF
      REM we continue to read the port until 4 bytes are read
      REM or someone presses the space bar.
      DO
         IF ReadFile(hcom,data1,4,bytesread,0) <> 0
            PRINT "Bytes Read ",bytesread
         ENDIF
      UNTIL (data1 = "test") OR (GETKEYSTATE(0x20))
   ENDIF
   success = CloseHandle(hcom)
ELSE
   PRINT "Could not open com port"
ENDIF

How can I create this in a function?.

Techno

Egil,

I try this:



SUB IsSerialPort(iPort AS INT), INT
   IF(iPort <> -1)
       IF(GetCommState(iPort, DCB1)
           IF(SetCommState(iPort, DCB1) = 0
               RETURN 0
           ELSE
               RETURN 1
           END IF
       END IF
   ELSE
       FUNCTION 0
   END IF
ENDSUB              


Egil

In order to see that a serial port is valid, and set up properly, you have to test the full DCB1 type, which you already do in your example.
But returning TRUE or FALSE directly after calling  GetCommState(iPort, DCB1)  only returns that the type has been defined, and nothing else.

So after calling GetCommState you have to check that the value of every member of that type has the  correct value.

( E.g. IF DCB1.BaudRate = 9600 then [ do whatever you need to do with that result], etc. )

The easiest way is to set the bits of a variable when the UDT members are set when opening the comport, and then use the variable value for later comparisons.
Or you, if you do not set every member of the type when opening the comport, define this value up front, and then do bitwise testing on every member of the DCB1 UDT, setting a bit if the DCB1 member value is correct, and not if uncorrect. Then, when all the member has been tested, you will either have the same value as the predefined one ( = everything is correct) or not ( = something is wrong).

Study that commread example again, and instead of setting the member values as in that example you can test them and see that they are what you expect them to be, as stated above.


Hope this make things a little more understandable.


Egil
Support Amateur Radio  -  Have a ham  for dinner!

Techno

Quote from: Egil on September 21, 2015, 03:03:40 AM
In order to see that a serial port is valid, and set up properly, you have to test the full DCB1 type, which you already do in your example.
But returning TRUE or FALSE directly after calling  GetCommState(iPort, DCB1)  only returns that the type has been defined, and nothing else.

So after calling GetCommState you have to check that the value of every member of that type has the  correct value.

( E.g. IF DCB1.BaudRate = 9600 then [ do whatever you need to do with that result], etc. )

The easiest way is to set the bits of a variable when the UDT members are set when opening the comport, and then use the variable value for later comparisons.
Or you, if you do not set every member of the type when opening the comport, define this value up front, and then do bitwise testing on every member of the DCB1 UDT, setting a bit if the DCB1 member value is correct, and not if uncorrect. Then, when all the member has been tested, you will either have the same value as the predefined one ( = everything is correct) or not ( = something is wrong).

Study that commread example again, and instead of setting the member values as in that example you can test them and see that they are what you expect them to be, as stated above.


Hope this make things a little more understandable.


Egil


Egil,

I don't understand what you main. Can you post here an short example or speudocode?

Egil

In the IWB commread example it is shown how to set the comport parameters to the needed values.
Just reverse this process by reading them.
Here is some pseudo code to show you what I mean:


if dcb1.BaudRate = [here you put the expected or correct value]
   [BaudRate is correct, and you do nothing]
else
   [BaudRate is NOT correct, you have to specify what to do]
endif


if dcb1.ByteSize = [here you put the expected or correct value]
   [ByteSize is correct, and you do nothing]
else
   [ByteSize is NOT correct, you have to specify what to do]
endif


if dcb1.StopBits = [here you put the expected or correct value]
   [StopBits is correct, and you do nothing]
else
   [StopBits is NOT correct, you have to specify what to do]
endif


if dcb1.Parity  = [here you put the expected or correct value]
   [Parity is correct, and you do nothing]
else
   [Parity is NOT correct, you have to specify what to do]
endif


Repeat this for all the comport parameters you need to take care of. And if the values are different from what you expect, you can correct them at will by writing them the same way as in the IWB commread example, but with your own values.


Egil
Support Amateur Radio  -  Have a ham  for dinner!

Techno

Quote from: Egil on September 21, 2015, 08:21:46 AM
In the IWB commread example it is shown how to set the comport parameters to the needed values.
Just reverse this process by reading them.
Here is some pseudo code to show you what I mean:


if dcb1.BaudRate = [here you put the expected or correct value]
   [BaudRate is correct, and you do nothing]
else
   [BaudRate is NOT correct, you have to specify what to do]
endif


if dcb1.ByteSize = [here you put the expected or correct value]
   [ByteSize is correct, and you do nothing]
else
   [ByteSize is NOT correct, you have to specify what to do]
endif


if dcb1.StopBits = [here you put the expected or correct value]
   [StopBits is correct, and you do nothing]
else
   [StopBits is NOT correct, you have to specify what to do]
endif


if dcb1.Parity  = [here you put the expected or correct value]
   [Parity is correct, and you do nothing]
else
   [Parity is NOT correct, you have to specify what to do]
endif


Repeat this for all the comport parameters you need to take care of. And if the values are different from what you expect, you can correct them at will by writing them the same way as in the IWB commread example, but with your own values.


Egil

Egil

I have studied the example commenter read but I can not function,
pull out the validity of the serial port test .
The question is what should I give return value to my job because I get stuck
I read the MSDN library about serial communications.

This is my code:
==========



REM Requires iwbasic 1.0 or greater
REM shows how to read bytes from a com port (COM1)
'NOTE: PRESS SPACEBAR TO QUIT

'Compile as a CONSOLE target

'useful defines
SETID "OPENEXISTING",3
SETID "GENERICREAD",0x80000000
SETID "GENERICWRITE",0x40000000
SETID "NOPARITY",0
SETID "ODDPARITY",1
SETID "EVENPARITY",2
SETID "MARKPARITY",3
SETID "SPACEPARITY",4
SETID "ONESTOPBIT",0
SETID "TWOSTOPBITS",2

type COMMTIMEOUT
    DEF ReadIntervalTimeout:INT 
    DEF ReadTotalTimeoutMultiplier:INT
    DEF ReadTotalTimeoutConstant:INT
    DEF WriteTotalTimeoutMultiplier:INT
    DEF WriteTotalTimeoutConstant:INT
endtype

type DCB
    DEF DCBlength:int
    DEF BaudRate:int
DEF flags:int
    DEF wReserved:word
    DEF XonLim:word
    DEF XoffLim:word
    DEF ByteSize:char
    DEF Parity:char
    DEF StopBits:char
    DEF XonChar:char
    DEF XoffChar:char
    DEF ErrorChar:char
    DEF EofChar:char
    DEF EvtChar:char
    DEF wReserved1:word
ENDTYPE

REM we use the Windows API to open the com port as a file. This works
REM in all versions of windows.
DECLARE IMPORT,CreateFileA(name:string,access:int,share:int,security:int,create:int,flags:int,handle:int),int
DECLARE IMPORT,GetCommState(handle:int,lpdcb:DCB),int
DECLARE IMPORT,SetCommState(handle:int,lpdcb:DCB),int
DECLARE IMPORT,CloseHandle(handle:int),int
DECLARE IMPORT,WriteFile(handle:int,buffer:string,count:int,written:pointer,overlapped:int),int
DECLARE IMPORT,ReadFile(handle:int,buffer:string,count:int,bytesread:pointer,overlapped:int),int
DECLARE IMPORT,SetCommTimeouts(handle:int,timeout:COMMTIMEOUT),int

def dcb1:DCB
def timeout:COMMTIMEOUT
def hcom,success,written,bytesread:int
def data1:string
dcb1.DCBlength = 26
REM in order to read from the port we need to set some
REM timeout values. The interval is 1 second
timeout.ReadIntervalTimeout = 1000
timeout.ReadTotalTimeoutMultiplier = 250
timeout.ReadTotalTimeoutConstant = 1

hcom = CreateFileA("COM1",@GENERICREAD | @GENERICWRITE,0,0,@OPENEXISTING,0,0)


OPENCONSOLE
IF(hcom <> -1) then
'=========================================================================='
'Sets the time-out parameters for all read and write operations '
'on a specified communications device. '
'=========================================================================='
SetCommTimeouts(hcom,timeout)

'=========================================================================='
'Retrieves the current control settings for a specified communications device.
'=========================================================================='
IF(GetCommState(hcom,dcb1)) then ' If TRUE or "1"
'PRINT "Press Spacebar to stop"
'PRINT "Com port open, Current baud = ",dcb1.BaudRate

'Set the comm parameters
'dcb1.BaudRate = 9600
'dcb1.ByteSize = 8
'dcb1.StopBits = @ONESTOPBIT
'dcb1.Parity = @NOPARITY

'======================================================================='
'Configures a communications device according to the specifications '
'in a device-control block. '
'======================================================================='
'IF(SetCommState(hcom,dcb1) = 0) then 'If FALSE or "0" --> Error
' PRINT "Error setting com parameters"
'ELSE
' PRINT "Baud rate changed to ",dcb1.BaudRate
'ENDIF

'======================================================================='
'we continue to read the port until 4 bytes are read '
'or someone presses the space bar. '
'======================================================================='
'DO
' IF(ReadFile(hcom, data1, 4, bytesread, 0) <> 0) then
' PRINT "Bytes Read ",bytesread
' ENDIF
'UNTIL(data1 = "test") OR (GETKEYSTATE(0x20))

'========================================================================'
'I try this but I don't know if that is correct '
'========================================================================'

IF(dcb1.Baudrate = %CBR_9600) THEN
RETURN 1
      ELSE
         RETURN 0
      ENDIF

      IF(dcb1.ByteSize = 8) THEN
         RETURN 1
      ELSE
         RETURN 0
      eNDIF

      IF(dcb1.Parity = %NOPARITY) THEN
         RETURN 1
      ELSE
         RETURN 0
      END IF

      IF(dcb1.StopBits = %ONESTOPBIT) THEN
         RETURN 1
      ELSE
         RETURN 0
      END IF 
ENDIF
success = CloseHandle(hcom)
ELSE
PRINT "Could not open com port"
ENDIF
END



Andy

The way your code looks to me has a problem.

You are returning after checking the baud rate, so your program will never get down to checking byte size or parity.

For me I would create some  variables:

DEF Rate:INT
DEF Size:INT
DEF Parity:CHAR

Then you could check each in turn BEFORE returning...

Open the com port, then check the settings....

IF dcb1.BaudRate = 9600 THEN Rate = 1
IF dcb1.ByteSize = 8 THEN Size = 1
IF dcb1.Parity = @NOPARITY THEN Parity = 1

OR you could do.....

IF dcb1.BaudRate = 9600
   IF dcb1.ByteSize = 8
      IF dcb1.Parity = @NOPARITY
          RETURN 1
      ENDIF
   ENDIF
ENDIF

Also, close the com port at the end of your program before exiting....


success = CloseHandle(hcom)


Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Egil

Techno,

Andy is right you know.
And to be sure the comport is set up properly, you have to check all comport parameters before returning to the calling function. Why not define an extra variable you can use as a flag. Set this flag to TRUE whenever a parameter is ok, but do not return at this point. Then check the next parameters in turn. Set them to TRUE (or do nothing) if they are ok, and if all parameters are ok end the subroutine by returning TRUE.
But if any of the parameters is incorrect, set the flag to FALSE and return  this value immediately.

When you get familiar with this kind of programming, you do not even need that "flag" variable. But it is easier to understand what is going on when using it,

And finally, a little friendly advice.
MSDN is a very informative website. But all code they refer to is C++, and the C++ syntax is very different from IWB. You will never learn any of the languages when mixing them like you do.
So stick to one single language until you are confident with its syntax.


Good Luck!

Egil

Support Amateur Radio  -  Have a ham  for dinner!

Techno

Egil

I try this but how can I this implement in a functions:

DECLARE IsSerialPort(iPort AS INT), INT

This is my code:
==========


REM Requires iwbasic 1.0 or greater
REM shows how to read bytes from a com port (COM1)
'NOTE: PRESS SPACEBAR TO QUIT

'Compile as a CONSOLE target

'useful defines
SETID "OPENEXISTING",3
SETID "GENERICREAD",0x80000000
SETID "GENERICWRITE",0x40000000
SETID "NOPARITY",0
SETID "ODDPARITY",1
SETID "EVENPARITY",2
SETID "MARKPARITY",3
SETID "SPACEPARITY",4
SETID "ONESTOPBIT",0
SETID "TWOSTOPBITS",2

type COMMTIMEOUT
   DEF ReadIntervalTimeout:INT 
   DEF ReadTotalTimeoutMultiplier:INT
   DEF ReadTotalTimeoutConstant:INT
   DEF WriteTotalTimeoutMultiplier:INT
   DEF WriteTotalTimeoutConstant:INT
endtype

type DCB
DEF DCBlength:int
   DEF BaudRate:int
DEF flags:int
   DEF wReserved:word
   DEF XonLim:word
   DEF XoffLim:word
   DEF ByteSize:char
   DEF Parity:char
   DEF StopBits:char
   DEF XonChar:char
   DEF XoffChar:char
   DEF ErrorChar:char
   DEF EofChar:char
   DEF EvtChar:char
   DEF wReserved1:word
ENDTYPE

REM we use the Windows API to open the com port as a file. This works
REM in all versions of windows.
DECLARE IMPORT,CreateFileA(name:string,access:int,share:int,security:int,create:int,flags:int,handle:int),int
DECLARE IMPORT,GetCommState(handle:int,lpdcb:DCB),int
DECLARE IMPORT,SetCommState(handle:int,lpdcb:DCB),int
DECLARE IMPORT,CloseHandle(handle:int),int
DECLARE IMPORT,WriteFile(handle:int,buffer:string,count:int,written:pointer,overlapped:int),int
DECLARE IMPORT,ReadFile(handle:int,buffer:string,count:int,bytesread:pointer,overlapped:int),int
DECLARE IMPORT,SetCommTimeouts(handle:int,timeout:COMMTIMEOUT),int

def dcb1:DCB
def timeout:COMMTIMEOUT
def hcom,success,written,bytesread:int
def data1:string
dcb1.DCBlength = 26
REM in order to read from the port we need to set some
REM timeout values. The interval is 1 second
timeout.ReadIntervalTimeout = 1000
timeout.ReadTotalTimeoutMultiplier = 250
timeout.ReadTotalTimeoutConstant = 1

hcom = CreateFileA("COM1",@GENERICREAD | @GENERICWRITE,0,0,@OPENEXISTING,0,0)


OPENCONSOLE
IF(hcom <> -1) then
'=========================================================================='
'Sets the time-out parameters for all read and write operations '
'on a specified communications device. '
'=========================================================================='
SetCommTimeouts(hcom,timeout)

'=========================================================================='
'Retrieves the current control settings for a specified communications device.
'=========================================================================='
IF(GetCommState(hcom,dcb1)) then ' If TRUE or "1"
'PRINT "Press Spacebar to stop"
'PRINT "Com port open, Current baud = ",dcb1.BaudRate

'Set the comm parameters
'dcb1.BaudRate = 9600
'dcb1.ByteSize = 8
'dcb1.StopBits = @ONESTOPBIT
'dcb1.Parity = @NOPARITY

'======================================================================='
'Configures a communications device according to the specifications '
'in a device-control block. '
'======================================================================='
'IF(SetCommState(hcom,dcb1) = 0) then 'If FALSE or "0" --> Error
' PRINT "Error setting com parameters"
'ELSE
' PRINT "Baud rate changed to ",dcb1.BaudRate
'ENDIF

'======================================================================='
'we continue to read the port until 4 bytes are read '
'or someone presses the space bar. '
'======================================================================='
'DO
' IF(ReadFile(hcom, data1, 4, bytesread, 0) <> 0) then
' PRINT "Bytes Read ",bytesread
' ENDIF
'UNTIL(data1 = "test") OR (GETKEYSTATE(0x20))

'========================================================================'
'I try this but I don't know if that is correct '
'========================================================================'

IF(dcb1.Baudrate = %CBR_9600) THEN
IF(dcb1.ByteSize = 8) THEN
IF(dcb1.Parity = %NOPARITY) THEN
IF(dcb1.StopBits = %ONESTOPBIT) THEN
RETURN 1
END if
END if
endif
else
RETURN 0
END if
success = CloseHandle(hcom)
PRINT "Could not open com port"
ENDIF
END


Egil

Quote from: Techno on September 22, 2015, 09:11:10 AM
I try this but how can I this implement in a functions:

If you manage to open the comport, it is most likely a comport. And, as mentioned before, to be 100% sure it is properly set up, you have to test all comport parameters separately.
You'll find all the information you need in the IWB examples, and by studying the many examples posted on this forum.

Support Amateur Radio  -  Have a ham  for dinner!

Andy

Techno,
Egil, Larry and me Keep telling you that you need to try things yourself.

Stick to one language, here it is IWB. For any other language please consult other forums.

One more point, when we do help, it would be polite to let us know when you have found a solution.


:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

ckoehn

I must admit, I really see no need for a IsSerialPort(iPort).  All you need to do is "try" to open a com port with the correct parameters.  If it succeeds, than not only is it a valid serial port, it also has the correct parameters set.

I named this file Comm.inc and use to include it whenever I needed a serial port.  I purchase IWComLib because it is economical and makes things much easier.

$INCLUDE "windowssdk.inc"

TYPE COMMTIMEOUT
    INT ReadIntervalTimeout 
    INT ReadTotalTimeoutMultiplier
    INT ReadTotalTimeoutConstant
    INT WriteTotalTimeoutMultiplier
    INT WriteTotalTimeoutConstant
ENDTYPE

SUB OpenCom(INT nPort,INT Baud,INT Bits,STRING Parity,INT StopBit),UINT
'usage:   hport=OpenCom(1,9600,8,"N",1)

DCB dcb1
INT hcom
COMMTIMEOUT timeout
dcb1.DCBlength = 26

timeout.ReadIntervalTimeout = 1000
timeout.ReadTotalTimeoutMultiplier = 1 '250
timeout.ReadTotalTimeoutConstant = 250
timeout.WriteTotalTimeoutMultiplier=0
timeout.WriteTotalTimeoutConstant=0

hcom = CreateFileA("\\\\.\\COM"+LTRIM$(STR$(nPort)),0x80000000 | 0x40000000,0,0,3,0,0)
IF hcom <> -1
SetCommTimeouts(hcom,timeout)

IF GetCommState(hcom,dcb1)
dcb1.BaudRate = Baud
dcb1.ByteSize = Bits
SELECT StopBit
CASE 2
dcb1.StopBits = 2
ELSE
dcb1.StopBits = 0
ENDSELECT
SELECT UCASE$(Parity)
CASE "O"
dcb1.Parity = 1
CASE "E"
dcb1.Parity = 2
CASE "M"
dcb1.Parity = 3
CASE "S"
dcb1.Parity = 4
ELSE
dcb1.Parity = 0
ENDSELECT
IF SetCommState(hcom,dcb1) = 0
hcom= -1
ENDIF
ELSE
hcom=-1
ENDIF
ENDIF
RETURN hcom
ENDSUB

SUB ReadCom(UINT hPort,STRING Msg BYREF,INT mLen),INT
INT bytesread=0

IF ReadFile(hPort,Msg,mLen,bytesread,0) = 0
bytesread=0
ENDIF
RETURN bytesread
ENDSUB

SUB WriteCom(INT hPort,STRING msg,INT mLen),INT
INT written=0

IF mLen>LEN(msg)
mLen=LEN(msg)
ENDIF

IF WriteFile(hPort,msg,mLen,written,0)=0
written=0
ENDIF
RETURN written
ENDSUB

SUB CloseCom(INT hPort),INT
RETURN CloseHandle(hPort)
ENDSUB

SUB BytesInBuffer(int hport),int
int bytes

bytes=GetFileSize(hport,2048)

RETURN bytes
ENDSUB

SUB WriteByte (int hPort,byte msg)
INT written=0

IF WriteFile(hPort,msg,1,written,0)=0
written=0
ENDIF
RETURN written
ENDSUB

SUB WriteBytes (int hPort,pointer msg, int mLen)
INT written=0

IF WriteFile(hPort,msg,mLen,written,0)=0
written=0
ENDIF
RETURN written
ENDSUB

SUB ReadByte(INT hPort,byte bite BYREF),INT
INT bytesread=0

IF ReadFile(hPort,bite,1,bytesread,0) = 0
bytesread=0
ENDIF
RETURN bytesread
ENDSUB

Techno

ckoehn

Thanks for your code.

But I have need functions for :
- set the output lines : DTR, RTS and RXD.
- read the input lines : CTS, DSR,
- Set the size for RX and TX Buffer
- Read the RX and TX Buffer

This is my code:



$INCLUDE "windowssdk.inc"

TYPE COMMTIMEOUT
    INT ReadIntervalTimeout 
    INT ReadTotalTimeoutMultiplier
    INT ReadTotalTimeoutConstant
    INT WriteTotalTimeoutMultiplier
    INT WriteTotalTimeoutConstant
ENDTYPE

SUB OpenCom(INT nPort,INT Baud,INT Bits,STRING Parity,INT StopBit, uint dwReadIntervalTimeOut, UINT dwReadTotalTimeout, uint rtsCtrl, UINT dtrCtrl),UINT
'usage:   hport=OpenCom(1,9600,8,"N",1)

DCB dcb1
INT hcom
COMMTIMEOUT timeout
dcb1.DCBlength = 26

'timeout.ReadIntervalTimeout = 1000
timeout.ReadTotalTimeoutMultiplier = 1 '250
'timeout.ReadTotalTimeoutConstant = 250
timeout.WriteTotalTimeoutMultiplier=0
timeout.WriteTotalTimeoutConstant=0

hcom = CreateFileA("\\\\.\\COM"+LTRIM$(STR$(nPort)),0x80000000 | 0x40000000,0,0,3,0,0)

IF hcom <> -1
IF(GetCommTimeouts(hCom,timeout)

SELECT dwReadIntervalTimeOut
case 100
timeout.ReadIntervalTimeout = 100
case 200
timeout.ReadIntervalTimeout = 200
case 300
timeout.ReadIntervalTimeout = 300
case 400
timeout.ReadIntervalTimeout = 400
case 500
timeout.ReadIntervalTimeout = 500
case 600
timeout.ReadIntervalTimeout = 600
case 700
timeout.ReadIntervalTimeout = 700
case 800
timeout.ReadIntervalTimeout = 800
case 900
timeout.ReadIntervalTimeout = 900
case 1000
timeout.ReadIntervalTimeout = 1000
else
timeout.ReadIntervalTimeout = 0
endselect

SELECT dwReadTotalTimeout
case 25
timeout.ReadTotalTimeoutConstant = 25
case 50
timeout.ReadTotalTimeoutConstant = 50
case 75
timeout.ReadTotalTimeoutConstant = 75
case 100
timeout.ReadTotalTimeoutConstant = 100
case 125
timeout.ReadTotalTimeoutConstant = 125
case 150
timeout.ReadTotalTimeoutConstant = 150
case 175
timeout.ReadTotalTimeoutConstant = 175
case 200
timeout.ReadTotalTimeoutConstant = 200
case 225
timeout.ReadTotalTimeoutConstant = 225
case 250
timeout.ReadTotalTimeoutConstant = 250
else
timeout.ReadTotalTimeoutConstant = 0
endselect

IF(SetCommTimeouts(hcom,dcb1) = 0)
hcom = -1
end if
else
hcom = -1
END if


IF(GetCommState(hcom,dcb1))
SELECT Baud
CASE 110
dcb1.BaudRate = 110
            CASE 300
               dcb1.BaudRate = 300
            CASE 600
               dcb1.BaudRate = 600
            cASE 1200
               dcb1.BaudRate = 1200
            cASE 2400
               dcb1.BaudRate = 2400
            CASE 4800
               dcb1.BaudRate = 4800
            CASE 9600
               dcb1.BaudRate = 9600
            CASE 14400
               dcb1.BaudRate = 14400
            CASE 19200
               dcb1.BaudRate = 19200
            CASE 38400
               dcb1.BaudRate = 38400
            CASE 57600
               dcb1.BaudRate = 57600
            CASE 115200
               dcb1.BaudRate = 115200
            cASE 128000
               dcb1.BaudRate = 128000
            CASE 256000
               dcb1.BaudRate = 256000
            ELSE
               RETURN 0
         ENDSELECT
         
         SELECT Bits
CASE 5
dcb1.ByteSize = 5
            CASE 6
               dcb1.ByteSize = 6
            CASE 7
               dcb1.ByteSize = 7
            CASE 8
               dcb1.ByteSize = 8
            ELSE
               RETURN 0
ENDSELECT

         SELECT StopBit
CASE 1
dcb1.StopBits = 1
            CASE 2
               dcb1.StopBits = 2
            ELSE
               dcb1.StopBits = 0
         ENDSELECT

         SELECT UCASE$(Parity)
CASE "E"
dcb1.Parity = 2
            CASE "M"
               dcb1.Parity = 3
            CASE "O"
               dcb1.Parity = 1
            CASE "S"
               dcb1.Parity = 4
            ELSE
               dcb1.Parity = 0
ENDSELECT                       
         
         SELECT rtsCtrl
CASE 0
dcb1.fRtsControl = 0
            CASE 1
               dcb1.fRtsControl = 1
            CASE 2
               dcb1.fRtsControl = 2
            ELSE
               RETURN 0
END SELECT
         
         SELECT CASE dtrCtrl
CASE 0
dcb1.fDtrControl = %DTR_CONTROL_DISABLE
            CASE 1
               dcb1.fDtrControl = %DTR_CONTROL_ENABLE
            CASE 2
               dcb1.fDtrControl = %DTR_CONTROL_HANDSHAKE
            ELSE
                RETURN 0
ENDSELECT
                                                               
         IF(SetCommState(hcom,dcb1)) = 0
hcom = -1
ENDIF
else
hcom = -1
endif
ENDIF
RETURN hcom
ENDSUB

SUB ReadCom(UINT hPort,STRING Msg BYREF,INT mLen),INT
INT bytesread=0

IF ReadFile(hPort,Msg,mLen,bytesread,0) = 0
bytesread=0
ENDIF
RETURN bytesread
ENDSUB

SUB WriteCom(INT hPort,STRING msg,INT mLen),INT
INT written=0

IF mLen>LEN(msg)
mLen=LEN(msg)
ENDIF

IF WriteFile(hPort,msg,mLen,written,0)=0
written=0
ENDIF
RETURN written
ENDSUB

SUB CloseCom(INT hPort),INT
RETURN CloseHandle(hPort)
ENDSUB

SUB BytesInBuffer(int hport),int
int bytes

bytes=GetFileSize(hport,2048)

RETURN bytes
ENDSUB

SUB WriteByte (int hPort,byte msg)
INT written=0

IF WriteFile(hPort,msg,1,written,0)=0
written=0
ENDIF
RETURN written
ENDSUB

SUB WriteBytes (int hPort,pointer msg, int mLen)
INT written=0

IF WriteFile(hPort,msg,mLen,written,0)=0
written=0
ENDIF
RETURN written
ENDSUB

SUB ReadByte(INT hPort,byte bite BYREF),INT
INT bytesread=0

IF ReadFile(hPort,bite,1,bytesread,0) = 0
bytesread=0
ENDIF
RETURN bytesread
ENDSUB

SUB ReadBytes (int hPort,POINTER msg, int mLen), INT
INT bytesread = 0

IF ReadFile(hPort,msg,mLen,bytesread,0) = 0
bytesread = 0
ENDIF
RETURN bytesread
ENDSUB

SUB Set_RTS_Line(int hPort, int status)
endsub

SUB Set_DTR_Line(int hPort, int status)
endsub

SUB Set_TXD_Line(int hPort, int status)
endsub

SUB Get_CTS_line(int hPort), int
endsub

sub Get_DSR_line(int hPort), int
endsub

SUB Get_CD_Line(int hPort), int
endsub

SUB Get_RING_Line(INT hPort), int
endsub

SUB Set_RX_BufferSize(int hPort, INT nLenSize)
endsub

SUB Set_TX_BufferSize(int hPort, INT nLenSize)
endsub

SUB Get_Tx_Buffer(int hPort), int
endsub

SUB Get_Rx_Buffer(int hPort), int
endsub


LarryMc

Techno
All your questions are answered in the Communications section of the Windows API.

Do you have a specific IWBasic question?
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library