SerialPort.pb

Linux specific forum
User avatar
dhouston
Enthusiast
Enthusiast
Posts: 430
Joined: Tue Aug 21, 2007 2:44 pm
Location: USA (Cincinnati)
Contact:

SerialPort.pb

Post by dhouston »

The SerialPort.pb example given with PB4.40B6 uses "stty0" for the port under Linux. This should be "ttyS0" or "ttyUSB0", etc.
Fred
Administrator
Administrator
Posts: 16664
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: SerialPort.pb

Post by Fred »

Good one, it's corrected.
User avatar
dhouston
Enthusiast
Enthusiast
Posts: 430
Joined: Tue Aug 21, 2007 2:44 pm
Location: USA (Cincinnati)
Contact:

Re: SerialPort.pb

Post 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?
Fred
Administrator
Administrator
Posts: 16664
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: SerialPort.pb

Post by Fred »

Better test with <> 0 ( If OpenSerialPort() ). IIRC it should be positive on linux, as negative numbers are for the errorcode.
User avatar
dhouston
Enthusiast
Enthusiast
Posts: 430
Joined: Tue Aug 21, 2007 2:44 pm
Location: USA (Cincinnati)
Contact:

Re: SerialPort.pb

Post by dhouston »

Does OpenSerialPort lock the file so other apps cannot also open it?
Fred
Administrator
Administrator
Posts: 16664
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: SerialPort.pb

Post by Fred »

I don't think so.
santiago.barreiro
New User
New User
Posts: 1
Joined: Mon Feb 08, 2010 2:04 pm

Re: SerialPort.pb

Post 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
Santiago
Fred
Administrator
Administrator
Posts: 16664
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: SerialPort.pb

Post 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.
User avatar
dhouston
Enthusiast
Enthusiast
Posts: 430
Joined: Tue Aug 21, 2007 2:44 pm
Location: USA (Cincinnati)
Contact:

Re: SerialPort.pb

Post 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
dibor
Enthusiast
Enthusiast
Posts: 125
Joined: Wed May 20, 2020 5:19 pm
Location: The 3rd planet in the Solar System
Contact:

Re: SerialPort.pb

Post by dibor »

Hi.
Where IsNumeric procedure?
Got error "procedure has been declared but not defined"

Thank you.
Mac Studio M1Max, PB 6.03Arm64 and x64.
Macbook Air M2, PB 6.03Arm64 and x64.
Windows 10, PB 6.03 x64 and x86.
Post Reply