Greetings all, I am attempting to write a script to communicate with a photovoltaic inverter via a serial port on a Damn Small Linux PC. The inverter reads/writes binary data, and I've figured out how to handle that via od and awk, but I can't seem to actually communicate with the inverter. I need to communicate with a serial port inside a Perl script. I need to send characters and read the input and search for strings. What is the simplest way to achieve this? By using 'expect' or by opening the /dev/ttys0 device in Perl itself? Or some other method? I prefer to use perl but I don't know if it is simple and featured as expect.
Active6 years ago
Sharing a serial port between two processes. Ask Question 7. Browse other questions tagged linux serial or ask your own question. 6 years, 3 months ago. 14,674 times. 6 years, 3 months ago. Differentiate between identical USB devices on Linux. Traditionally in Linux, the first serial port (COM1) is assigned a name /dev/ttyS0, the second serial port (COM2) assigned /dev/ttyS1, etc. If you specify a serial port name as the first argument of the screen command, the current terminal window where you run screen will be directly connected to the serial port. The Serial Programming Guide for POSIX Operating Systems will teach you how to successfully, efficiently, and portably program the serial ports on your UNIX® workstation or PC. Solaris®, Digital UNIX®, Linux®, and most other UNIX operating systems. The biggest difference between operating systems that you will find is the filenames used.
I'm using a serial device for a project, and what I'm trying to accomplish PC side, is listening for a command sent by the serial device, interpreting the query, running some code depending on the query, and transmitting back the result.
To be honest I tried out using PHP as the listener, and it works, unfortunately the infinite loop required to make the script act as a receiver, loads the CPU to 25%. So it's not really the best option.
I'm using cygwin right now, I'd like to create a bash script using linux native commands.
I can receive data by using:
And send a response with:
My question is, how do I make an automated listener to be able to receive and send data? The main issue I have, is actually how do I stop the cat /dev/ttyS2 command once information was received, put it into a variable which then I could compare with a switch, or a series of if else then blocks. Afterwards send back a response and start the cycle all over again?
Thanks
dsolimano
7,61733 gold badges4141 silver badges5757 bronze badges
MattMatt
61311 gold badge77 silver badges2020 bronze badges
4 Answersricirici
165k2222 gold badges149149 silver badges215215 bronze badges
To remain fairly system independent, use a cross platform programming language: like Python, use a cross platform serial library like : pySerial and do the processing inside a script. I have used pySerial and I could run the script cross platform with almost no changes in source code. By using BASH you're limiting yourself a fair little.
Aniket IngeAniket Inge
21.3k22 gold badges3737 silver badges7272 bronze badges
If you use right tools, it is possible to actually have your CPU usage to be exactly 0 when your device does not have any output.
To accomplish this, you should use some higher level language (Perl, Python, C/C++ would do, but not bash) and use select loop on top of file handle of your serial device. This is an example for Perl http://perldoc.perl.org/IO/Select.html, but you can use any other language as long as it has support for select() syscall.
mvpmvp
77.7k99 gold badges9494 silver badges127127 bronze badges
I would recommend to use C/C++ with Qt 5.1.1, it's really easy and if you are familiar with the framework it'll be a piece of cake!!!Here you can find more information and here more helpful examples, give it a try,it's really pain free!! Also you can develop on win and then port your code to linux...straight forward.
Declare an object like this:
Then add this code:
ulitosCoderulitosCoder
Update Perl LinuxNot the answer you're looking for? Browse other questions tagged linuxbashserial-portcygwin or ask your own question.
Active3 years, 3 months ago
I'm bit tired of reading serial port, using
Perl first of all Perl is not my primary development tool, I'm from PHP background.So please guide me in a right way. my intention is to read the data from a weight machine to a web page, I already did a lot of research on this topic.
![]()
The browser app cannot read serial port data directly so I choose the user end machine can be installed with a server (Apache) and do the task with PHP and web application can read the result using an Ajax call to
localhost url the idea is working great on Linux machines but on windows PC it stuck.
I understand PHP can't read data from serial port in windows like Linux so I choose Perl to do the stuff. I did some search an got few codes and last 8 hours I'm fighting with that :(
The code is not working but on forum and many other places it says working like here , here, this and this.
Im unable to find what is this means 'use Win32::SerialPort qw( :STAT 0.19 );'is that something like importing that library with version number. ?
I'm already installed Xampp with Perl and
activePerl Win32::serialport 0.22 and its API but when I run the code it return an error like
500 Error
Any idea ??
Solved
For reading section I use the code below.
The code works fine from CLI so I just put a PHP file to read the perl script and return the data.
Jobin Jose
Jobin JoseJobin Jose
![]()
7,13311 gold badge2727 silver badges4545 bronze badges
Perl Serial Port Linux1 Answer
This is the same as
The
:STAT imports a number of useful status constants, and is explained under Exports in the documentation for the module
The BorodinBorodin
0.19 is part of Perl, and is insisting that the version of Win32::SerialPort must be 0.19 or later. The current version is 0.22. If you change 0.19 to 0.23 then you will see the effect it has
Serial Port Open Hangs Perl Linux
121k99 gold badges5858 silver badges128128 bronze badges
Serial Port Perl Linux List
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Perl Linux Distros WatchNot the answer you're looking for? Browse other questions tagged phplinuxperlserial-port or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |