r/stackoverflow • u/poxford3 • Jan 03 '20
Need help formatting my question
I am trying to make a post on Stack Overflow but I am not very good at formatting questions. I tried to explain it thoroughly but I think I didn't do it well enough. Hopefully someone here can better explain where I went wrong or can ask my questions here so that I don't get banned on there. I am not trying to waste people's time I just want to get help on a project. I don't need people to do it for me either I just need basic help with syntax. I put the post here:
"
I am working on a project using a Parallax Laser RangeFinder ([datasheet][1]) and I am using a Raspberry Pi 3 Model B+ to control it.
Because I am using a Raspberry Pi, I am having to use Python to control it, specifically, the serial communication on the Pi. I have tried to program this myself by researching serial communication on the Pi to no avail. I have found an example of this exact device being used but on an Arduino and subsequently, using C code. I was wondering if there was an easier way that I hadn't seen to convert from C++ to Python easily using the example [here][2].
My main objective is to find the syntax needed to recreate the sending/receiving of the data from the Pi to the device. The code provided isn't exactly what I need but it gives an understanding of what types of syntax the device uses. For example, it requires a user to send a "U" to check if the device is ready, I am primarily trying to find the syntax on how to do that in Python.
I have also included the exact code from the section I am trying to replicate below:
```
#include <SoftwareSerial.h>
#define rxPin 8 // Serial input (connects to the LRF's SOUT pin)
#define txPin 9 // Serial output (connects to the LRF's SIN pin)
#define ledPin 13 // Most Arduino boards have an on-board LED on this pin
#define BUFSIZE 16 // Size of buffer
int lrfDataInt;
SoftwareSerial lrfSerial = SoftwareSerial(rxPin, txPin);
void setup() // Set up code called once on start-up
{
// *************************************** setup for LRF ***********************************************
pinMode(ledPin, OUTPUT);
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
digitalWrite(ledPin, LOW); // turn LED off
Serial.begin(9600);
while (!Serial); // Wait until ready
Serial.println("\n\nParallax Laser Range Finder");
lrfSerial.begin(9600);
Serial.print("Waiting for the LRF...");
delay(2000); // Delay to let LRF module start up
lrfSerial.print('U'); // Send character
while (lrfSerial.read() != ':');
delay(10); // Short delay
lrfSerial.flush(); // Flush the receive buffer
Serial.println("Ready!");
Serial.flush(); // Wait for all bytes to be transmitted to the Serial Monitor
}
// ****************************************** main loop ************************************************
void loop() // Main code, to run repeatedly
{
lrf();
}
// ****************************************** end main loop *********************************************
void lrf()
{
lrfSerial.print('R'); // Send command
digitalWrite(ledPin, HIGH); // Turn LED on while LRF is taking a measurement
char lrfData[BUFSIZE]; // Buffer for incoming data
int lrfDataInt1;
int lrfDataInt2;
int lrfDataInt3;
int lrfDataInt4;
int offset = 0; // Offset into buffer
lrfData[0] = 0; // Clear the buffer
while(1)
{
if (lrfSerial.available() > 0) // If there are any bytes available to read, then the LRF must have responded
{
lrfData[offset] = lrfSerial.read(); // Get the byte and store it in our buffer
if (lrfData[offset] == ':') // If a ":" character is received, all data has been sent and the LRF is ready to accept the next command
{ lrfData[offset] = 0; // Null terminate the string of bytes we just received
break; } // Break out of the loop
offset++; // Increment offset into array
if (offset >= BUFSIZE) offset = 0; // If the incoming data string is longer than our buffer, wrap around to avoid going out-of-bounds
}
}
lrfDataInt1 = ( lrfData[5] -'0');
lrfDataInt2 = ( lrfData[6] -'0');
lrfDataInt3 = ( lrfData[7] -'0');
lrfDataInt4 = ( lrfData[8] -'0');
lrfDataInt = (1000*lrfDataInt1)+ (100*lrfDataInt2)+(10*lrfDataInt3) + lrfDataInt4;
Serial.print("Distance = "); // The lrfData string should now contain the data returned by the LRF, so display it on the Serial Monitor
Serial.println(lrfDataInt);
Serial.flush(); // Wait for all bytes to be transmitted to the Serial Monitor
digitalWrite(ledPin, LOW); // Turn LED off
delay(1000);
} ```
[1]: https://www.parallax.com/sites/default/files/downloads/28044-Laser-Range-Finder-Guide-v2.0.pdf
"
View all comments
Show parent comment
2
u/talex000 Jan 06 '20
So you problem you don't know how to send bytes to serial port of your Pi?
Just ask about that.
Or you can google it yourself. It is second link
If solution in this link doesn't work. You can start from that. Use sample in you question, Then explain what you expect to happen and what you expect.