Page 1 of 1

RS232 query device

Posted: Wed Aug 06, 2008 2:21 pm
by johanholstein
Hello all,

I'm a newbie so this is my question, my device is a vacuum meter and to get data out i must send a string ?GA1 the device responds with science notation say 1.00E3 or 5.00E-4 so what i need is to send to the RS232 port the query ?GA1 and get back on the display the value every 200ms. Can somebody help with this ......

Thanks Johan Holstein

Posted: Wed Aug 06, 2008 3:36 pm
by superadnim
PB 4.2x has a serial port lib, which is what you need. However I found that it is somewhat limited on the advanced side of the deal...

You may want to sniff the port when the device is in use with any current existent application, so you get to see what kind of setup it requires (what sort of authentication, etc).

Once that's ready, you can setup the library and send / receive data very easily, just as you would with the windows api.

I'm sure this example WILL NOT work but perhaps with a little adaptation you will get it to work.

Code: Select all

	; Syntax
	; Result = OpenSerialPort(#SerialPort, SerialPortName$, Bauds, Parity, Data, Stop, HandshakeMode, InputBufferSize, OutputBufferSize)

If OpenSerialPort(0, "COM4", 9600, #PB_SerialPort_NoParity, 8, 1, #PB_SerialPort_NoHandshake, 1024, 1024)
	
	Define.l is_there_data_to_read, read_amount
	Define.l timeout_now, timeout_max = 5000
	Define.l incoming = AllocateMemory(1024)
	
	If incoming
		
		WriteSerialPortString(0, "?GA1")
		
		Repeat
			Delay(10) : timeout_now + 10
			is_there_data_to_read = AvailableSerialPortInput(0)
		Until is_there_data_to_read Or (timeout_now => timeout_max)
		
		If is_there_data_to_read
			read_amount = ReadSerialPortData(0, incoming, Length)
			Debug PeekS(incoming, read_amount)
		Else
			Debug "timeout reached / no data to read"
		EndIf
		
		FreeMemory(incoming)
		
	EndIf
	
Else
	Debug "couldn't open com"
EndIf
Make sure you use the right COM and to setup the "connection" with the appropriate info. You may have to setup the "status" with SetSerialPortStatus() as well if signals are non-standard in the device.

I wish I could give you working code but without the device / specs I can't.

Posted: Wed Aug 06, 2008 4:42 pm
by Baldrick
Here is a little bit of a snippet from a little project I was working on a while back. It talks to a microcontroller & recieves return data in Hex format.
As superadnim said, very hard to know what you need without specs on your equipment. In my case, I have to send data 1 character at a time & wait for the controller to echo back before sending the next character. When the controller has recognised several symbols, it will then return a number in Hex format or an error if it doesn't recognise the character sequence sent, so in my case I just send my string, then delimit the total result to remove unwanted echo characters & then return the Hex as a decimal number.
All connection info such as baud rate, dbits.... is setup elswhere within my program.
Poll code:

Code: Select all

Structure Comms 
   Port.l
   hPort.l
   Baud.l
   DatBits.l
   StopBits.l 
   Handshake.l 
   InBuffer.l 
   OutBuffer.l 
   Status.l 
   Parity$ 
   PortName$ 
EndStructure 

Procedure Poll(*myDat.String) 
Shared Serial.Comms 
   With Serial 
    If IsSerialPort(\hPort)  
      For a.l=1 To Len(*myDat\s) 
       sDat$=Mid(*myDat\s,a,1) 
       WriteSerialPortData(\hPort,@sDat$,1) 
       Timer.l=ElapsedMilliseconds() 
        Repeat 
       In.l=AvailableSerialPortInput(\hPort) 
        If In 
         In$=Space(In) 
         ReadSerialPortData(\hPort,@In$,In) 
         Result$+In$  
        EndIf 
       TimerEnd.l=ElapsedMilliseconds() 
        If Timer+35<TimerEnd ; allow up to 35ms for reaction from relay controller
         TimeOut.l=1 
        EndIf 
       Until In Or TimeOut 
      Next 
     Result$=RemoveString(Result$,"S0") :Result$=RemoveString(Result$,"I0") 
     Result$=RemoveString(Result$,#CRLF$) :Result$=RemoveString(Result$,"#")  
      If Result$="" 
       \Status=0 
        Else 
       \Status=1
      EndIf 
     Result.l=@Result$
     ProcedureReturn Hex2Dec(@Result)   
    EndIf 
   EndWith 
EndProcedure 


The code in the main loop where polling is actually done:

Code: Select all

   If Update 
    Update=0     
    Dt$="S0"+#Commit :*Dt=@Dt$
    Status\Relay=Poll(@*Dt) 
    Dt$="I0"+#Commit :*Dt=@Dt$
    Status\Input=Poll(@*Dt) 
Using an timer API for polling period set upon succesfull connection:
Pollrate is set prior to connecting.

Code: Select all

Procedure poll_Timer() 
Shared Update.l
   Update=1 
EndProcedure 

       Case #Button_Connect 
         If Connect() 
          pEv.l=SetTimer_(WindowID(#Window_0),#pEv,PollRate,@poll_Timer()) 
          IpStat=-1 
         EndIf
This API timer is killed upon disconnection:

Code: Select all

       Case #Button_DisConnect 
         If DisConnect() 
          If pEv 
           KillTimer_(WindowID(#Window_0),#pEv) 
          EndIf
         EndIf

Re: RS232 query device

Posted: Fri Dec 19, 2014 11:38 pm
by infratec
If you want to do something with serial port and continiuos data,
you should definately use a thread for the communication part.

Bernd

Re: RS232 query device

Posted: Sat Dec 20, 2014 2:51 am
by skywalk
Remember Ascii compiles are going away, so you need to manage your strings. Writing strings has an Ascii option but ReadSerialPortData() does not. Use PeekS(*buf,-1,#PB_Ascii).

Re: RS232 query device

Posted: Sat Dec 20, 2014 11:15 am
by infratec
There was a troll on his way.
But it was to late, my answer was already written.