Hello,
I have written a simple C script with which I want to send messages via UART.
I use a 485Bus with one transmitter and 8 listeners.
Each listener has an ID from 0-7.
In the first loop a message is sent to each listener.
The response is used to distinguish whether the listener is present or has returned a correct or incorrect value.
In the second loop, which again runs from 0-7, each listener is asked to take a measurement.
Unfortunately, this loop is not executed.
Translated with DeepL.com (free version)
The code works up to this point
but the printf below is not executed when the research loop has run through
This is what the output of the code looks like (for the first test I don't have listeners connected.
Does anyone know where the fault could be?
I have written a simple C script with which I want to send messages via UART.
I use a 485Bus with one transmitter and 8 listeners.
Each listener has an ID from 0-7.
In the first loop a message is sent to each listener.
The response is used to distinguish whether the listener is present or has returned a correct or incorrect value.
In the second loop, which again runs from 0-7, each listener is asked to take a measurement.
Unfortunately, this loop is not executed.
Translated with DeepL.com (free version)
Code:
#include<wiringPi.h>#include<wiringSerial.h>#include<stdio.h>#include<string.h>#include<errno.h>#include<stdint.h>#include<stdbool.h>#define SSC 1#define LEDR 4#define LEDG5bool newData=FALSE;uint8_t numChars=43;char recevedChars[43];int64_t previousMillis=0;int32_t wait=0;uint8_t value=0;int returnID =-1;int returnValue=-1;float FlowRates[8];int fd=0;int recvWithEndMarker(){recevedChars[0]='\0';digitalWrite(SSC,LOW);while(digitalRead(SSC)){}static uint8_t ndx=0;char endMarker='\n';char rc;while(serialDataAvail(fd)>0 &&newData==FALSE){rc=serialGetchar(fd); printf("Read\n"); if(rc!=endMarker){ printf("RC: ");printf("%c",rc);printf("\n"); recevedChars[ndx]=rc; ndx++; if(ndx>=numChars){ndx=numChars-1;}}//if RC!=EndMarker else{ recevedChars[ndx]='\0'; printf("RC: ");printf("%c",rc);printf("\n"); ndx=0; newData=TRUE;}}//while SerialData Availablereturn 0;}//void recvWithEndMarkerint Act(){if (newData==TRUE){switch(recevedChars[1]){ case'R':returnID=((int)recevedChars[0]-48);returnValue=(((int)recevedChars[2]-48)*100)+(((int)recevedChars[3]-48)*10)+((int)recevedChars[4]-48);printf("I get a Valve Setup from ID: ");printf("%c",returnID);printf(" and ValveSetup: ");printf("%c",returnValue);printf("\n");//WRITE VLAVESTATES AND ID TO SQL break; case'F':returnID=(int)recevedChars[0]-48;printf("I get Flowrates from ID ");printf("%c",returnID);for(int i=0;i<=7;i++){ int InterimFlow=( (((int)recevedChars[i*5+2]-48)*10000)+ (((int)recevedChars[i*5+3]-48)*1000)+ (((int)recevedChars[i+5+4]-48)*100)+ (((int)recevedChars[i*5+5]-48)*10)+ ((int)recevedChars[i*5+6]) ); FlowRates[i]=InterimFlow/100; printf("%c",FlowRates[i]); printf("\n"); }//for i>=7break;}//switch on recevedCharsnewData=FALSE;}//if newData true return 0;}//voidActint standby(int a){ uint64_t currentMillis=millis(); if(currentMillis-previousMillis>=(a*1000)){ previousMillis=currentMillis; wait++; printf("TIMEOUT\n"); return 1;}else{return 0;}}//void standbyint main(){wiringPiSetup();pinMode(SSC,OUTPUT);fd=serialOpen("/dev/ttyS0",9600);printf("Raspi Start RS485 Test\n");digitalWrite(SSC,HIGH);serialFlush(fd);printf("Verion Nr.2 ");while(1){for (uint8_t IDB=0;IDB<=7;IDB++){value=0b00000000;for(uint8_t IDC=0;IDC<=7;IDC++){//get data From SQL Server and set valve Statevalue=(3*IDB);}digitalWrite(SSC,HIGH);while(!digitalRead(SSC)){};serialPrintf(fd,"%d",IDB);serialPrintf(fd,"V" );char Buffer[3];sprintf(Buffer,"%3d",value);serialPrintf(fd,"%s\n",Buffer);serialPrintf(fd,"\n");printf("I send ");printf("%d",IDB);printf(" V ");printf("%d",value);printf(" \n");printf("Wait for Response\n");digitalWrite(SSC,LOW);while(digitalRead(SSC)){}wait=0;while(serialDataAvail(fd)!=6 && wait<1){standby(2);//printf("%d",standby(1));}if(wait>=1){returnID=255;}else {recvWithEndMarker();Act();}//channel Select for LED Misingif(IDB==returnID && (value==returnValue)){ digitalWrite(LEDG,HIGH); digitalWrite(LEDR,LOW); printf("Corrrect returnvalue\n");}else if (returnID==255){ digitalWrite(LEDG,LOW); digitalWrite(LEDR,HIGH); printf("No Device\n");}else if(returnID!=IDB || returnValue!=value){ digitalWrite(LEDG,HIGH); digitalWrite(LEDR,HIGH); printf("Bad Return\n");}}//for IDB1printf("Valve loop completed");for(uint8_t IDB=0;IDB<=7;IDB++){printf("FlowLoop");digitalWrite(SSC,HIGH);while(digitalRead(SSC)){}serialPrintf(fd,"%d",IDB);serialPrintf(fd,"M");serialPrintf(fd,"\n");printf("I send M to ");printf("%c",IDB);printf("for Measure Flowrates\n");digitalWrite(SSC,LOW);while(digitalRead(SSC)){};wait=0;while(serialDataAvail(fd)!=43 && wait<1){standby(9);}if (wait>=1){returnID=255;}else{recvWithEndMarker(); Act();}if(IDB==returnID){ digitalWrite(LEDG,HIGH); digitalWrite(LEDR,LOW); printf("I got FlowResponse form: ");printf("%c",IDB);printf("\n");}else if (returnID==255){ digitalWrite(LEDG,LOW); digitalWrite(LEDR,LOW); printf("No Flow Response\n");}}//for IDB2}//mainLoop While(1)return 0;}//Setup Main
The code works up to this point
Code:
}//for IDB1
but the printf below is not executed when the research loop has run through
This is what the output of the code looks like (for the first test I don't have listeners connected.
Code:
root@Database:/etc/GA_MK23/test# ./RS485MasterRaspi Start RS485 TestVerion Nr.2 I send 0 V 0 Wait for ResponseTIMEOUTNo DeviceVCI send 1 V 3 Wait for ResponseTIMEOUTNo DeviceVCI send 2 V 6 Wait for ResponseTIMEOUTNo DeviceVCI send 3 V 9 Wait for ResponseTIMEOUTNo DeviceVCI send 4 V 12 Wait for ResponseTIMEOUTNo DeviceVCI send 5 V 15 Wait for ResponseTIMEOUTNo DeviceVCI send 6 V 18 Wait for ResponseTIMEOUTNo DeviceVCI send 7 V 21 Wait for ResponseTIMEOUTNo Device
Statistics: Posted by Mathrim — Thu Oct 31, 2024 9:04 am — Replies 1 — Views 21