OSX, PB & USB-RS232 serial ports: SOLVED
Posted: Tue Aug 24, 2010 7:21 pm
I am ready to port my application, which is working under Windows & Linux, to OSX. It interfaces with an embedded device via RS232. I have code working under both Windows and Linux that automatically enumerates all of the serial ports on the system. Under OSX I will have to interface with USB-RS232 adapters and am hoping that someone here has a little experience with this.
It looks like my Linux code to enumerate the ports will be useless.
I still have to dig a couple of USB-RS232 adapters out of my parts bin and plug them in to my Mac Mini. How does OSX report their presence?
It looks like my Linux code to enumerate the ports will be useless.
Code: Select all
Procedure EnumSerialPorts() ;enumerate serial ports
#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
Protected dev.s,LineIn.s,found.s
Protected ps,dmesg,fgrep,tee,i,t,s,u,c
NewList Comports.s()
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",AppPath,#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(AppPath+"tty.txt")
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",AppPath,#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,AppPath+"tty.txt")
While Eof(0) = 0
LineIn=ReadString(0)+" "
t=FindString(LineIn,"tty",1) ;Tibbo uses vspdN
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)
fd=OpenSerialPort(#PB_Any,"/dev/"+dev,19200,#PB_SerialPort_NoParity,8,1,#PB_SerialPort_NoHandshake,1024,1024)
If fd<>0
AddElement(ComPorts())
ComPorts()="/dev/"+dev
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)
fd=OpenSerialPort(#PB_Any,"/dev/"+dev,19200,#PB_SerialPort_NoParity,8,1,#PB_SerialPort_NoHandshake,1024,1024)
If fd<>0
AddElement(ComPorts())
ComPorts()="/dev/"+dev
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
SortList(ComPorts(),0)
ForEach ComPorts()
If FindString(ports,ComPorts(),1)=0 ;avoid duplicates
If Len(ports):ports+",":EndIf
ports+ComPorts()
EndIf
Next
;Debug ports
OpenPreferences(AppPath+"serial.prf"):PreferenceGroup("port")
WritePreferenceString("found",ports)
ClosePreferences()
EndProcedure