How to structure radar commands?

Hi all,

I'm trying to communicate with a sensor with a STM32 Blue pill through the Arduino IDE.
Below is a Python snippet that initializes the serial connection and the structure of the commands that is needed.

I can make the correct serial connection and I understand the how and why around the structure of the command but I can't translate it to anything working i C.

What would be an efficient way to write the command in C?

# create serial object with corresponding COM Port and open it 
com_obj=serial.Serial(COM_Port)
com_obj.baudrate=115200
com_obj.parity=serial.PARITY_EVEN
com_obj.stopbits=serial.STOPBITS_ONE
com_obj.bytesize=serial.EIGHTBITS

# connect to sensor and set baudrate 
payloadlength = (4).to_bytes(4, byteorder='little')
value = (3).to_bytes(4, byteorder='little')
header = bytes("INIT", 'utf-8')
cmd_init = header+payloadlength+value
com_obj.write(cmd_init)

Thank you!

Put your snippet into the google translator and select Python on the left and C++ on the right, press translate.

The result will be pretty close but should be checked carefully.

HTH

a7

alto777:
Put your snippet into the google translator and select Python on the left and C++ on the right, press translate.

The result will be pretty close but should be checked carefully.

HTH

a7

a7,
Is this a serious comment? I had a look in Google translate because it seemed unlikely but just about possible that they had implemented some thing to convert from one programming language to another. As I suspected they have not, at least not as I can see. If Google translate can do this please explain how.

Thanks,

The reply was obviously sarcastic and it got me to think about "Comic Book Guy" and that it has been a while since I watched The Simpsons. :slight_smile: However, it doesn't get me closer to solving my problem.

I don't find the question unnecessarily trivial as if it was something I could Google in 5 minutes because then I would be finished a long time ago.

"Arduinos" are programmed in C++. You'll have to write the code in that language. Post what you have so far.

jatox:
The reply was obviously sarcastic and it got me to think about "Comic Book Guy" and that it has been a while since I watched The Simpsons. :slight_smile: However, it doesn't get me closer to solving my problem.

I don't find the question unnecessarily trivial as if it was something I could Google in 5 minutes because then I would be finished a long time ago.

That's what I suspected, but give what AI can do these days I would not be surprised if someone was at least working on a programming language converter...

Jatox.

Can you be clear about what you are asking for? Are you:

Asking for someone to write the code for you? If so please click on report to moderator and ask for this to be moved to gigs and collaborations. Expect to pay.

Asking for help with code. You are obviously experienced to some degree but not in C/C++. If you are not expecting someone to do this for you then the folk that help generally want to see your best effort, they don't want what you have asked, which reads to me as 'write me some code to do this'. If you just want help then be specific about what the problem is you want help with. There are plenty of tutorials on this site.

Sry @PerryBebbington, just a bit of humor and/or sarcasm, and a gentle suggestion to the OP.

Who has been advised to share what he has so far in terms of a sketch for his target that shows his claim to be able "make the correct serial connection".

I don't know Python, but it looks like from there (having established a serial connection with the parameters that seem obvious in the Python) the rest of the Python just builds up a sequence of bytes and ships it out.

This in C would just (just!) be a character array. OP, if you don't know C, that is where to start learning more. If you know Python, C is easily understood from like the first chapter or two of whatever book you learnt Python from. It is a tiny language, and you should probably know it as well as it can be known anyway.

Draw out what the Python lands up with in its byte or text or character object. It should be downhill from there.

Now.

Thinking more seriously about google translate for computer languages I ask why not? And when?

Since mostly we are talking about regular computers all of which can only do certain things, it seems that any algorithm can be translated into any language. I am leaving off quantum architectures and neural nets and all that kinda stuff, just your old Turing complete machinery doing the kind of thing that, after all, most computing machines do all the time every day all over the place.

But that leaves out the hardware. I think of and use C as a low level language, the first step up from assembly or machine code. All just syntactic sugar that allows rapid readable expressive access to the metal. Many C programs are quite opaque if one is not at least somewhat dimly aware of how hardware world works.

Many times I see things expressed in Python, but they use all kinds of whizzy built-in stuff that makes direct understanding (or translation/exploitation) of the underlying algorithm(s) nearly impossible. That does not appear to be the case with the OP's snippet.

There is a reason that algorithm books use regular old languages or so-called pseudo-code, the kind of power that these days is briskly covered in two chapters of a book about a modern high-level language. So we can see and understand the step-by-step operation.

Haven't looked lately, for all I know a modern algorithms book would be full of magical features of the language used, and go far deeper into territory I would argue is not really algorithms as such.

I suspect many never fully grasp all and what can be done with just old C and a microprocessor. Or are srsly in trouble once they realize that not everything is going to pop out of some library perfect ready to go for every circumstance.

Some languages are not that modern but would present a translation challenge, like APL or LISP. Nevertheless, since they ultimately run on the same hardware, it seems within grasp.

