Page 1 of 1

SerialPort.pb

Posted: Fri Nov 06, 2009 8:01 pm
by dhouston
The SerialPort.pb example given with PB4.40B6 uses "stty0" for the port under Linux. This should be "ttyS0" or "ttyUSB0", etc.

Re: SerialPort.pb

Posted: Sat Nov 07, 2009 12:14 pm
by Fred
Good one, it's corrected.

Re: SerialPort.pb

Posted: Mon Nov 09, 2009 8:35 pm
by dhouston
Thanks, Fred.

OpenSerialPort says it will return 0 if the port cannot be opened. Will the number returned always be >=0? i.e. Can we test using if OpenSerialPort(blah,blah,etc.)>0?

Re: SerialPort.pb

Posted: Mon Nov 09, 2009 8:37 pm
by Fred
Better test with <> 0 ( If OpenSerialPort() ). IIRC it should be positive on linux, as negative numbers are for the errorcode.

Re: SerialPort.pb

Posted: Sat Nov 14, 2009 12:46 pm
by dhouston
Does OpenSerialPort lock the file so other apps cannot also open it?

Re: SerialPort.pb

Posted: Mon Nov 23, 2009 6:11 pm
by Fred
I don't think so.

Re: SerialPort.pb

Posted: Mon Feb 08, 2010 3:12 pm
by santiago.barreiro
Hello everyone

I have programming VB for as long as it exist. Although I begun with Atari, Commodor, Amiga and QBasic, definitly, PureBasic is a very different dialect of basic.

I am intending to do a simple RS232 reading that must be dump to a file with a date-time-pcname signature stamp. The file must be named simply at yyyy\mm\dd.log. But, I havenĀ“t got there so far.

As for now, I simply want to find out the which serial ports are available... The computer has 1 physical port and 3 bluetooth serial ports, none of which shows in the following code:

Code: Select all

Enumeration 
#port1=1
#port2
#port3
#port4
#port5
#port6
#port7
#port8
#port9
#port10
#port11
#port12
EndEnumeration

