RS232 help

New to FreeBASIC? Post your questions here.
Post Reply
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

RS232 help

Post by mark bower »

WinXP, null modem cable, FB 0.17b
I am trying to commuicate with an HP meter via RS232. My attempt at converting the QB code supplied by HP, to FB, has not been successful. I would appreciate help in identifying where the code is in error? I am using a null modem (cross over pins) as stipulated by HP. One of my code line options seems to prove that there is some sort of communication with the meter where I have noted that an error code is received on the meter. But that is the only activity I could get. Pls see following code

Code: Select all

'file: HP.bas

dim as string resp (100)

'9600 baud,even parity, 7 bits,2 stop bits
'rs = supress detection of request to send(RTS) signal
'cd = # of ms to wait for the time-out on data carrier detect (DCD)line 
'LF = adds a linefeed to every carriage rtn
'pe = enable parity check

open com "com1:9600,E,7,2,RS,CD,LF,PE" for random as 1 len = 1000 'no meter activity, blank console,hangs
'open com "com2:9600,E,7,2,RS,CD,LF,PE" for random as 1 len = 1000 'no meter activity, console displays queries
'open "com1:9600,E,7,2,RS,CD,LF,PE" for random as 1 len = 1000  'meter displays an error msg, console displays queries
'open "com2:9600,e,7,2,rs,cd,lf,pe" for random as 1 len = 1000  'no meter activity, console displays queries

'put meter in remote operation
print #1, ":SYST:REM"

'query the meter ID
print #1, "*IDN?"
print "*IDN? returned: ",resp (100)

'ask what revision of SCPI meter conforms to
print #1, ":SYST:VERS?"
print ":SYST:VERS? returned: ",resp(100)

sleep
end
phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

I have some code I wrote for an HP/Agilent meter. I can post it when I get to work tomorrow. Just as a hint, you don't have any input or get statements to receive the response.
maddogg6
Posts: 824
Joined: Dec 07, 2005 22:58
Contact:

Post by maddogg6 »

your never reading the com port... only sending commands ??
but maybe the open com line is doing this, but I dont see any connection to 'resp()' var you have dim'd

seems wrong to me - no?

edit - can you post the QB code from HP - ??

I also wonder about the LEN 1 to 1000 in the open command also.

I would expect something more like
Print#1 "[ command ]" ' send the command to initiate a response from meter
Print "[ command] = "; ' print some info about the command sent
for I = 1 to 1000 ' loop thru the 1000 chars expected to be received
Get #1, resp(i) ' get a char from the port
Print resp(I); 'print each char received
Next I
print ' make a new line

you may need to add line feeds after the Print #1 commands as well before the meter accepts the command.. ??
/edit

Oh - and the DIM is for 100 - but a len of 1000 is set in the open command (that I dont understand)
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Post by mark bower »

o.k., i see where i blatantly omitted inputs from the meter. i added them, still no luck

the following is the original text HP QB code (some notes omitted for brevity)

Code: Select all

Locate 1,1
Dim cmd$(100), resp$(1000) - [cmd$ appears never to be used]

Open "com1:9600,E,7,2,RS,CD,LF,PE" For Random As #1 Len =1000

'put meter in remote operation
Print #1, ":SYST:REM"

'query the meter ID
Print #1, "*IDN?"
Line Input #1, resp$
Print "*IDN? returned: ",resp$

'ask what revision of SCPI meter conforms to
Print #1, ":SYST:VERS?"
Line Input #1, resp$
Print ":SYST:VERS? returned: ",resp$

'send a message to the meter
Print #1, ":SYST:BEEP;:DISP:TEXT 'BP 34401A'"

'config the meter for dc voltage readings - 4 readings
Print #1, ":CONF:VOLT:DC 10, 0.1;:SAMP:COUN 4"
'trigger the readings, and fetch the results
Print #1, ":READ?"
Line Input #1, resp$
Print ":READ? returned: ", resp$

End
nobozoz
Posts: 238
Joined: Nov 17, 2005 6:24
Location: Chino Hills, CA, USA

Post by nobozoz »

This works for me ...

Code: Select all

'Compile: fbc -lang fb -s console -w pedantic -v
'Compiler version: 0.17 (05-11-2007)

cls

Dim As String s_cmd, s_resp, sComOpen 			
Dim As Integer dmmdly = 200
sComOpen = "COM1:" + "9600" + ",N,8,1,BIN,CD0,CS0,DS0,LF,OP0,RS,TB8192,RB8192"
OPEN Com sComOpen As 1

'clear status meter
Print #1, "*cls"
Sleep dmmdly,1

'put meter in remote operation
Print #1, ":SYST:REM"
Sleep dmmdly,1

