https://blog.csdn.net/qq_27694835/article/details/108593864 //汉字转hex
#include <ESP8266WiFi.h> #include <ESP8266HTTPClient.h> HTTPClient http; WiFiClient client; const char* ssid = "你大哥"; // wifi名 const char* password = "11111111"; // wifi密码 const char* url="https:/qwtz.net/8266/api/?s="; String wxname=""; void setup() { Serial.begin(115200); delay(10); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { god("北北"); delay(3000); //Serial.print(char2hexstr(wxname.c_str(),sizeof(wxname)-1)); String hexstring = "61707069643aB2E2CAD4"; // int leng = hexstring.length()/2; // char destByte[]={0}; // int j=0; // for(int i=0;i<leng;i++) { // byte high = (byte) (Character_h.digit(hexstring.charAt(j), 16) & 0xff); // byte low = (byte) (Character.digit(hexstring.charAt(j + 1), 16) & 0xff); // destByte[i] = (byte) (high << 4 | low); // j+=2; // } //hexStrToBytes(hexstring); Serial.println(hexStrToBytes(hexstring)); } byte[] hexStrToBytes(String hexStr){ int len = hexStr.length()/2; byte[] bytes = new byte[len]; char[] chars = hexStr.toCharArray(); for(int i=0;i<len;i++){ bytes[i] = (toByte(chars[i])<<4) | toByte[i*2]; } return bytes; } byte toByte(char c){ byte b = "0123456789ABCDEF".indexOf(c); return b; } void god(String wx){ http.setReuse(true); http.begin(client, "http://qwtz.net/8266/api/?s="+wx); int httpCode = http.GET(); if (httpCode > 0) { Serial.printf("[HTTP] GET... code: %d\n", httpCode); if (httpCode == HTTP_CODE_OK) { // http.writeToStream(&Serial); String wxname = http.getString(); Serial.println(wxname+"test2"); http.end(); } } else { Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); http.end(); http.begin(client, "http://baidu.com"); } }
void setup() { Serial.begin(115200); while (!Serial); String inputString = "48454C4C4F20574F524C4421"; int str_len = inputString.length() + 1; char char_array[str_len]; int ASCIINumber_len = str_len / 2; byte ASCIINumberByte[ASCIINumber_len]; /* inputString.toCharArray(char_array, str_len); for (char i = 0; i < str_len; i++) { byte extract; char a = char_array[2 * i]; char b = char_array[2 * i + 1]; extract = HexChangeDec(a) << 4 | HexChangeDec(b); ASCIINumberByte[i] = extract; } */ for (char i = 0; i < str_len; i++) { byte extract; char a = inputString.charAt(2 * i); char b = inputString.charAt(2 * i + 1);; extract = HexChangeDec(a) << 4 | HexChangeDec(b); ASCIINumberByte[i] = extract; } for (int j = 0 ; j < ASCIINumber_len; j++) { Serial.print("0x"); Serial.println(ASCIINumberByte[j], HEX); } for (int k = 0 ; k < ASCIINumber_len; k++) { Serial.print(char (ASCIINumberByte[k])); } Serial.println(""); delay(100); } void loop() { ; } char HexChangeDec(char ch) { char returnType; switch (ch) { case '0': returnType = 0; break; case '1' : returnType = 1; break; case '2': returnType = 2; break; case '3': returnType = 3; break; case '4' : returnType = 4; break; case '5': returnType = 5; break; case '6': returnType = 6; break; case '7': returnType = 7; break; case '8': returnType = 8; break; case '9': returnType = 9; break; case 'A': returnType = 10; break; case 'B': returnType = 11; break; case 'C': returnType = 12; break; case 'D': returnType = 13; break; case 'E': returnType = 14; break; case 'F' : returnType = 15; break; default: returnType = 0; break; } return returnType; }
解决了,hexstring to hex 的问题
#include <SoftwareSerial.h> unsigned char item[8] = {0x02, 0x03, 0x00, 0x00, 0x00, 0x10, 0x44, 0x35}; //16进制命令 String data = ""; //接收到的16进制字符串 SoftwareSerial RS485(3, 2); // RX, TX void setup() { RS485.begin(9600); Serial.begin(115200); } void loop() { delay(1000); //放慢输出频率 for (int i = 0 ; i < 8; i++) { // 发送命令 RS485.write(item[i]); // write输出 } delay(1000); // 等待测温数据返回 data = ""; while (RS485.available()) //从串口中读取数据 { unsigned char in = (unsigned char)RS485.read(); //read读取 Serial.print(in,HEX); Serial.print(','); data += in; data += ','; } if (data.length() > 0) //输出接收到的数据 { Serial.println(); Serial.println(data); } }