If OpenConsole ()
iResult=IsSerialPort(#port1)
PrintN("Result("+Str(#port1)+":"+Str(iResult))
iKey$=Input()  
EndIf
But, I could not find (easily at least) how to loop for searching without reapiting the code over.. since
IsSerialPort expects a constant and not a variable.

Can some one help me with these two issues?

Regards

Re: SerialPort.pb

Posted: Mon Feb 08, 2010 3:48 pm
by Fred
Hello, IsSerialPort() is only here to test if the PB object is initialized. There is currently no functions to natively enumerate the available port. On linux it should be easy enough to execute a commandline via RunProgram() and parse the result to get it.

Re: SerialPort.pb

Posted: Mon Feb 08, 2010 3:51 pm
by dhouston
santiago.barreiro wrote:As for now, I simply want to find out the which serial ports are available...
http://www.purebasic.fr/english/viewtop ... 15&t=31049

I think this is my latest version...

Code: Select all

;
; ------------------------------------------------------------
;
;  Enumerate Serial Ports
;
; ------------------------------------------------------------
;
EnableExplicit

#O_RDONLY   = 0
#O_WRONLY   = 1
#O_RDWR      = 2
#O_NOCTTY   = $100
#O_NONBLOCK = $800
#TIOCMGET   = $5415
#TIOCMSET   = $5418
#SET_DTR    = %000000000010    ;$002
#CLR_DTR    = %111111111101    ;~#SET_DTR

Global ports.s, Pid.s,ComPort.s
Global fd
Declare IsNumeric(some.s) 

Procedure GetAvailablePorts()
  Protected buf.s=Space(5),out.s=Space(10),dev.s,output.s,LineIn.s,found.s,key.s,bad.s
  Protected ps,dmesg,fgrep,tee,i,t,s,u,c
  ps=RunProgram("/bin/ps", "-A","",#PB_Program_Open|#PB_Program_Read)
  fgrep=RunProgram("/bin/fgrep", "SerialTest","",#PB_Program_Open|#PB_Program_Connect|#PB_Program_Read,ps)
  tee = RunProgram("/usr/bin/tee","PID.txt",GetCurrentDirectory(),#PB_Program_Open|#PB_Program_Connect|#PB_Program_Read,fgrep)
  While ProgramRunning(tee)
    Pid=StringField(Trim(ReadProgramString(tee)),1," ")
  Wend 
  WaitProgram(tee)
  CloseProgram(ps):CloseProgram(tee)
  ;-----enum ports & write tty.txt-----
  DeleteFile("tty.txt")
  gtk_init_(0,0)
;  path=GetCurrentDirectory()
;  MessageRequester("PATH",path)
;  MessageRequester("App.Path",GetPathPart(ProgramFilename()))
;  path=GetPathPart(ProgramFilename())
  dmesg = RunProgram("/bin/dmesg","","",#PB_Program_Open|#PB_Program_Read)
  fgrep = RunProgram("/bin/fgrep","tty","",#PB_Program_Open|#PB_Program_Connect|#PB_Program_Read,Dmesg)
  tee = RunProgram("/usr/bin/tee","tty.txt",GetCurrentDirectory(),#PB_Program_Open|#PB_Program_Connect|#PB_Program_Read,Fgrep)
  WaitProgram(tee)
  CloseProgram(Dmesg):CloseProgram(Fgrep):CloseProgram(Tee)
  ;-----parse tty.txt-----
  ports=""
  If ReadFile(0,GetCurrentDirectory()+"tty.txt")
    While Eof(0) = 0 
      LineIn=ReadString(0)+" "
      t=FindString(LineIn,"tty",1)
      If t>0
        s=FindString(LineIn," ",t)
        dev=Trim(Mid(LineIn,t,s-t))     
        If Right(dev,1)=":":dev=Left(dev,Len(dev)-1):EndIf
        If Not IsNumeric(Mid(dev,4,1))
          c=CountString(LineIn,"tty")
          If c=1      
            RunProgram("stty","-F /dev/"+dev+" 19200 cread -echo -icanon","")
            fd=open_("/dev/"+dev,#O_NONBLOCK|#O_NOCTTY|#O_RDWR,0)  
            If fd>0
              If FindString(ports,dev,1)=0
                If Len(ports):ports+",":EndIf
                ports+"/dev/"+dev
              EndIf  
              close_(fd)
            EndIf             
          ElseIf (c=2) 
            s=Val(Mid(dev,5,2))                                               ;first index
            t=FindString(LineIn,"tty",t+Len(dev))                   ;find second tty?xx
            c=Val(Mid(LineIn,t+4,2))                                       ;second index
            If (c>s)
              For i=s To c
                dev=Left(dev,4)+Str(i)       
                RunProgram("stty","-F /dev/"+dev+" 19200 cread -echo -icanon","")
                fd=open_("/dev/"+dev,#O_NONBLOCK|#O_NOCTTY|#O_RDWR,0)  
                If fd>0
                  If FindString(ports,dev,1)=0
                    If Len(ports):ports+",":EndIf
                    ports+"/dev/"+dev
                  EndIf  
                  close_(fd)
                EndIf                 
              Next
            ElseIf FindString(LineIn,"-",1)>0
              MessageRequester("Serial Port Enumeration","Unexpected dmesg format - Copy tty.txt to roZetta author"+#CRLF$+LineIn)
            EndIf
          Else
            MessageRequester("Serial Port Enumeration","Unexpected dmesg format - Copy tty.txt to roZetta author"+#CRLF$+LineIn)
          EndIf  
        EndIf
      EndIf
    Wend
    CloseFile(0)
  EndIf 
;Debug ports  
  OpenPreferences(AppPath+"serial.prf"):PreferenceGroup("port")
    WritePreferenceString("found",ports)
  ClosePreferences()  
EndProcedure

Re: SerialPort.pb

Posted: Thu May 21, 2020 8:23 am
by dibor
Hi.
Where IsNumeric procedure?
Got error "procedure has been declared but not defined"

Thank you.