I need a proper while condition for my loop - loops

Here i have a while loop which is ment to have a condition to be satisfied, when i receive the whole message from My Arduino. The problem is that: i dont know how i could change this condition
do
{
read_result = arduino.readSerialPort(incomingData, MAX_DATA_LENGTH);
//prints out data
if (read_result > 0)
cout << incomingData;
//puts(incomingData);
} while (true);
i have inspired all my project from Here
but i have changed the main function and my arduino file. here is my main function:
//This code snippet will help you to read and write data from arduino
//#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "SerialClass.h"
#include <fstream>
//using namespace std;
using std::cin;
using std::cout;
using std::endl;
//String for getting the Output from Arduino
char outputData[MAX_DATA_LENGTH];
//String for incoming data
char incomingData[MAX_DATA_LENGTH];
//Portname must contain these backslashes
char *port_name = "\\\\.\\COM5";
const unsigned int bufSize =255;
int main()
{
SerialPort arduino(port_name);
if (arduino.isConnected())
std::cout << "Connection Established! \n" << std::endl;
else
std::cout << "ERROR, check port!";
while (arduino.isConnected()) {
std::string input_string ;
//cout << "Write something: \n";
getline(cin, input_string);
//Creating a c string
char *c_string = new char[input_string.size() + 1];
//copying the std::string to c string
std::copy(input_string.begin(), input_string.end(), c_string);
//Adding the delimeter
c_string[input_string.size()] = '\n';
//Writing string to arduino
arduino.writeSerialPort(c_string, MAX_DATA_LENGTH);
//Getting reply from arduino
arduino.readSerialPort(outputData, MAX_DATA_LENGTH);
//printing the output
//puts(outputData);
//freeing c_string memory
delete[] c_string;
//Check if data has been read or not
int read_result;
do
{
read_result = arduino.readSerialPort(incomingData, MAX_DATA_LENGTH);
//prints out data
if (read_result > 0)
cout << incomingData;
//puts(incomingData);
} while (true);
//wait a bit
//Sleep(100000);
}
}
and here is my arduino file:
int PButton=2;
int Led=13;
unsigned long start,finished,elapsed;
void setup() {
Serial.begin(9600); //begins the Serial Connection.
pinMode(PButton, INPUT_PULLUP);
pinMode(Led,OUTPUT);
digitalWrite(Led,LOW);
Serial.println("Is the video already started? ""yes/no""...\n");
start=millis();
String ans;
while(Serial.available()<=0)
{
}
ans=Serial.readStringUntil('\n');
if(ans.equals("yes")){
digitalWrite(13,HIGH);
Serial.println("The video is started!\n\nplease Press the Pushbutton when the task is fullfilled!\n");
}
else if(ans.equals("no")){
digitalWrite(13,LOW);
Serial.println("The Video is still not started! \n");
}
}
void loop() {
int buttonState=digitalRead(PButton); //read the input pin
if(buttonState==LOW)
{
Serial.print("The pushbutton is pressed after ");
finished=millis();
elapsed=finished-start;
Serial.println(elapsed);
delay(3000);
}
}

Related

Array Data Reading Failed

