In Octave, how to close the serial port after opening with the new "serialport" command syntax - serial-port

The latest documentation for serial communication in Octave explains to use the "serialport" command to open the port instead of the deprecated "serial" command.
https://octave.sourceforge.io/instrument-control/overview.html
There is no explanation of how to close the serial port. I used this to successfully open the port and do some writing
s1=serialport('com5','Baudrate',57600)
num=write(s1,'help')
But I can't figure out how to close the port. I used this:
fclose(s1)
And got this error response
error: file id must be a file object, std::string, or integer value
Does anyone know how to close the serial port?

The octave functions attempt to mimic where possible the Matlan functions, which also does not have a close function.
To close the serial port, set the variable to something else, and if it is not used elsewhere, it will close the port.
# create a serialport references by the s1 variable
s1=serialport('com5','Baudrate',57600)
num=write(s1,'help')
# set s1 to something else
s1 = [];
# s1 was only variable referencing the serial port
# so the serial port is now freed/closed
You could also clear the variable:
clear s1

Related

WebIOPi no connection with I2C device alert

I am working with WebIOPi on a Raspberry Pi 2. The raspberry is connected to an Arduino Nano via I2C (Python script working well with the Nano - a button from a web page can turn on or off LEDs).
Is it possible to make a diagnostic Javascript alert when the connection with the Arduino is broken (if line/wire I2C is broken)?
Thank you.
solved in low level (python script + return some value).
Example python script:
try:
bus.write_byte(address, int(value))
time.sleep(0.01)
number = bus.read_byte(address)
flag = number
except IOError:
number = 11 #error value (my fantasy)
flag = number
"flag" variable used in javascript to recognize connection status and if flag = 11 then comes alert("no connection")

Cannot Read From Multimeter with RS232 Communication

I am currently attempting to use a computer and RS232 connection to control a HP 34401A Multimeter. I am able to use commands such as "SYSTem:REMote" or "*TST" however when I attempt to use a command that sends back information nothing is returned to the computer. If I use another return command directly after I get a -410 error signifying that the output buffer is full. Are there any commands I need to use to tell it to send the information to the computer? I have been looking through the manual for one but cannot find it.
Extra Info:
RS232 Connection Setup: Baudrate=9600, Data bits=8, Stop bits=2, Parity=None
Since the connection is DTE to DTE I am using a null modem cable
I am using Termite to send commands to the multimeter
Thank you for any help!

Programming a "t command" into a serial object

So I have a grouping of "t commands", which is a string. These commands are supposed to be sent to a serial port, which will then run the command. I am using something similar to this in order to change the string into byte form which can then be read by the serial port:
(I'm using pySerial)
import serial
s=serial.Serial('COM')
str=('t command')
str=bytearray(str,"utf-8")
s.write.(str)
there are no errors, but the command is not working. Anybody have any ideas?

How can I read commands coming over serial port with openHAB?

these are the write commands towards a serial port:
sendCommand(SendCOM3,"hallo\r\r") --- text format
sendCommand(SendCOM4,"\u0001\u0012\u0123\u000F\r\r") --- binary format
and works fine.
Now, who can tell me what I have to do to get the response message over the same serial port ?
Thanks
Ciao
marco
Since you're already writing to the port, I'm assuming you have the serial binding as an addon and the serial ports enabled.
Create a new item and bind it to the serial port to assign incoming data. For example,
String Hallo1 "Hallo [%s]" (hall0) {serial="SendCOM3"}
The data is a string that you'll need to parse. Restart OpenHAB, check the logs and you should see your item updated.
Add serial binding .jar file to your addons directory
org.openhab.binding.serial-1.7.1.jar
And add item to yourschema.items:
String MySerialDevice "MySerialDevice [%s]" { serial="/dev/ttyUSB0" }
change ttyUSB0 to your real tty serial device.

Virtual COM failing with pyserial/Linux, but working otherwise

I am using Virtual COM Port (VCP) example code from http://blog.memsme.com/stm32f4-virtual-com-port-2/ on STM32F4 Discovery Board to have USB VCP. This code is originally by ST and used by many other people in their projects
Communication with the STM32F4 over VCP works fine from Windows. In Linux (Ubuntu 12.04 x86), if I send data to the port with
echo "aasfg" > /dev/ttyACM0
then, the MCU gets the data and everything works fine. I can receive the continuous data stream with
cat /dev/ttyACM0
However, if I send data with the simple Python script that uses pySerial
import serial
sercom = serial.Serial('/dev/ttyACM0')
sercom.write('asdf')
then I stop receiving data with the cat command, and following cat commands also don't receive any data. The MCU is constantly executing some USB interrupt routines, never returning to execute actual application code. I can receive data from VCP again after re-plugging the device.
The STM32 USB VCP code is probably not perfect, but it is used by many other people in many projects so it should be good enough. I am not able to debug that code. I suspect that sending data with pySerial does something with the port that the VCP driver (either on STM32 or PC) does not like and I would like to track it down and hopefully still use pySerial.
I executed
stty --file=/dev/ttyACM0 -a
before and after pyserial broke the communication. After breaking the VCP with pyserial, setting -clocal became clocal and setting min = 1 became min = 0. Are these relevant in VCP communication and could they hint how to fix VCP with pySerial?
The serial port was actually fine. As I mentioned, the pySerial call changed the port parameters. The param min = 0 meant that cat /dev/ttyACM0 returned immediately, reconfiguring to min = 1 with stty made cat blocking and outputted the data as before.

Resources