sending and recieving via rs485 works but the value what i get from the reciever wannt to Show on the http page and when I refesh the page, it should be shown the update value on the page. Could you help me further?
Thank you very much!
Nahom
let’s assume the Master send request to the slave and slave reply quensquently. The communication works without problem and I implement for this request and reply method a state machine in the main function under while loop. After I integrate my program to yours unfortunatly the program jumps the state machine and build the server communication but the rs485 communication stops.
…
int main ()
{
while(true)
{
switch (state)
{
case ‘M’:
if((Master.readable())) //read Data from Master
{
d = Master.getc()
if (d == ‘M’)
{
state = ‘S’;
}
else
state = ‘M’;
}
break;
case S:
Slave4 = (Slave4 << 8) | d; //shift the 8bit result to left for Slave 4
if (++counter4 >= 3)
{
if(Slave4 > 0x7FFFFF)
{
Slave4 = Slave4|0xFF000000;
}
S4 = Slave4*0.1;
pc.printf(“Slave4_4 = %.1f\n”, S4); // send the result to com terminal
counter4 = 0;
state = M;
}
break;
default: state = M;
}
}
}
my problem is to show or update the float S4 value on the web page because i get always 0.0, do you have an idea? or better method ?
For your information, you can make code blocks by placing triple backticks before and after your code. It makes it easier to read.
```cpp
// your code starts after triple backticks (cpp tag is optional)
#include “mbed.h”
int d;
float S4;
string& showWebPage(int status)
{
char roomTempStr[5];
// .....
// close the code block with triple backticks
```
Thank you for the advice! I used to insert four spaces at the begin of each source line to get it formatted. But your hint makes life easier
Hello Nahom,
Rather than to run the RS485 receiver state machine in the main thread I’d suggest to run it in another one in parallel with the main.
For example:
#include "mbed.h"
...
float S4;
...
Thread rcvRS485Thread(osPriorityAboveNormal); // RS485 receiver thread
...
//RS485 receive routine running in the rcvRS485Thread (in parallel with the main() task)
void rcvRS485Task()
{
while(true)
{
switch (state)
{
case ‘M’:
if((Master.readable())) //read Data from Master
{
d = Master.getc()
if (d == ‘M’)
{
state = ‘S’;
...
S4 = Slave4*0.1;
...
}
string& showWebPage(int status)
{
char roomTempStr[6];
sprintf(roomTempStr, "%3.1f", S4);
...
}
vod main()
{
...
rcvRS485Thread.start(rcvRS485Task); // start the RS485 receive task to run in parallel with the following while loop
//listening for http GET request
while (true) {
...
}
}
Then two endless while loops (one in the main() and another one in the rcvRS485Task() running in parallel) will handle two different tasks. Switching between them is done periodically (automatically) by the underlying Mbed Operating System.
Hello Kentaro,
Thank you for your Suggestion I will do for future.
Hello Hudakz,
Thank you very much for your Reply, I implemented as you suggested and have got “No page access” and also the communication will be blocked.
I used to test the priority the below program. It works for normal,low ,below and Idle that means you can update the poti value on the page. But I got for prioAbove no page Access. I thought I can set any prio for the thread. Is there anything that I shoud not consider? could you please explain further?
Thread rcvRS485Thread(osPriorityAboveNormal); // RS485 receiver thread
//RS485 receive routine running in the rcvRS485Thread (in parallel with the main() task)
void rcvRS485Task()
{
while (true)
{
adc_value = analog_sensor.read_u16(); // Read the ADC
voltage_value = 3.3*adc_value/65535; // Read and convert to voltage
percent_value = analog_sensor.read(); // Read the percent
led=!led;
}
}
. . .
string& showWebPage(int status)
{
char AdcStr[1024];
char VoltageStr[1024];
char PercentStr[1024];
sprintf(AdcStr, "ADC = %d ", adc_value);
sprintf(VoltageStr, "Voltage = %.4fV ", voltage_value);
sprintf(PercentStr, "Percent = %.2f ", percent_value);
int main()
{
. . .
rcvRS485Thread.start(rcvRS485Task); // start the RS485 receive task to run in parallel with the following while loop
while (true) {
. . .
}
}
I’m sorry, It was just an example I have never tested. I used osPriorityAboveNormal for the RS485 receiver only to show it doesn’t have to match main’s priority. Sorry I don’t know why it does not work (the Ethernet and TCP/IP are quite complex things). Use whatever priority works for you.
I will try to digout the solution. But if I use two interfaces rs485 and Ethernet there is something blocked that the rs485 does not work.
Thank you very much for your help and time!
Nahom
void rcvRS485Task()
{
while (true) {
adc_value = analog_sensor.read_u16(); // Read the ADC
voltage_value = 3.3*adc_value/65535; // Read and convert to voltage
percent_value = analog_sensor.read(); // Read the percent
led=!led;
}
}
This task does not release the CPU, it is consuming 100 % CPU power when running with higher prio than others. There should be a ThisThread::sleep_for() or sleep_until() to give other threads the chance to run.