I am reading the data from a "Torque Wrench" using "USB Host Shield2.0" and Arduino UNO. I am receiving correct data from my "Torque Wrench" Data is receiving in a array.
But when I started reading data after "for" loop inside Void loop() I am receiving incorrect data. I attached Both output pictures correct and incorrect data.
Basically I am read data from Torque Wrench and send to receiver using Nrf24l01. I am receiving incorrect data.
My question is :- Why I am reading Incorrect data outside "for" loop.
Correct Data inside "for" loop :- enter image description here
Incorrect Data outside "for" loop :- enter image description here
#include <SPI.h> // for SPI communication
#include <nRF24L01.h>
#include <RF24.h>
#include <cdcacm.h>
#include <usbhub.h>
//#include "pgmstrings.h"
// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>
RF24 radio(7, 8); // CE, CSN
const byte address[6] = {'R','x','A','A','A','B'}; // the address the the module
class ACMAsyncOper : public CDCAsyncOper
{
public:
uint8_t OnInit(ACM *pacm);
};
uint8_t ACMAsyncOper::OnInit(ACM *pacm)
{
uint8_t rcode;
// Set DTR = 1 RTS=1
rcode = pacm->SetControlLineState(3);
if (rcode)
{
ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode);
return rcode;
}
LINE_CODING lc;
lc.dwDTERate = 9600;
lc.bCharFormat = 0;
lc.bParityType = 0;
lc.bDataBits = 8;
rcode = pacm->SetLineCoding(&lc);
if (rcode)
ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode);
return rcode;
}
USB Usb;
//USBHub Hub(&Usb);
ACMAsyncOper AsyncOper;
ACM Acm(&Usb, &AsyncOper);
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MAX);
radio.stopListening();
#if !defined(__MIPSEL__)
while (!Serial);
#endif
Serial.println("Start");
if (Usb.Init() == -1)
Serial.println("USB Not Connected");
delay( 200 );
}
void loop() {
Usb.Task();
if( Acm.isReady()) {
uint8_t rcode;
/* reading the keyboard */
if(Serial.available()) {
uint8_t data= Serial.read();
/* sending to the phone */
rcode = Acm.SndData(1, &data);
if (rcode)
ErrorMessage<uint8_t>(PSTR("SndData"), rcode);
}
delay(10);
uint8_t buf[64];
uint16_t rcvd = 64;
char text[64];
rcode = Acm.RcvData(&rcvd, buf);
if (rcode && rcode != hrNAK)
ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
if ( rcvd ) {
for(uint16_t i=0; i < rcvd; i++ )
{
// Serial.print((char)buf[i]); // correct Data read from torque wrench
text[i] = (char)buf[i];
}
Serial.println(text); // reading wrong data here
//radio.write(&text, sizeof(text));
//Serial.println(text);
}
delay(10);
}
}
Character arrays must be null-terminated to count as C strings.
After the for loop, add text[rcvd] = '\0';
Also, your rcvd is fixed at 64. It needs to be one less than the array size for the null terminator to fit.

C: variables retain the value from previous operation instead of resetting

I am fairly new to C and have been trying my hand with some arduino projects on Proteus. I recently tried implementing a keypad and LCD interface with Peter Fleury's libraries, so far the characters I input are displayed fine, but I run into a problem when trying to print to the serial port. It's like the value of the keys keeps on being concatenated with every iteration so the ouput has extra characters like this:
The value before the comma is from the 'key' variable, the value after it the 'buf' variable:
151
(The 5 I input in the second iteration was added to the 1 from the first iteration and then put into the variable I print)
I figure it may be due to my lack/incorrect use of pointers, heres is my code:
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>
#include "lcd.h"
#include "mat_kbrd.h"
#include "funciones.h"
#include "menu.h"
char buf[256];
char* coma = ",";
int main(void)
{
pin_init();
serial_begin();
lcd_init(LCD_DISP_ON);
kbrd_init();
bienvenida();
while (1) {
int i = 0;
char key = 0;
//char *peso;
//int pesoSize = 1;
char peso[100];
//peso = calloc(pesoSize,sizeof(char));
int salida = 0;
lcd_clrscr();
desechos();
key = kbrd_read();
if (key != 0) {
lcd_gotoxy(0,3);
lcd_putc(key);
_delay_ms(2000);
lcd_clrscr();
cantidad();
while (salida != 1) {
char keypeso = 0;
keypeso = kbrd_read();
//pesoSize = i;
//peso = realloc(peso,pesoSize*sizeof(char));
if (keypeso != 0) {
if (keypeso == '+') {
salida = 1;
keypeso = *("");
lcd_clrscr();
calcularTotal(key,peso);
_delay_ms(2000);
} else {
lcd_gotoxy(i,1);
lcd_putc(keypeso);
snprintf(peso, sizeof peso, "%s%s",peso, &keypeso);
//strcat(peso,&keypeso);
i++;
_delay_ms(2000);
}
}
}
snprintf(buf, sizeof buf, "%s%s%s", &key,coma,peso);
serial_println_str(buf);
}
}
}
&key and &keypeso point to a single char, but you are using the %s format specifier, so trying to read a string into a single char. Use %c rather then %s for single characters, and pass the char not the address-of-char..

ESP8266- Buffer storage capacity from the UART

