otuga
Posts: 14
Joined: Mon Jul 12, 2021 7:41 pm

Where to start - programming in c

Mon Jul 12, 2021 7:50 pm

Hi all
I am lost after looking online for an answer. Too many different approaches and not sure which one to follow.
I will try to explain what I need to do and I would like you help to guide me, please.

I need to have a raspberry pi 4 to have
- serial Port (RS232 or RS485)
Listen port for a commands and reacted accordingly

- snmp server
Listen for commands and react accordingly

- read 3/4 inputs and react accordingly

- set on 3/4 outputs depending on commands received

- web pages to display info and receive commands

I was hoping doing in c language but I am a bit confused.
Can I use visual studio code? Visual studio 2019? Netbean? Which one is best and easy to use on a windows pc and debug remote via ssh on the raspberry?

Thank you for all your help

Heater
Posts: 18365
Joined: Tue Jul 17, 2012 3:02 pm

Re: Where to start - programming in c

Sat Jul 17, 2021 7:17 am

otuga wrote:
Mon Jul 12, 2021 7:50 pm
Hi all
I am lost after looking online for an answer. Too many different approaches and not sure which one to follow.
I will try to explain what I need to do and I would like you help to guide me, please.

I need to have a raspberry pi 4 to have
- serial Port (RS232 or RS485)
Listen port for a commands and reacted accordingly

- snmp server
Listen for commands and react accordingly

- read 3/4 inputs and react accordingly

- set on 3/4 outputs depending on commands received

- web pages to display info and receive commands

I was hoping doing in c language but I am a bit confused.
Can I use visual studio code? Visual studio 2019? Netbean? Which one is best and easy to use on a windows pc and debug remote via ssh on the raspberry?

Thank you for all your help
You have a lot there to be asking in a single forum post. But let's see:

Firstly is there any particular reason you want to do this in C? I love C but when it comes to creating a web server and/or web pages I don't think it is suitable language for your task. Firstly it is much easier to create web servers in many other languages. Secondly you are likely to want some interaction in your web pages, that interaction will be written in Javascript.

It's convenient to create the whole system in the same language. That saves having to juggle two or more languages in ones mind at the same time. Given that the web page stuff will be using Javascript in makes sense then to create your server in Javascript as well. This can be done easily with node.js: https://nodejs.org/en/ and it's "Express" web framework module: https://expressjs.com/.

Handling a serial port in node.js is also very easy: https://www.npmjs.com/package/serialport. As is creating a SNMP server with the net-snap module: https://www.npmjs.com/package/net-snmp. You will also find node.js modules for handling GPIO on the Pi.

Aside: I'm wondering why you want to use SNMP? It's not the nicest way to send commands/recieve data.

Personally I use Visual Studio code for all my development, except when I use Vim, on Windows, Mac and Linux. It has very good support for Javascript and many other languages. VS Code can be used to edit remotely but why not just run it on your Pi. Most of the time though I develop code on a PC. When it is in some kind of working condition I move the source to a Pi and test it there.
Memory in C++ is a leaky abstraction .

Heater
Posts: 18365
Joined: Tue Jul 17, 2012 3:02 pm

Re: Where to start - programming in c

Sun Jul 18, 2021 11:33 am

If your really must do this in C:

0) Learn C from the book: "C Programming Language", 2nd Edition 2nd Edition by Brian W. Kernighan and Dennis M. Ritchie. https://www.amazon.com/Programming-Lang ... 0131103628

1) For the serial ports there are many examples of how to use them from C on the net: https://www.cmrr.umn.edu/~strupp/serial.html

2) For the SNMP server there is net-snmp http://www.net-snmp.org/

3) For GPIO there pigpio https://github.com/joan2937/pigpio

4) For the HTTP server there is Libmicrohttpd https://www.gnu.org/software/libmicrohttpd/ or libhttp https://www.libhttp.org/ and no doubt others.

5) As you will want to be waiting to respond to all those events at the same time: Serial port commands, SNMP commands, HTTP commands, GPIO activity, etc, you will likely need to combine all the code handling those inputs using threads. Typically one uses pthreads in C to do this: https://www.cs.cmu.edu/afs/cs/academic/ ... reads.html

All in all though, unless you really need the performance I would advise not doing this is C. Too much of a fiddly pain. All theses things are much easier to do in other languages. One huge advantage of Javascript running under node.js I forgot to mention before was that it is very easy to combine all the activities you want, responding to GPIO, serial port, HTTP request, SNMP request without having to get into the complexity of using threads in your code. Instead of using threads Javascript has an event driven programming model which is ideal for juggling such things in one program.

