Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Cannot write to, or read from, the SerialPort

by Marshall (Canon)
on Jun 15, 2022 at 19:00 UTC ( [id://11144776]=note: print w/replies, xml ) Need Help??


in reply to Cannot write to, or read from, the SerialPort

I read your post with some interest because a friend of mine has requested a related project which I think could be done in Perl but I'm a bit fuzzy on the details.

From reading the spec for Device::SerialPort and looking at the CAT manual for the FTDX-1200, I figure that haukex has it right -> all you need to do is write_settings to send all of the device settings to the device before starting to use the serial port. Evidently statements like $port->parity("none"); set some parameter in a data structure, but the method does not actually send that info onto the actual device.

For testing, I would put a sleep(x seconds) after: $port->write("FA21050000;");. If the VFO-A command doesn't work, the fancier command for VFO-B probably won't work either! I didn't see any timing restrictions in the CAT manual, but for testing, I would pause and just look at the radio display to see what, if anything, happens on the display before immediately sending (or trying to send) another command to the radio.

I don't know the reliability that you expect from your application. You are controlling a piece of hardware. Hardware makes mistakes for a variety of reasons. The idea of send command, e.g. "FB;" and then wait with a read() until exactly 11 bytes have been returned, will work virtually all of the time, but not all of the time. This detail may not matter for your application. But best practice would be to "timeout" a blocking input request like that so that your program does not "hang". A perhaps contrived example: radio is powered off after 5 of 11 bytes have been sent to you. Radio is powered back on. It will not send anymore bytes, but you will still be waiting to read some bytes which will never come. In that example, what is the probability of this happening? Very, very low. But, there are other reasons why this same sort of thing could happen. In general, if it can happen, it will happen. It is just a matter of "how often".

I am curious as to your actual application and whether or not you have to co-exist with some other application?

My friend has two apps which want to talk to the same radio serial port. You can't just willy-nilly "y" the cables from 2 different serial ports together. That actually will work a lot of the time, but not all of time! What is desired is a "man in the middle" box. This box talks to the single "real serial port", but looks like two separate virtual ports to 2 different applications. If App A sends "FB;", it gets the response to that command before sequencing in perhaps an "FA;" command from App B. This man-in-the-middle is the "traffic cop" so that the radio only "hears one single voice" and is completely unaware that there are two end user apps out there.

I see how to to talk to the USB->Serial Port. But I am unsure how to make my software look like a serial port (COM port) to other applications?
My traffic cop app will be Windows based.

Replies are listed 'Best First'.
Re^2: Cannot write to, or read from, the SerialPort
by Anonymous Monk on Jun 16, 2022 at 17:30 UTC
    01. Thank you for viewing the Code and replying.

    02. I will be looking into the 'IO::Termios' Module as suggested by 'haukex'.

    03. Viewing various Perl Code Examples ...

    $port->write_settings || undef $port;

    ... was not in some of the Example Code. Thus, I tried my Perl Code with / without 'port->write_settings' used.
    I just happened to comment out that Line of Code, on my last Test, before posting my Request.

    I will make sure in any new Code, to use: $port->write_settings || undef $port;

    04. I beleived I did use 'sleep()' in previous Versions of the Code - before the posted Version - but I will again try using 'sleep()' after each '$port->write("...")'. Thank you for the Suggestion.

    05. "I am curious as to your actual application and whether or not you have to co-exist with some other application?" With repect to Python - No. I have wriiten well over 100 '.py' Script Files for the many Amateur Radio Contests which happen over the Year. My Scripts may call some personally written Modules for commonly used Routines - such as the Sample Python below; but the Scripts are not reliant on any external Application(s).

    Please see '06.' about 'co-exist with some other application(s)'.

    06. "My friend has two apps which want to talk to the same radio serial port ..." - correct, with respect to possible Conflicts.
    My Python Code closes the Serial Port once an Action is performed. However, once I had 'FLDIGI' running, using 'FLRIG' as its Frequency Controller. I then executed an AppleScript Applet to change the Power Output of my Transceiver - and it complained. Once I quitted 'FLDIGI' and 'FLRIG', the AppleScript worked just find. Either one or both - 'FLDIGI' and / or 'FLRIG' keep sampling the Serial Port; thus, it was never closed.
    The same would happen if I tried to run a Python Script, requiring access to the Serial Port - if 'FLDIGI' and "FLRIG' were running.

    07. I do thank you for the Suggestions.


    Below is only one (1) of many Python Functions / Methods / Procedures I use for accessing Yaesu Transceivers - this may assist you in your Perl Version for your Friend.

    ------------------------------------------------------------ Python Function / Method / Procedure for obtaining the VFO-A Frequency, in Hz; the Mode - 'LSB', 'USB', 'CW', 'FM', 'AM', 'RTTY-LSB', 'CW-R', 'DATA-LSB', 'RTTY-USB', '----', 'FM-N', or 'DATA-USB'; and Output Power, in Watts, of the Transceiver

    ----

    def Handle_FTdx1200(): lSer=serial.Serial('/dev/cu.usbserial', 4800) # Open a Serial Port, and configure its Baud Rate. lSer.write('FA;'.encode()) # Tell FTdx-1200 to obtain 'FA' VFO-A Frequency. lFreq=lSer.read(11) # Retrieve the 'FA' Data lSer.flush() # Purge the Input Buffer of any Characters. lSer.write('MD0;'.encode()) # Tell FTdx-1200 to obtain 'MD' Module. lMode=lSer.read(5) # Retrieve the 'MD' Data lSer.flush() # Purge the Input Buffer of any Characters. lSer.write('PC;'.encode()) # Tell FTdx-1200 to obtain 'PC' lPower Output Value. lPower=lSer.read(6) # Retrieve the 'PC' Data lSer.close() # Close the Connection of the USB Serial Adapter. lFreq=lFreq[2:10] # Remove 'FA' and ending ';' lFreq=lFreq.decode("utf-8") # Without '.decode("utf-8")' 'lFreq' would be printed, #in the print Line below # "b'XXX', where XXX is the Value of lFreq. lPower=lPower[2:5] # Remove 'PC' and ending ';' #lPower=str(int(lPower)) # If 'lPower' is less than 100 a preceding '0' will appear. #This Code removes the preceding '0'. lPower=lPower.decode("utf-8") lMode=lMode[3:4] lMode=lMode.decode("utf-8") # Without '.decode("utf-8")' 'lMode' would be printed, #in the print Line below as "b'XXX', where XXX is the Value of lMode. if((lMode=='3') or (lMode=='7')): lMode='CW' elif((lMode=='1') or (lMode=='2')): lMode='PH' elif(lMode=='5'): lMode='AM' elif((lMode=='4') or (lMode=='B')): lMode='FM' elif((lMode=='8') or (lMode=='C')): lMode='DG' elif((lMode=='6') or (lMode=='9')): lMode='RY' else: lMode='---' return(lFreq, lMode, lPower)
    ----

    The above Code works flawlessly at the FTdx-1200 default Baud Rate of 4800; as well at - 9600, 19200, and 38400 bps.

    ------------------------------------------------------------

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11144776]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-19 14:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found