element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • Product Groups
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Raspberry Pi Projects
  • Products
  • Raspberry Pi
  • Raspberry Pi Projects
  • More
  • Cancel
Raspberry Pi Projects
Blog Raspberry Pi RS232 Marine compass protocol converter in Python
  • Blog
  • Documents
  • Events
  • Polls
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Raspberry Pi Projects requires membership for participation - click to join
Blog Post Actions
  • Subscribe by email
  • More
  • Cancel
  • Share
  • Subscribe by email
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: lawsonkeith
  • Date Created: 4 Nov 2021 5:57 PM Date Created
  • Views 4626 views
  • Likes 1 like
  • Comments 1 comment
  • wifi
  • industry
  • marine
  • protocol converter
  • pi projects
  • gyro
  • raspberry pi
  • raspberry pi projects
  • rs232
  • compass
  • udp
Related
Recommended

Raspberry Pi RS232 Marine compass protocol converter in Python

lawsonkeith
lawsonkeith
4 Nov 2021

This is a project to demonstrate how to use the raspberry pi to interface to an industrial RS232 device and send it's data elsewhere over an ethernet WiFi link.  This could be done over a hardwired ethernet link or as in this case over the WiFi to a computer several metres away.

 

Industrial protocol converters are quite expensive so it makes sense to do this with something as powerful as the raspberry Pi.

 

DevicePathDevicePathDevice
Gyro compassRS232 lead (0.5m)Raspberry PiWifi (10m)My laptop

 

Here we're using a marine compass 'CDL INSENSE' which communicates using a protocol called 'TOGS' over RS232 - this reports heading, pitch and roll of the device.

 

image

Firstly we need to connect out compass to our raspberry pi.  Here we've used a commercial board to do the interfacing since we need to mount it in panel and this unit has several mounting options as well as some 24V interfacing we may use to set the network address or just indicate status. (https://www.piio.co.uk) but you could just as easy use a USB device etc.  Interfacing to RS232 requires a minimum of 3 wires Tx,Rx and GND. 

 

 

CompassRaspberry Pi
TxRx
RxTx
GNDGND
24V-
0V-
-5V power

 

Next log onto your router and get your Pi's IP address, you can use this to remote log on vis SSH using a terminal program like putty.

image

Download putty or some other ssh terminal program if you wish to develop this in 'headless mode' - this allows us to develop on the Pi from another computer such as a laptop.

image

Next we can log on with ssh and use a program called minicom to talk to the compass locally from the raspberry pi.

[minicom -D /dev/ttyAMA0 -b19200) opens up a local terminal on the pi to communicate with the serial port and thus the compass.  This just lets us view data from the port and send characters to the connected device using the keyboard.

image

Here we see the data back from the device via the ssh session, we can use minicom to enter a setup mode to setup the device now if required (type menu).  As it is we're happy so we're going to try and write our python program now.

 

A good strategy here is to copy the string to the clipboard then develop the initial code using an online python compiler.  Here we're using https://www.programiz.com/python-programming/online-compiler/ .  We use this to quickly prototype some code to parse the incoming RS232 string we received earlier.  It's convenient here to just check this works and doesn't crash if the string changes (see github example for more robust implementation).

 

image

Once this is done we can paste this into our putty ssh session and test it with the real device with some code running locally.  Here we just run the python program on the local pi using the real 232 port again.  Now the code is parsing the incoming string on the Pi.

 

image

We can now write some code to send this data out over UDP using the network to another computer which may be located several metres away.  Here I'm using a program called 'pcomm terminal emulator' which can receive UDP messages on a windows PC.  We'll use this to temporarily test that we can indeed send the data.

image

The code to send out data over UDP is quite straightforward in python.  We just need the target IP and port, here we've added a header string s well so we can easily identify the string.

 

image

 

We can use this to send our parsed string back to another computer (which doesn't have an RS232 port).

 

image

Creating our output MESSAGE is very easy in python, we add our header as discussed earlier.

 

We can now use our UDP string monitoring program to receive the RS232 data from the Pi Remotely.  This is just 10m away here but could be read back anywhere on a much larger network, RS232 is generally limited to 10-20m, here se can send the data any distance we like.

image

So it really is that easy to make a protocol converter using the raspberry pi.

 

Complete code listing available at....

 

https://github.com/lawsonkeith/PiIO/tree/master/examples/PiIO_H_boards/232_H

 

Featured IO board

https://piio.co.uk/piio-232-h/

https://www.tindie.com/products/industry/raspberry-pi-r232-interface-board/

  • Sign in to reply
  • DAB
    DAB over 1 year ago

    Nice post.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube