Dear friends;
I need to send several strings to a device. But in order to work the device requires that I send according a protocol. For example to send the string "hello" at position X =5 and Y =12 i will need send: (Serial.write)
3 fixed bytes (commands to write )
1 byte (position X variable = 0x05)
1 byte (position Y variable = 0x0C)
10 bytes : (0xA3) , ("h"), (0xA3,("e"),(0xA3), ("l"),(0xA3),("l"),(0xA3),("o") (need to add 0xA3 (fixed) before each character of the string)
1 byte (chksum) chksum = exor of all above bytes
What is the best way to do this ?
I mind something like:
x= 5;
y=12;
mystring = "hello";
callMyFunction();
void callMyFuncion {
????? how implement this function ?
}
I needed to send data (in bytes). The actual output is in string format, i.e. 45.3 is "4" "5" "." "3"
So I created an array of bytes. Filled in the locations I needed to and set it out using the UART.
void sendHubitat(){
zArray[0] = mHumidity;
zArray[1] = round(mTemperature); // units)
zArray[2] = round((mTemperature - zArray[1])*10); // tenths
//zArray[3] = ETX pre declaired global.
Serial.write(zArray,sizeof(zArray));
}
something like this maybe:
void writeText(uint8_t x, uint8_t y, char *str) {
int i = 0;
uint8_t checksum = 0;
//send write command
Serial.write(writecommandbyte1); //update with the ACTUAL byte value!
checksum ^= writecommandbyte1; //update checksum
Serial.write(writecommandbyte2); //update with the ACTUAL byte value!
checksum ^= writecommandbyte2; //update checksum
Serial.write(writecommandbyte2); //update with the ACTUAL byte value!
checksum ^= writecommandbyte1; //update checksum
//send 'x' value
Serial.write(x);
checksum ^= x; //update checksum
//send 'y' value
Serial.write(y);
checksum ^= y; //update checksum
//send string
while (str[i] != '\0') {
Serial.write(0xA3);
checksum ^= 0xA3; //update checksum
Serial.write(str[i]);
checksum ^= str[i]; //update checksum
++i;
}
//send checksum
Serial.write(checksum);
}
void setup() {
uint8_t x = 5;
uint8_t y = 12;
char str[] = "hello";
Serial.begin(9600); //update baudrate to match device speed
writeText(x, y, str);
}
void loop()
{
}
note that you did not provide the write command byte so I could not include in it this sample code. please update accordingly.
Hope that helps....
1 Like
gcjr
May 12, 2022, 9:07am
4
consider
dispBuf
01 02 03 05 0c a3 68 a3 65 a3 6c a3 6c a3 6f c8
#define BUF_SIZE 80
byte buf [BUF_SIZE];
byte prefix [] = { 1, 2, 3 };
// -----------------------------------------------------------------------------
int
msg1 (
byte *buf,
int maxByte,
byte *prefix,
byte x,
byte y,
const char *s )
{
if (BUF_SIZE < strlen(s) + 6)
return -1;
memcpy (buf, prefix, 3);;
buf [3] = x;
buf [4] = y;
int n = 5;
for (unsigned i = 0; i < strlen(s); i++) {
buf [n++] = 0xA3;
buf [n++] = s [i];
}
byte chksm = 0;
for (int i = 0; i < n; i++)
chksm ^= buf [i];
buf [n++] = chksm;
return n;
}
// -----------------------------------------------------------------------------
void
dispBuf (
byte *buf,
int nByte )
{
char s [80];
Serial.println (__func__);
for (int i = 0; i < nByte; i++) {
sprintf (s, " %02x", buf [i]);
Serial.print (s);
}
Serial.println ();
}
// -----------------------------------------------------------------------------
void setup (void)
{
Serial.begin (9600);
int n = msg1 (buf, BUF_SIZE, prefix, 0x5, 0xC, "hello");
dispBuf (buf, n);
}
// -----------------------------------------------------------------------------
void loop (void)
{
}
what you describe is the format of a message, not a protocol
a protocol is the sequences of messages between two (or more) endpoints allowing them to reliably exchange a set of commands or data
1 Like
system
Closed
November 8, 2022, 9:08am
5
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.