I have an issue while transferring the values of string from UART to buffer. I am using ESP8266 to receive strings on serial from STM32 device. I have total 600 string and i have CSV file that is being transmitted from the STM32 device. I have used proper filtering for every row on the NodeMCU side... I am having no clue why after 300 string the value transmitted to the buffer gets changed as well as the string read on UART also changes exactly after 300/305 string being transmitted. please let me know if there's any mistake in the code.
int i,j,k,l=0;
int httpCode=0;
String fields[24] = {"","month","year","hours","minutes","seconds","rimin","riavg","rimax","yimin","yiavg","yimax","bimin","biavg","bimax","nimin","niavg","nimax","eimin","eiavg","eimax","simin","siavg","simax"};
String element = "API_key=123&mac=0fa&day=";
String postApi1[300];
String postApi2[300];
String str= "";
String str_tx = "";
char char_array[128];
char* token;
char* rest = char_array;
WiFiClient client;
HTTPClient http;
void setup()
{
Serial.begin(230400);
s.begin(9600);
pinMode(13, INPUT);
pinMode(15, OUTPUT);
}
void loop()
{
if (s.available() >0)
{
for(j=0;j<300;j++)
{
i=0;
str = s.readStringUntil('\n');
Serial.println(str);
Serial.println("j");
postApi1[j]= str;
Serial.println(postApi1[j]);
str = "";
}
for(k=0;k<300;k++)
{
i=0;
str = s.readStringUntil('\n');
Serial.println(str);
Serial.println("k");
postApi2[k]= str;
Serial.println(postApi2[k]);
str = "";
}
}
}
Here is the code i am using and following is the Serial output of the code.
I think that the readStringUntil function waits for the terminator char. Is the '\n' sent also after 300th character? Also, try to avoid Serial.println in the read function, since the print function is quite slow, you might miss some characters. I would also avoid two for loops, instead, declare a variable, count number of received bytes and according to this save the "str" into "postApi1" or "postApi2".
Also, what is the purpose of "i" variable?
Hey #Andrej I tried your suggestion but it worked as follows
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <SoftwareSerial.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <stdio.h>
#include <string.h>
using namespace std;
ESP8266WiFiMulti WiFiMulti;
SoftwareSerial s(13,15); //RX TX
double buff[24]; //Initialized variable to store recieved data
double val;
char c;
int i,j,k,l=0;
int httpCode=0;
String fields[24] = {"","month","year","hours","minutes","seconds","rimin","riavg","rimax","yimin","yiavg","yimax","bimin","biavg","bimax","nimin","niavg","nimax","eimin","eiavg","eimax","simin","siavg","simax"};
String element = "API_key=123&mac=0fa&day=";
String postApi[616];
String str= "";
String str_tx = "";
char char_array[128];
char* token;
char* rest = char_array;
WiFiClient client;
HTTPClient http;
void setup()
{
Serial.begin(230400);
s.begin(9600);
pinMode(13, INPUT);
pinMode(15, OUTPUT);
//Serial.setDebugOutput(true);
//ESP.wdtDisable();
//Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("ECLP", "p##$w0rD");
if ((WiFiMulti.run() == WL_CONNECTED))
{
Serial.print("WIFI connected");
}
}
void loop()
{
if (s.available() >0)
{
for(j=0;j<616;j++)
{
i++;
str = s.readStringUntil('\n');
//Serial.println(str);
//Serial.println(j);
postApi[j]= str;
//Serial.println(postApi[j]);
str = "";
}
}
if(i == 616)
{
i=0;
for(k=0;k<616;k++)
{
Serial.println(k);
Serial.print(" : ");
Serial.print(postApi[k]);
}
}
}
And receive on the output the following thing
Just for the ref i printed "k" value with output to know at which string the data loss occurs in buffer

How to create a txt file from FOR loop?