If you do need the performance I would suggest you use the Rust language instead.

0) Learn Rust from "The Book": https://doc.rust-lang.org/book/

1) For the serial ports there is the serial port library ("crate" in Rust) https://docs.rs/serialport/4.0.1/serialport/

2) For the SNMP server there is https://crates.io/crates/snmp

3) For GPIO there is https://docs.rs/gpio/0.4.1/gpio/

4) For the HTTP server there is Rocket: https://rocket.rs/

5) No special library required for threading in Rust. Just use the built in language features. As described in The Book: https://doc.rust-lang.org/book/ch16-01-threads.html

When it comes to an IDE VS Code makes a great IDE for Rust with the "rust-analyser" plugin.
Memory in C++ is a leaky abstraction .

User avatar
jahboater
Posts: 7196
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Where to start - programming in c

Sun Jul 18, 2021 11:54 am

Heater wrote:
Sun Jul 18, 2021 11:33 am
5) As you will want to be waiting to respond to all those events at the same time: Serial port commands, SNMP commands, HTTP commands, GPIO activity, etc, you will likely need to combine all the code handling those inputs using threads. Typically one uses pthreads in C to do this: https://www.cs.cmu.edu/afs/cs/academic/ ... reads.html
Threads allow the use of all the four cores on some Pi models.
Otherwise (I personally) think select() is simpler, much safer, and possibly faster.

See "man select".

Heater
Posts: 18365
Joined: Tue Jul 17, 2012 3:02 pm

Re: Where to start - programming in c

Sun Jul 18, 2021 2:46 pm

jahboater wrote:
Sun Jul 18, 2021 11:54 am
Otherwise (I personally) think select() is simpler, much safer, and possibly faster.
"select" is a wonderful thing and all but a couple of issues:

1) The man page says "modern applications should instead use poll(2) or epoll(7)".

2) You need to have file descriptors to use select/poll.

So how does one make use of select/poll/epoll when using the API of a web server library or SNMP server library etc.

3) You are right, if ones application has tens of thousands of simultaneous connections to wait on and serving each of them is not a long winded compute task then the asynchronous nature of select/poll can be much more efficient than spinning up a thread for each connection.

I doubt our OP has that problem and threads would be just fine.

4) I also get the impression our OP does not experience enough in such things. It's a steep learning curve.

All in all, this kind of thing is much more easily done in Javascript and node.js or Rust.
Memory in C++ is a leaky abstraction .

ejolson
Posts: 7601
Joined: Tue Mar 18, 2014 11:47 am

Re: Where to start - programming in c

Sun Jul 18, 2021 4:37 pm

otuga wrote:
Mon Jul 12, 2021 7:50 pm
I was hoping doing in c language but I am a bit confused.
Can I use visual studio code? Visual studio 2019? Netbean? Which one is best and easy to use on a windows pc and debug remote via ssh on the raspberry?

Thank you for all your help
Unlike an Arduino or Pico, the Pi 4B and it's predecessors host a Linux development environment. As a result there is no official cross development environment to use a Windows computer for building code that runs on a Pi. Therefore, it will be easier to do the coding and debugging directly on the Pi, especially the GPIO part.

Both gcc and clang are available. I think most people use gcc as it's also the system compiler. The choice of editor is less standardized as everyone has their own preferences. If you are familiar with Visual Studio Code, then maybe use that.

To get started with C on a Pi, the book by Simon Long could be useful.

https://magpi.raspberrypi.org/articles/learn-code-c

Since the Pi has a Unix-like development environment, even the C Programming Language by Kernighan and Ritchey as already mentioned is relevant. That book is very well written but assumes a bit of prior programming knowledge.

After this one needs to read a tutorial on working with GPIO. Things have changed since I last did that, so I don't know what the good method is for GPIO. If you don't need any high speed toggling or reading of pins, it's possible to read and write the GPIO directly through the /dev interface.

Heater
Posts: 18365
Joined: Tue Jul 17, 2012 3:02 pm

Re: Where to start - programming in c

Mon Jul 19, 2021 5:54 pm

It's been a week since the first post with no feedback to our replies. I guess our OP is not much interested in our suggestions.
Memory in C++ is a leaky abstraction .

ejolson
Posts: 7601
Joined: Tue Mar 18, 2014 11:47 am

Re: Where to start - programming in c

