It is currently Wed Jan 27, 2021 8:08 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: SerialPort.pb
PostPosted: Fri Nov 06, 2009 8:01 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Tue Aug 21, 2007 2:44 pm
Posts: 430
Location: USA (Cincinnati)
The SerialPort.pb example given with PB4.40B6 uses "stty0" for the port under Linux. This should be "ttyS0" or "ttyUSB0", etc.


Top
 Profile  
Reply with quote  
 Post subject: Re: SerialPort.pb
PostPosted: Sat Nov 07, 2009 12:14 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 14181
Location: France
Good one, it's corrected.


Top
 Profile  
Reply with quote  
 Post subject: Re: SerialPort.pb
PostPosted: Mon Nov 09, 2009 8:35 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Tue Aug 21, 2007 2:44 pm
Posts: 430
Location: USA (Cincinnati)
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: SerialPort.pb
PostPosted: Mon Nov 09, 2009 8:37 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 14181
Location: France
Better test with <> 0 ( If OpenSerialPort() ). IIRC it should be positive on linux, as negative numbers are for the errorcode.


Top
 Profile  
Reply with quote  
 Post subject: Re: SerialPort.pb
PostPosted: Sat Nov 14, 2009 12:46 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Tue Aug 21, 2007 2:44 pm
Posts: 430
Location: USA (Cincinnati)
Does OpenSerialPort lock the file so other apps cannot also open it?


Top
 Profile  
Reply with quote  
 Post subject: Re: SerialPort.pb
PostPosted: Mon Nov 23, 2009 6:11 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 14181
Location: France
I don't think so.


Top
 Profile  
Reply with quote  
 Post subject: Re: SerialPort.pb
PostPosted: Mon Feb 08, 2010 3:12 pm 
Offline
New User
New User

Joined: Mon Feb 08, 2010 2:04 pm
Posts: 1
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: SerialPort.pb
PostPosted: Mon Feb 08, 2010 3:48 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 14181
Location: France
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: SerialPort.pb
PostPosted: Mon Feb 08, 2010 3:51 pm 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Tue Aug 21, 2007 2:44 pm
Posts: 430
Location: USA (Cincinnati)
santiago.barreiro wrote:
As for now, I simply want to find out the which serial ports are available...
http://www.purebasic.fr/english/viewtopic.php?f=15&t=31049

I think this is my latest version...
Code:
;
; ------------------------------------------------------------
;
;  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


Top
 Profile  
Reply with quote  
 Post subject: Re: SerialPort.pb
PostPosted: Thu May 21, 2020 8:23 am 
Offline
User
User

Joined: Wed May 20, 2020 5:19 pm
Posts: 29
Hi.
Where IsNumeric procedure?
Got error "procedure has been declared but not defined"

Thank you.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye