RS232 Serial communication with PR4000 controller from MKS - serial-port

I am trying to establish a serial connection via an RS232 port on the PR4000 controller from MKS. This controller is connected to a pressure gauge, and I try to read the pressure from my PC with the following script:
import time
import serial
import bitarray
ba = bitarray.bitarray()
# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
port='COM7',
baudrate=9600,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.SEVENBITS
)
#ser.isOpen()
print ('Enter your commands below.\r\nInsert "exit" to leave the application.')
data_in=0
while True :
data_in = input(">> ")
if data_in == 'exit':
ser.close()
break
else:
ser.write((data_in).encode('utf-8'))
out = ''
time.sleep(0.1)
while ser.inWaiting() > 0:
out = ser.read(ser.inWaiting()).decode('utf8')
if out != '':
print (out)
This code is inspired from this post :
Full examples of using pySerial package
you can find the doc of the controller here :
https://www.idealvac.com/files/manuals/PR4000_InstructionManual.pdf
The interface chapter start at page 43.
basically, the RS interface works with an requests and answers syntax.
example of answer :
RT,ON : set the remote on the controller.
?RT : ask for the state of the remote mode.
I managed to establish the connection with hyper terminal
But with python, I've tried to enter the commands and I can't have any answers, the serial buffer is empty.
Do you think the problem is in the format of the requests ?

Do you think the problem is in the format of the requests ?
A command to the device needs to be terminated with a carriage return character. Suggest you append a CR character (i.e. '\r') to data_in before it is sent.
Refer to the code that inspired your version for an example.

Related

data sending from jetson to arduino

It was successful to train from jet son-XAVIER and recognize it as cam. But I don't know how to send the information that jet son-XAVIER recognized in real time to Arduino. The objects we recognize are elk and wild boar. I know that I can only send 1 letter through communication, so elk will send e and wild boar to w. Is there a way to send it as soon as it recognizes it through real-time web cam?
There's not a lot of information here on your setup, but here's a possible solution:
I see the NVIDIA Jetson AGX Xavier module has USB-C ports.
Buy a USB-A to USB-C cable, and plug the Arduino directly in.
I'm not sure what program/language you're using with your trained model, but I'll guess that it's python for now.
You'll want to open a Serial connection to the arduino, and you can do this with pyserial:
https://pypi.org/project/pyserial/
You can send more than just one letter, you can send entire data streams. But, if you want to just send one letter, it will do that as well.
Here's the official documentation for how to communicate with an Arduino using Python:
https://create.arduino.cc/projecthub/ansh2919/serial-communication-between-python-and-arduino-e7cce0
If you're not using python, specify your language of choice, and we can look up and see if it has a serial library.
I have never used darknet but may be this can point you in the right direction.
I have used the library sugested by Bucky and I believe you could add the serial comunication to darknet.py. This is what I would do:
#Add this import at begining of the file darknet.py
import serial
#########################################################
#this is a mocked version of detect in darknet.py, assuming that the labels you used are "elk" and "wildboard". You should not add this lines to the file.
def detect():
res = []
res.append(("elk",0.98,(2,2,50,50)))
res.append(("wildboard",0.98,(2,2,50,50)))
return res
r = detect()
##########################################################
#Add this after the 'print r' at the end of the file darknet.py
ser = serial.Serial('/dev/ttyUSB0') # open serial port. You should check what serial port is assigned to your arduino.
for obj in r:
if obj[0]=="elk" and obj[1]>=0.9: #assuming 0.9 as the minimum confident for a detection
print "found elk"
ser.write('e') # you can send a string with just one letter
elif obj[0]=="wildboard" and obj[1]>=0.9:
print "found wildboard";
ser.write('w') # you can send a string with just one letter
ser.close() # close port

Telnetlib doesn't seem to be passing .write commands

I'm a python noob here just trying to learn a new skill so please be gentle :)
After executing the following script it appears that my script creates a telnet session and I can see the cisco devices banner but is not passing the username/password when prompted.
I've tried changing tn.read_until() and tn.read_very_eager() neither of which is triggering the script to move on to write the desired input. I've also tried forcing the username and password to be input as a byte as well as adding + '\n' to my write functions. I've also used sleep to pass a little extra time to wait for the banner to finish printing out.
tldr: when executing the script I see the username prompt, but can't get further than that.
any assistance here is welcomed.
'''
from time import sleep
import telnetlib
from telnetlib import Telnet
from getpass import getpass
Username = input('please provide your username ')
password = getpass()
# f is the .txt document that lists the IP's we'll be using.
f = open('devicess.txt')
for line in f:
print ('Configuring Device ' + (line))
device = (line)
#open connection to variable
tn = telnetlib.Telnet(device)
#For those devices in the above list, connect and run the below commands
for device in f:
tn.expect('Username: ')
tn.write(Username)
tn.expect('Password: ')
tn.write(password)
tn.write('show ver')
print('connection established to ' + device)
tn.write('logout')
output = tn.read_all()
print(output)
print('script complete')
'''

Cannot set receiver phone number using AT+CMGS="XXXXXXXXX" returns error 325

I am using SIM808 to send SMS to a perticuar number. But when trying to set the number using AT+CMGS=XXXXXXX returns +CMS ERROR:325. I have set the AT+CSCS to GSM but still no luck.The following is the code:
import serial
import os, time
# Enable Serial Communication
port = serial.Serial("/dev/ttyUSB0", baudrate=9600, timeout=1)
# Transmitting AT Commands to the Modem
# '\r\n' indicates the Enter key
port.write('AT'+'\r\n')
rcv = port.read(10)
print rcv
port.write('AT+CMGF=1\r\n')
time.sleep(10)
rcv = port.read(10)
print rcv
port.write('AT+CMGS=\'9912345678\'\r\n')
time.sleep(2)
port.write('test msg')
time.sleep(2)
port.write(chr(26))
rcv = port.read(10)
print rcv
port.flush()
SIM808 expects that AT+CMGS command should enclose the mobile/cell number in double quotes. You have provided escape sequence for single quote.
Your code should be :
port.write("AT+CMGS=\"9912345678\"\r\n")
instead of
port.write('AT+CMGS=\'9912345678\'\r\n')
Because you are providing single quotes escape sequence you get +CMS ERROR:325 error.
While providing mobile/cell number it is an good practice to include country code (in your case +91).