'query the meter ID
Print #1, "*IDN?"
Sleep dmmdly,1
Line Input #1, s_resp
Print "*IDN? returned: ",s_resp

'ask what revision of SCPI meter conforms to
Print #1, ":SYST:VERS?"
Sleep dmmdly,1
Line Input #1, s_resp
Print ":SYST:VERS? returned: ",s_resp

'send a message to the meter
Print #1, ":SYST:BEEP;:DISP:TEXT 'BP 34401A'"
Sleep dmmdly,1

'config the meter for dc voltage readings - 4 readings
Print #1, ":CONF:VOLT:DC 10, 0.1;:SAMP:COUN 3"
'trigger the readings, and fetch the results
Sleep dmmdly,1
Print #1, ":READ?"
Sleep dmmdly,1
Line Input #1, s_resp
Print ":READ? returned: ", s_resp

'put meter in remote operation
Print #1, ":SYST:LOC"
sleep
End

phishguy
Posts: 1201
Joined: May 05, 2006 16:12
Location: West Richland, Wa

Post by phishguy »

Someone beat me to it. Oh well. Here is my example test program anyway.

Code: Select all

dim resp as string
DIM V AS SINGLE
open com "com1:9600,e,7,2,cs0,ds0,cd0,rs,LF" as 1
SLEEP 100
print #1, ":SYST:REM" 
SLEEP 100
print #1,":CONF:VOLT:DC 10,.01;:SAMP:COUN 1" 
SLEEP 100
DO
print #1, ":READ?"

  INPUT #1,RESP

V=VAL(RESP)
LOCATE 10,10
IF V>.01 OR V<-.01 THEN
PRINT USING "##.##";V
END IF
sleep 100
LOOP UNTIL INKEY$<>""
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Post by mark bower »

grt! both codes get things to happen, the "Rmt" annunciator lite for sure which my code did not do. neither code displays any rtns from the meter, so a little more work. give me some time; i will fiddle with both of the codes supplied to see if i can advance things. i'll rtn as soon as appropriate (either still in trouble or code solve).

thanks

mark
maddogg6
Posts: 824
Joined: Dec 07, 2005 22:58
Contact:

Post by maddogg6 »

the LF ext. option for the OPEN COM - isnt supported in windows according to the help file
'LF' Communicate in ASCII mode (add LF to every CR) - Win32 doesn't support this one
It looks like you should use 'ASC' instead...
'ASC' same as 'LF'
edit: or add the CR/LF manually at the end of PRINT#1 statement..

Print#1, "[ command ]" & chr(13) & chr(10)

and eliminate the 'LF' and 'ASC' open com ext options...

something to try anyway...
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

RS232 solved again

Post by mark bower »

pls see code below for HP34401A meter. code may not be optimal (totally correct), but it works. per maddogg6, deleted LF re chm comment that does not work in Win32, code still functions. help from 3 players got my code going.

Code: Select all

'file: HP.bas 28 Aug 07
'XP, null modem cable, HP34401A
'working code thks to Phisguy & Nobozoz 
'read two consecutive voltages from HP meter set to 10V full scale

Dim as string resp, scom
Dim V as Single
dim J as integer

'9600 baud,even parity, 7 bits,2 stop bits
'rs = supress detection of request to send(RTS) signal
'CDn' Set the Data Carrier Datect(DCD line) duration (in ms) (n>=0), 0 = turn off 
'LF = Communicate in ASCII mode (add LF to every CR) - Win32 doesn't support this one 
'PE = enable parity check
'CSn' Set the CTS duration (in ms) (n>=0), 0 = turn off, default = 1000 
'DSn' Set the DSR duration (in ms) (n>=0), 0 = turn off, default = 1000 
'TBn' Set the 'Transmit Buffer' size (n>=0), 0 = default, depends on platform 
'RBn' Set the 'Receive Buffer' size (n>=0), 0 = default, depends on platform 
'BIN' The opposite of LF and it'll always work 
scom = "com1:9600,e,7,2,cs0,ds0,cd0,rs"  'cs0 ds0->req'd'  'TB8192,RB8192"

Open Com scom AS 1                              'Use Sleep 100 if speed issues or 4800 baud

Print #1, ":SYST:REM"                           'Use Sleep 100 if speed issues or 4800 baud
Print #1,":CONF:VOLT:DC 10,.001;:SAMP:COUN 1"   'Sleep 100 use if speed issues or 4800 baud
                                 
for j = 1 to 2
    Print #1, ":READ?"
    Input #1,RESP
    V=Val(RESP)
    Locate j,1
    Print Using "##.####";V
next

sleep
end
thanks - mark b
Post Reply