I wrote a C++ code using iterative methods. For this, I used a FOR loop. However, I need to save every result by iteration in same text file (or DATA file) as a columns. How can I do it? Thanks for your advices.
This a simple version of my code:
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;
int i;
main()
{
cout<<"Value 1"<<right<<setw(20)<<"Value 2"<<endl;
for(i=0;i<5;i++)
{
cout<< left << setw(20) << i+10
<< setw(20) << i<<endl;
}
getch();
}
For most purposes using a CSV file would be better. Here is a code that does what you need.
#include <stdio.h>
int main() {
FILE * fpw; // A file pointer/handler that can refer to the file via a cpp variable
fpw = fopen("data.txt", "w"); // Open the file in write("w" mode
if (fpw == NULL) {
printf("Error"); // Detect if there were any errors
return 0;
}
fprintf(fpw, "Value 1,Value 2\n"); // Write the headers
int i = 0;
for (i = 0; i < 5; i++) {
fprintf(fpw, "%d,%d\n", i + 10, i); // Write the values
}
fclose(fpw); //Don't forget to close the handler/pointer
return 0;
}
Output:
A file data.txt will be created with following contents:
Value 1,Value 2
10,0
11,1
12,2
13,3
14,4

How create a simple program using threads in C?

I'm new in C development, I know just the basics and I need to create a program that discover a simple hash password like this one:
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <crypt.h>
#include <stdlib.h>
#define SIZE_HASH 256
#define SIZE_PASSWORD 4
/* Get the hash from a passwod and put the result in hash. The array hash shoud have at lest 14 elements. */
void calculate_hash_password(const char *password, char *hash);
void increment_password(char *password);
void test_password(const char *p_hash, const char *password);
int main(int argc, char *argv[]) {
int i;
char password[SIZE_PASSWORD + 1];
if (argc != 2) {
printf("Use: %s <hash>", argv[0]);
return 1;
}
for (i = 0; i < SIZE_PASSWORD; i++) {
password[i] = 'a';
}
password[SIZE_PASSWORD] = '\0';
while (1) {
test_password(argv[1], password);
increment_password(password);
}
return 0;
}
void test_password(const char *p_hash, const char *password) {
char hash_calculado[SIZE_HASH + 1];
calculate_hash_password(password, hash_calculado);
if (!strcmp(p_hash, hash_calculado)) {
printf("Achou! %s\n", password);
exit(0);
}
}
void increment_password(char *password) {
int i;
i = SIZE_PASSWORD - 1;
while (i >= 0) {
if (password[i] != 'z') {
password[i]++;
i = -2;
} else {
password[i] = 'a';
i--;
}
}
if (i == -1) {
printf("Não achou!\n");
exit(1);
}
}
void calculate_hash_password(const char *password, char *hash) {
struct crypt_data data;
data.initialized = 0;
strcpy(hash, crypt_r(password, "aa", &data));
}
I must do the same thing as this one but using threads in C.
How can I do that ?
EDIT
Using threads to hash passwords is not a particularly intuitive or obviously useful approach, so it is not clear why anyone would want to do that.
Presumably the calculation for hashing is split up in some way: perhaps one thread processes passwords beginning with A through M and another does N through Z, or some such partitioning. One idea would be to run the same function multiple times with a parameter which determines which partition to execute. Here is a simple, functioning program which demonstrates the framework.
#include <iostream>
#include <pthread.h>
static void *calc_func (void *arg)
{
int param = (int) arg;
if (param == 1)
{
// do first partition of calculation
// ...
std::cout << "partition 1" << std::endl;
}
else
{
// do second partition of calculation
// ...
std::cout << "partition 2" << std::endl;
}
}
int main (...)
{
// ...
pthread_t threadh[2];
if (pthread_create (&threadh[0], NULL, calc_func, (void *)1) != 0)
{
std::cerr << "error creating thread 1" << std::endl;
}
if (pthread_create (&threadh[1], NULL, calc_func, (void *)2) != 0)
{
std::cerr << "error creating thread 2" << std::endl;
}
// wait for threads to exit
pthread_join (threadh[0], NULL);
pthread_join (threadh[1], NULL);
return 0;
}
To build it on Linux using gcc, use the command g++ -pthread filename.c++ -o filename
On a Linux shell execute:
man pthread_create
Read it carefully, and notice that provides a very descriptive example, on how to use threads. See also the man pages of the functions in the SEE ALSO section.
If you are on windows you can see the decomentation of pthreads-win32 here
After that you have to decide which part(s) of your code can be parallelized and assign that code to different threads.

Resources