Open Com With Root Privileges - Linux [solved]

For issues with communication ports, protocols, etc.
Post Reply
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Open Com With Root Privileges - Linux [solved]

Post by mark bower »

I am talking from FB to an HP multimeter set to 9600 baud, even, 7, 2. The following code previously worked about 10 years ago and records help I received to get things going at that time. Now the code returns "0.000"s when when volts are present. I compiled the code on the present computer, 32 bit processor. Would appreciate suggestions?

Code: Select all

'file: HP1_rs232.bas 16 dec 09 modified 22aug19
'XP, null modem cable, HP34401A
'baud on meter set to 9600
'working code thks to Phishguy & Nobozoz 
'linux code thks to Phishguy
'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,7,2,cs0,ds0,cd0,rs,LF"                               'Windows   cs0 ds0->req'd'  'TB8192,RB8192"  
scom = "/dev/ttyS0:9600,e,7,2,cs0,ds0,cd0,rs"                           'Linux
 
Open Com scom AS 1   
                   
Print #1, "*CLS"
Print #1, ":SYST:REM"                                   'System:Remote     Sleep absolutely necessary w >1Ghz CPU
Print #1,":CONF:VOLT:DC 10,.001"   
sleep 500                                               'Sleep absolutely necessary w >1Ghz CPU, 30  intermittent                            
                                                        'use > 30 to be safe
for j = 1 to 2
    Print #1, ":READ?"
    Input #1,resp
    sleep 500
    V=Val(resp)
    Locate j,1
    Print Using "##.####";V
next[code]

sleep
end
Last edited by mark bower on Aug 27, 2019 3:10, edited 2 times in total.
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Open Com syntax

Post by coderJeff »

What is the actual contents of variable "resp"? You must be getting something if program makes it past the INPUT statement.

Add a print "*" & resp & "*" after the input statement. That might help determine if it's a coding problem or a hardware problem; like a fuse blown in the meter leads, (assuming there is a fuse, obviously). Does the meter have it's own display too?
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Re: Open Com syntax

Post by mark bower »

I do not know what the input looks like. Using your code insert shown below, all i get is two asterisks, as **. I should say the HP meter is set to RS-232, and briefly displays "RS-232" when turned on. And yes, there is a front display showing volts input (eg, 1.35VDC)

Code: Select all

for j = 1 to 2
    Print #1, ":READ?"
    Input #1,resp
    print "*" & resp & "*"
    sleep 1000
    V=Val(resp)
    Locate j,1
    Print Using "##.####";V
next
mark
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Re: Open Com syntax [solved]

Post by mark bower »

The problem is that the code must be launched as root/superuser. I found a hint of this by googling, but then revisited my notes from 10 years ago and they stated the requirement. I simply forgot about this requirement.

So my complied program to read the HP multimeter via Open Com must be executed on the command line with root privileges as "sudo ./file" (with out quotes).
And the Input to the program from the HP meter is +1.35330000E +00, when the meter displays 01.353 volts.
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Open Com syntax

Post by coderJeff »

Oh, I'm glad you were able to figure it out, because I was way off.

Compiling the code with '-exx' option will enable run time error checking, which may have helped in locating the problem sooner. Or Open Com can also be used like a function to get an error code to check for failure or success.
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Re: Open Com syntax

Post by mark bower »

I appreciated your interest even if it did not work. Still, looking at the Input per you suggestion was useful and I have taken note.

I remember your user name from when I left off years ago. Also, I note that we both "joined the FB band wagon" within a month of each other.
mark
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Open Com syntax

Post by caseih »

mark, are you using Linux? superuser should not be required to access a serial port in Linux. Instead you need to make your normal user a part of the built-in "dialout" group. Once you do that, then you can access serial ports as a normal user. Most linux distributions have GUI tools for manipulating users and groups, but if not you can always do it the command-line way. As arduino requires serial port access, they've helpfully provided a guide to doing this, which would apply to you even though you're not using arduino: https://www.arduino.cc/en/guide/linux (scroll down a ways)
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Re: Open Com syntax

Post by mark bower »

oh thanks. superuser is a pain. i will take a look at the link tomorrow, and if trouble, i'll be back, but may be a bit of time before doing so.
mark
mark bower
Posts: 395
Joined: Dec 28, 2005 6:12
Location: arcadia, CA USA

Re: Open Com syntax

Post by mark bower »

@caseih
You saved the day. I went to the link you provided and it did the trick with its code example. I converted the CLI code for my system and I can now execute without having to explicitly appear as root. For possible use for others RS-232ing to COM 1 (ttyS0), the following is what I used to avoid the "sudo ./file" syntax execute.

mark@mark:~$ ls -l /dev/ttyS0
crw-rw---- 1 root dialout 4, 64 Aug 26 08:39 /dev/ttyS0
mark@mark:~$ sudo usermod -a -G dialout mark
[sudo] password for mark:
enter password
mark@mark:~$

The privilege change saves me a lot of time as I have a huge (for me) debugging problem, and correspondingly a lot of system reboots.
mark
Post Reply