Mon Jul 19, 2021 6:00 pm

Heater wrote:
Mon Jul 19, 2021 5:54 pm
It's been a week since the first post with no feedback to our replies. I guess our OP is not much interested in our suggestions.
That's possible. It's also possible they have lost track of the thread or expect an email notification when it's replied to.

Are there any languages other than C and Python that are supported well enough to be practical when programming the Pico?

Heater
Posts: 18365
Joined: Tue Jul 17, 2012 3:02 pm

Re: Where to start - programming in c

Mon Jul 19, 2021 7:38 pm

If C works I would expect C++ does as well.

Somebody is working on Rust for the Pico: https://github.com/rp-rs

Someone else is woking on Javascript for the Pico: https://blog.moddable.com/blog/pico/
Memory in C++ is a leaky abstraction .

User avatar
jahboater
Posts: 7196
Joined: Wed Feb 04, 2015 6:38 pm
Location: Wonderful West Dorset

Re: Where to start - programming in c

Mon Jul 19, 2021 8:46 pm

ejolson wrote:
Mon Jul 19, 2021 6:00 pm
Are there any languages other than C and Python that are supported well enough to be practical when programming the Pico?
Likely assembler should be possible.
The C compiler produces assembler, so a decent assembler is always available, usually called simply "as".

Your better off with C of course for most things.

otuga
Posts: 14
Joined: Mon Jul 12, 2021 7:41 pm

Re: Where to start - programming in c

Tue Jul 20, 2021 4:13 pm

Heater wrote:
Mon Jul 19, 2021 5:54 pm
It's been a week since the first post with no feedback to our replies. I guess our OP is not much interested in our suggestions.
Thank you!
Sorry for the late reply.
I was trying some of my tasks in c and just realized how difficult and complex it is, maybe I do not need to go into that level... and don't have the time and even knowledge.
I will follow your advice... nodejs
I am new to nodeJS, where should I start?

Heater
Posts: 18365
Joined: Tue Jul 17, 2012 3:02 pm

Re: Where to start - programming in c

Tue Jul 20, 2021 11:21 pm

Ah, hello again.

I love C. It is actually a pretty simple language. Compared to the likes of C++. But that simplicity is the problem when you want to do complicit things, like crate a web server, perform many tasks at the same time and so on. As you do. That simplicity means you have to do more and attend to a lot more details to get things done.

There are node.js tutorials all over the net:
https://nodejs.dev/learn
https://www.w3schools.com/nodejs/

And on YouTube, For example:
https://www.youtube.com/watch?v=TlB_eWDSMt4
https://www.youtube.com/watch?v=RLtyhwFtXQA

I'm sure google can find you many more.
Memory in C++ is a leaky abstraction .

otuga
Posts: 14
Joined: Mon Jul 12, 2021 7:41 pm

Re: Where to start - programming in c

Wed Jul 21, 2021 7:45 am

Assembler and later C were my languages for many and many years... I did a lot of embedded software and some windows applications but this was more than 20 years ago... the choices at the time were not many and web was starting.
My professional life moved on and I continue to be involved in some ways with software but not as a developer.
Things changed a lot and sometimes I just feel overwhelmed with info.

Thank you all for the support.

Heater
Posts: 18365
Joined: Tue Jul 17, 2012 3:02 pm

Re: Where to start - programming in c

Wed Jul 21, 2021 9:10 am

otuga wrote:
Wed Jul 21, 2021 7:45 am
Assembler and later C were my languages for many and many years... I did a lot of embedded software and some windows applications but this was more than 20 years ago... the choices at the time were not many and web was starting.
OK. That rather puts a different perspective on your original question.

I suspect that if you were into assembler and C back in the day you could get back into C today easily enough. Like riding a bike as they say.

It does not really change my response though. One you get into needing a web server, and SNMP server, GPIO etc you are going to want to be using libraries to provide that functionality. At that point it is far easier to use some other language than C to get the job done.

One aspect of my response that does change though is that given your past experience in C I would strongly suggest you take a look at the Rust language. Rust is like a very much more elegant and robust C. Whilst making programming a whole lot easier like Javascript or Python.
otuga wrote:
Wed Jul 21, 2021 7:45 am
Things changed a lot and sometimes I just feel overwhelmed with info.
I think that is normal. Even for those currently active in software. There is so much going on now a days.

Like you I started out with assembler and languages like C back in 1980. It's been a wild ride.
Memory in C++ is a leaky abstraction .

Return to “General programming discussion”