Modern languages and fashions would be harder, but equally not impossible. The real trick would be ending up with idoimatic expressions in the target language - if you've ever de-compiled object code you know what I mean. Google translate is very good, even so there are some amusing results. But usually better than translations of foreign languages found in literature accompanying cheap foreign products.

I think google translate uses an in-between language so that translation is a two step dance. It seems a similar approach would be possible with computer languages. Again, the hardware would be the stumbling block.

In the meantime (I'll probably not live to see this glorious future) there will be ways for people who get it at the point of intersection between software and hardware to be heroes and haul down the big bucks.

a7

It appears to me like the message is four characters ("INIT") followed by two little-endian 4-byte integers. "Little-endian" means the least significant byte comes first. The length field contains 4 and the data field contains 3.

const byte cmd_init[12] = {'I', 'N', 'I', 'T', 4, 0, 0, 0, 3, 0, 0, 0};

void init()
{
   Serial.write(cmd_init, 12);
}

This could also be written as a string literal:

Serial.write("INIT\4\0\0\0\3\0\0\0", 12);

WARNING: The one to three digits after the backslash are in OCTAL. The digits 8 and 9 are not legal. The value 8 would be represented as "\10" or "\010" and the value 9 would be "\11" or "\011". You can't use decimal values but you can use hex: "\x4".

Note: If you store the string literal in an array, it will have a null added at the end. If you use 'sizeof' you will be sending 13 bytes instead of 12:

const byte cmd_init[] = "INIT\4\0\0\0\3\0\0\0";
Serial.write(cmd_init, sizeof cmd_init); // Sends 13 bytes

jatox:
The reply was obviously sarcastic and it got me to think about "Comic Book Guy" and that it has been a while since I watched The Simpsons. :slight_smile: However, it doesn't get me closer to solving my problem.

Wikipedia / Comic Book Guy "He is well known for his distinctive accent, disagreeable personality and his catchphrase, "Worst [blank] ever!"

Worst burn ever, dude. :wink:

a7

Thank you all! I have no idea why I wrote 'C'....sorry for the confusion I meant C++.... And alto777 I did laugh at your remark .:smiley:
I'm obviously not a programmer although I'm trying to learn as much as possible. I do however work with data and with a lot of code-less ML APIs so I can also appreciate the thought of having a code translator. And yes, it is incredibly how much of an improvement that happened to Google Translate with the implementation of GNMT.

I 'think' I can set up the serial connection to match the parameters in the python snippet using the 'SERIAL_8E2' parameter to match the parity like this?

 Serial1.begin(112500, SERIAL_8E2);

Being a non-programmer that understood the Python snippet I assumed it was more than clear for everyone else.

But basically what the snippet does is setting up the Serial object and necessary parameters and then it sends the first command.
The command structure is always these 3 variables, "header", "payloadlength" and "value".

header is always 4 letters eg. INIT
payloadlength is the bytesize of the payload/value in this case 4
value in this case is 3 and specifies a new baud rate

The snippet converts the variables to bytes and concatenates them into a new variable 'cmd_init' which is then sent to the serial port.

**The core of my question: **Would the equivalent in C++ for Arduino, be to convert the command variables to one big byte array and write to the serial port of my sensor? As it(for me) looks like that's what's going on when I compile the Python code.

johnwasser, thank you it worked like a charm! :slight_smile: I had already tried writing it as a byte array but obviously didn't do it properly.
Now I have something to study on!

Thank you all again!

jatox:
Would the equivalent in C++ for Arduino, be to convert the command variables to one big byte array and write to the serial port of my sensor?

You could. No need though, just write each component in turn to the serial port.

jatox:
Being a non-programmer that understood the Python snippet I assumed it was more than clear for everyone else.

There's all levels of expertise here from beginners who fancy their hand at helping others to professional programmers who appear, to me at least, to be seriously clever.

For myself I consider myself to be a reasonably competent hobby programmer in C, with some insight but little expertise in C++. I do not know Python at all.

alto777:
Put your snippet into the google translator and select Python on the left and C++ on the right, press translate.

The result will be pretty close but should be checked carefully.

HTH

a7

I read this and almost laughed out loud, then thought.. "Wait.. Have they done this?"

pseudo-code

One of the BEST computer courses I ever had, back in the early 80s, and as far as I can figure out, it was only offered once. It was a computer algorithm class taught complely in pseudo-code. All the class work, text etc. was in pseudo-code. The homework was computer listings of our working code in whatever language we wanted to use. Not a lot of choices back them. I was writing in HP BASIC at the time. Probably 2..3 years before Turbo Pascal came out.

It was a great class because, using pseudo-code instead of programming, we were able to train our minds on how to think through and lay out solutions to problems in "code", without worrying about syntax. Big eye opener for me! From what I've see here, not having that background is a huge handicap for all these new users.

It was the "wax on wax off" foundation that turned out to be the most helpful.

-jim lee

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.