我正在寻找可以执行此示例的示例代码或代码。
我有2个字符缓冲区
Character_Buf1=<**DATA1,1**> Format used to receive character from Serial Receive
character Buf2=<**DATA1,1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val2**>
其中val1是整数值。我需要将它转换为一个字符并附加在Buf2
中If Externally send request <DATA1,1>
if Char_buf1[First_6character]=Char_buf2[First_6character]
Char_Buf[100]=<**DATA1,1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val1,val2**>
print Char_buf2[]
其他打印字符缓冲区不匹配。
If serial request
char_buf1
其中charact_buf1是从Arduino委员会发送的sereial请求。
从我从堆栈溢出中找到的代码中,我能够按预期发送请求&amp;在案件之间切换。如何通过值转换实现字符缓冲区数据存储?
#include "glob.h"
const int BUFFER_SZ = 32;
char sendBuffer[60]={'0'};
void Take_Reading() {
for (int row = 0; row < 8; row++)
{
// // get rid of me:
digitalWrite(SO_enable, array[row][0]);
digitalWrite(S1_enable, array[row][1]);
digitalWrite(S2_enable, array[row][2]);
delay(50);
analog_1_pv[row] = ANALOG_SCALING * analogRead(A0);
analog_2_pv[row] = ANALOG_SCALING * analogRead(A1);
analog_3_pv[row] = ANALOG_SCALING * analogRead(A2);
if ( (analog_1_pv[row] <= MINV_RANGE || (analog_1_pv[row] >= MAXV_RANGE) ))
{
Current_Value1[row] = 0.0;
}
else
{
Current_Value1[row] = (analog_1_pv[row] * 12.5) - 31.25;
}
if ( (analog_2_pv[row] <= MINV_RANGE || (analog_2_pv[row] >= MAXV_RANGE) ))
{
Current_Value2[row] = 0.0;
}
else
{
Current_Value2[row] = (analog_2_pv[row] * 12.5) - 31.25;
}
if ( (analog_3_pv[row] <= MINV_RANGE || (analog_3_pv[row] >= MAXV_RANGE) ))
{
Current_Value3[row] = 0.0;
}
else
{
Current_Value3[row] = (analog_3_pv[row] * 12.5) - 31.25;
}
Mod_current[row] = (uint16_t)(Mul_Factor * Current_Value1[row]);
Mod_current[row + 8] = (uint16_t)(Mul_Factor * Current_Value2[row]);
Mod_current[row + 16] = (uint16_t)(Mul_Factor * Current_Value3[row]);
}
}
int ID_Check() {
int ID_value;
for (int row = 0; row < 8; row++)
{
digitalWrite(SO_enable, array[row][0]);
digitalWrite(S1_enable, array[row][1]);
digitalWrite(S2_enable, array[row][2]);
Status_Out[row] = digitalRead(Output_Read);
}
ID_value = 1 * Status_Out[7] + 2 * Status_Out[6] + 4 * Status_Out[5] + 8 * Status_Out[4] + 16 * Status_Out[3] + 32 * Status_Out[2] + 64 * Status_Out[1] + 128 * Status_Out[0];
return (ID_value);
}
int Take_Temp_Reading()
{
Temp_Total = Temp_Total - Temp_readings[Temp_index];
Temp_readings[Temp_index] = analogRead(A5);
Temp_Total = Temp_Total + Temp_readings[Temp_index];
Temp_index = Temp_index + 1;
if (Temp_index >= numReadings)
{
Temp_index = 0;
Temp_Average = Temp_Total / numReadings;
}
temp = (Temp_Average * 5.0) / 1023.0;
// temp = float(analogRead(A5)) * 5.0 / 1024.0;
temp_int = (int)(temp * 100.0);
return (temp_int);
// Serial.print("Temp"); Serial.println(temp);
}
int SPD_Check()
{
SPD_STATUS = digitalRead(SPD_STATUS_PIN);
return (SPD_STATUS);
}
int DC_Status()
{
DC_STATUS = digitalRead(DC_STATUS_PIN);
return (DC_STATUS);
}
int Take_HV_Reading()
{
//int analog_int= analogRead(A4);
total = total - readings[index];
// read from the sensor:
readings[index] = analogRead(A4);
// add the reading to the total:
total = total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings1)
{
index = 0;
average = total / numReadings1;
}
else
{
HV_voltage = (average * 5.0) / 1023.0;
}
if (HV_voltage <= 0.25)
{
Conv_HV_voltage = 0.0;
}
else
{
Conv_HV_voltage = 197.837837838 * HV_voltage + 10.8108108108;
}
HV_Reading = (uint16_t)(Conv_HV_voltage * 10);
// HV_Reading = (uint16_t)(Conv_HV_voltage * 10);
return(HV_Reading);
}
void Serial_Command1()
{
Take_Reading();
int Temp_read=Take_Temp_Reading();
int HV_Read=Take_HV_Reading();
SPD=SPD_Check();
DISCONNECTOR=DC_Status();
Serial.println("Command1 executed ");
sendBuffer[1]=char(Mod_current[1]) ;
sendBuffer[2]= ',';
sendBuffer[2]= Mod_current[1];
sendBuffer[3]= ',';
sendBuffer[4]= Mod_current[2];
sendBuffer[5]= ',';
sendBuffer[6]= Mod_current[3];
sendBuffer[7]= ',';
sendBuffer[8]= Mod_current[4];
sendBuffer[9]= ',';
sendBuffer[10]= Mod_current[5];
sendBuffer[11]= ',';
sendBuffer[12]= Mod_current[6];
sendBuffer[13]= ',';
sendBuffer[14]= Mod_current[7];
sendBuffer[15]= ',';
sendBuffer[16]= Mod_current[8];
sendBuffer[17]= ',';
sendBuffer[18]= Mod_current[9];
sendBuffer[19]= ',';
sendBuffer[20]= Mod_current[10];
sendBuffer[21]= ',';
sendBuffer[22]= Mod_current[11];
sendBuffer[23]= ',';
sendBuffer[24]= Mod_current[12];
sendBuffer[25]= ',';
sendBuffer[26]= Mod_current[13];
sendBuffer[27]= ',';
sendBuffer[28]= Mod_current[23];
sendBuffer[29]= ',';
sendBuffer[30]= Mod_current[22];
sendBuffer[31]= ',';
sendBuffer[32]= Mod_current[21];
sendBuffer[33]= ',';
sendBuffer[34]= Mod_current[20];
sendBuffer[35]= ',';
sendBuffer[36]= Mod_current[19];
sendBuffer[37]= ',';
sendBuffer[38]= Mod_current[18];
sendBuffer[39]= ',';
sendBuffer[40]= Mod_current[17];
sendBuffer[41]= ',';
sendBuffer[42]= Mod_current[16];
sendBuffer[43]= ',';
sendBuffer[44]= Mod_current[15];
sendBuffer[45]= ',';
sendBuffer[46]= Mod_current[14];
sendBuffer[47]= ',';
sendBuffer[48]= Temp_read;
sendBuffer[49]= ',';
sendBuffer[50]= HV_Read;
sendBuffer[51]= ',';
sendBuffer[52]= SPD;
sendBuffer[53]= ',';
sendBuffer[54]= DISCONNECTOR;
Serial.println(sendBuffer[1]);
}
void Serial_Command2(int a)
{
Serial.print("Command2 executed: ");
Serial.println(a);
}
void setup()
{
Serial.begin(57600);
Serial.println("Format 1: <SMCB1,1>");
Serial.println("Format 2: <SMCB1,1,Length>");
}
// Parse the request, WITHOUT the '<' and '>' delimiters.
void parseRequest(char *request)
{
// Check the request starts with the prefix "SMCB1,1".
if (strncmp(request, "SMCB1,1", 7) != 0) {
Serial.println(F("Error: bad request prefix"));
return;
}
// Remove that known prefix.
// Now we have either "", ",Length" or ",Timer,On_OFF"
request += 7;
// Format 1: ""
if (request[0] == '\0') {
//Serial.println(F("Received format 1"));
Serial_Command1();
return;
}
// Remove the leading comma.
// Now we have either "Length" or "Timer,On_OFF"
if (request[0] != ',') {
Serial.println(F("Error: ',' expected"));
return;
}
request++;
// Format 2: "Length", no comma.
char * comma = strchr(request, ',');
if (!comma) {
int length = atoi(request);
Serial_Command2(length);
//Serial.print(F("Received format 2, length = "));
// Serial.println(length);
return;
}
}
void loop()
{
static char buffer[BUFFER_SZ]; // received chars
static size_t pos; // current position in buffer
static bool insideRequest; // are we between '<' and '>'?
if (Serial.available()) {
char c = Serial.read();
if (insideRequest) {
if (c == '>') { // end of request received
buffer[pos] = '\0'; // terminate the string
parseRequest(buffer);
pos = 0; // get ready for the next request
insideRequest = false;
}
else if (pos < BUFFER_SZ-1) { // add char to buffer
buffer[pos++] = c;
}
}
else if (c == '<') { // start of request
insideRequest = true;
}
}
}
我编辑了代码。在这里,我可以切换模式。但是如何将数字存储到 Serial_Command1()函数的字符缓冲区中。我无法将商店数据打印到字符缓冲区数组中。