Write and read from a serial port

I am using the following python script to write AT+CSQ on serial port ttyUSB1.
But I cannot read anything.
However, when I fire AT+CSQ on minicom, I get the required results.
What may be the issue with this script?
Logs:
Manual Script
root#imx6slzbha:~# python se.py
Serial is open
Serial is open in try block also
write data: AT+CSQ
read data:
read data:
read data:
read data:
Logs:
Minicom console
1. ate
OK
2. at+csq
+CSQ: 20,99
3. at+csq=?
OKSQ: (0-31,99),(99)
How can I receive these results in the following python script?
import serial, time
#initialization and open the port
#possible timeout values:
# 1. None: wait forever, block call
# 2. 0: non-blocking mode, return immediately
# 3. x, x is bigger than 0, float allowed, timeout block call
ser = serial.Serial()
ser.port = "/dev/ttyUSB1"
ser.baudrate = 115200
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
ser.timeout = None #block read
#ser.timeout = 0 #non-block read
ser.timeout = 3 #timeout block read
ser.xonxoff = False #disable software flow control
ser.rtscts = False #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False #disable hardware (DSR/DTR) flow control
ser.writeTimeout = 2 #timeout for write
try:
ser.open()
print("Serial is open")
except Exception, e:
print "error open serial port: " + str(e)
exit()
if ser.isOpen():
try:
print("Serial is open in try block also")
ser.flushInput() #flush input buffer, discarding all its contents
ser.flushOutput()#flush output buffer, aborting current output
#and discard all that is in buffer
#write data
ser.write("AT+CSQ")
time.sleep(1)
# ser.write("AT+CSQ=?x0D")
print("write data: AT+CSQ")
# print("write data: AT+CSQ=?x0D")
time.sleep(2) #give the serial port sometime to receive the data
numOfLines = 1
while True:
response = ser.readline()
print("read data: " + response)
numOfLines = numOfLines + 1
if (numOfLines >= 5):
break
ser.close()
except Exception, e1:
print "error communicating...: " + str(e1)
else:
print "cannot open serial port "
You have two very fundamental flaws in your AT command handling:
time.sleep(1)
and
if (numOfLines >= 5):
How bad are they? Nothing will ever work until you fix those, and by that I mean completely change the way you send and receive command and responses.
Sending AT commands to a modem is a communication protocol like any other protocols, where certain parts and behaviours are required and not optional. Just like you would not write a HTTP client that completely ignores the responses it gets back from the HTTP server, you must never write a program that sends AT commands to a modem and completely ignores the responses the modem sends back.
AT commands are a link layer protocol, with with a window size of 1 - one. Therefore after sending a command line, the sender MUST wait until has received a response from the modem that it is finished with processing the command line, and that kind of response is called Final result code.
If the modem uses 70ms before it responds with a final result code you have to wait at least 70ms before continuing, if it uses 4 seconds you have to wait at least 4 seconds before continuing, if it uses several minutes (and yes, there exists AT commands that can take minutes to complete) you have to wait for several minutes. If the modem has not responded in an hour, your only options are 1) continue waiting, 2) just give up or 3) disconnect, reconnect and start all over again.
This is why sleep is such a horrible approach that in the very best case is a time wasting ticking bomb. It is as useful as kicking dogs that stand in your way in order to get them to move. Yes it might actually work some times, but at some point you will be sorry for taking that approach...
And regarding numOfLines there is no way anyone in advance can know exactly how many lines a modem will respond with. What if your modem just responds with a single line with the ERROR final result code? The code will deadlock.
So this line number counting has to go completely away, and instead your code should be sending a command line and then wait for the final result code by reading and parsing the response lines from the modem.
But before diving too deep into that answer, start by reading the V.250 specification, at least all of chapter 5. This is the standard that defines the basics of AT command, and will for instance teach you the difference between a command and a command line. And how to correctly terminate a command line which you are not doing, so the modem will never start processing the commands you send.

Gstreamer automation in STB (Set Top Box)

mates,I have a set top box which I am communicating through serial port.This box has Gstreamer media frame-work(linux platform and C language).
I am trying to automate Gstreamer i.e gst-launch,gst-inspect....there are also other frame work like Qt which I want to automate.
Following are my attempts toward this problems :
Attempt 1:
Tried using Pyserial and was successful toward working of it,but by using Pyserial I was able to access my port and communicate to my board,but I found no way to automate things.
import serial
import time
port = "COM1"
baud = 115200
ser = serial.Serial(port, baud,xonxoff=False, rtscts=False, dsrdtr=False,timeout=None)
ser.flushInput()
ser.flushOutput()
if ser.isOpen():
print(ser.name + ' is open...')
while True :
cmd = input("Enter command or 'exit':")
if cmd == 'exit':
ser.close()
exit()
else:
ser.write(cmd.encode() + b'\r\n' )
bytesToRead = ser.inWaiting()
out=ser.read(bytesToRead)
print(out.decode(),sep='')
Attempt 2 :
To have a communicator install in my board which can communicate to my box.
If this is correct ,I have no Idea how to proceed with this.
Any help toward STB automation will be greatly appreciated.

Resources