Cdma cat sends a Chinese text message using the AT command (C #)
Http://www.cublog.cn/u1/38200/showart_2211241.html
CDMA cats can only send text messages. The Chinese text message is Unicode rather than PDU, and cannot be input in the Super Terminal. Only programs can be written. This problem is often discussed on the Internet, and sometimes garbled. Post the successful code of C.
Reprinted please indicate the source
Void sendchnsms (string content, string phone)
{
// Chinese CDMA Transmission, unicode encoded bytes
Byte [] B = encoding. bigendianunicode. getbytes (content );
// You cannot add 86 to the phone number of the AT command of CDMA. Otherwise, although the message is successfully sent, the SMS center responds to error code 5.
If (phone. indexof ("86") = 0)
{
Phone = phone. substring (2 );
}
// Assume it is a com3 port.
SerialPort sp = new SerialPort ("com3 ");
// Unlike a GSM cat, the baud rate is 115200. This problem has been plagued for a long time.
Sp. baudrate = 115200;
Sp. open ();
// Set Chinese text message
Sp. Write ("at + wscl = 6, 4 \ r ");
Thread. Sleep (500 );
// Set text message format
Sp. Write ("at + cmgf = 1 \ r ");
Thread. Sleep (500 );
// Set the length of the sent number and sent content in bytes.
Sp. Write ("at + cmgs = \" "+ phone +" \ "," + B. Length + "\ r ");
Thread. Sleep (500 );
// Write
Sp. Write (B, 0, B. Length );
Thread. Sleep (500 );
// Write Ctrl + Z to end the text message content. Note that two bytes are required in Unicode mode. This is also the reason why the operation cannot be performed on a Super Terminal.
Byte [] b2 = new byte [] {0x00, 0x1a };
Sp. Write (B2, 0, b2.length );
Sp. Close ();
}
------------------------------------------------------------
My experiences:The baud rate is not necessarily 115200. It depends on the factory settings of cats from different manufacturers. It may be 9600 or other. "At + cmgs =" "---- Add the phone number directly. If no text length is added, the message can be sent successfully and sent directly to the Chinese language. garbled characters may appear when the last character is sent, add a space to avoid garbled characters. -------------------------------------------------------------
The previous manual encoding method (equivalent to bigendianunicode in the code above)
Private string getmessage (string strvalue)
{
String strtemp = "";
Int ilen = 0;
Byte [] bt = new byte [1024];
Byte bttemp = new byte ();
String strvalue1 = strvalue;
System. Text. encoding. Unicode. getbytes (strvalue1, 0, strvalue1.length, BT, 0 );
For (INT I = 0; I <Bt. length; I = I + 2)
{
If (BT [I] = 0 & BT [I + 1] = 0)
{
Ilen = I;
I = Bt. length;
}
Else
{
Bttemp = BT [I];
BT [I] = BT [I + 1];
BT [I + 1] = bttemp;
}
}
// The default encoding in C # is Unicode.
Strtemp = system. Text. encoding. Default. getstring (BT, 0, ilen );
Return strtemp;
}
Common encoding formats
ASCII:The first encoding method in the United States. Each byte represents one character. The highest bit is 0, and the encoding range is 0 × 00-0 × 7f (127 ), the ASCII character set includes English letters, Arabic numerals, punctuation marks, and other characters. 0x00-0x20 and 0 x 7f contain 33 control characters.
Expanded ASCII:According to the characteristics of their own language, European countries also add the highest bit in ASCII encoding to add some European characters that are unique outside English characters. After expansion, there are a total of 256 characters, the first 127 of which remain unchanged.
Unicode (UCS ):The default Unicode encoding uses littleendian for low-level storage. Generally, two more bytes indicate a single character, 256*256 = 65536 characters. Since the characters in countries such as Asia are not fully represented by one byte and can only be represented by multiple characters, Unicode encoding is developed. UCOS: Unicode Character Set. Generally, Unicode indicates the abbreviation of UCOS.
Bigendianunicode:It is in the opposite order of Unicode storage.
UTF-8/UTF-16/UTF-32:Unicode cannot cover all historical texts, nor can it solve the problem of transmission (implantation head-ache's), especially in network-based applications. Therefore, unicode uses three encoding methods based on some basic reserved characters. They are UTF-8, UTF-16, and UTF-32 respectively. As the name suggests, in a UTF-8, a character is encoded in an 8-bit sequence and represents a character in one or several bytes. The biggest benefit of this approach is that the UTF-8 retains the ASCII character encoding as a part of it, for example, in the UTF-8 and ASCII, "a" encoding is 0 × 41.
The UTF-16 and UTF-32 are Unicode 16-bit and 32-bit encoding methods, respectively. Given the initial purpose, Unicode is typically a UTF-16. When discussing Unicode, it is very important to determine which encoding method is used. The conversion between GBK, gb2312, and utf8 must be unicode encoded:
GBK, gb2312 -- Unicode -- utf8
Utf8 -- Unicode -- GBK, gb2312
GBK/gb2312/gb18030:
Gb2312: it is designed based on the location code. The encoding table is divided into 94 areas, each of which corresponds to 94 places. The combination of the area code and the location code of each character is the location code of this Chinese character. Generally, the location code is represented by a 10-digit number. For example, if the value is 1601, it indicates 16-digit and 1-digit. The corresponding character is "ah ". Add 0xa0 to the area code and bit Code respectively to get gb2312 encoding. In the location code, the 01-09 area is the symbol and number area, the 16-87 area is the Chinese character area, and the 10-15 and 88-94 areas are undefined blank areas. It divides the recorded Chinese characters into two levels: the first level is 3755 commonly used Chinese characters, which are placed in the 16-55 area and arranged in the order of Chinese pinyin letters/PEN; the second-level Chinese characters are 3008 frequently used Chinese characters, which are placed in Area 56-87 and arranged in sequence by the beginning/strokes. First-level Chinese characters are sorted by pinyin. This gives you the range of a pinyin In the first-level Chinese character location. Many programs that can obtain pinyin based on Chinese characters are compiled based on this principle.
In addition to common simplified Chinese characters, the gb2312 Character Set also contains Greek letters, Japanese hirakana, Katakana letters, and Russian Spanish letters. You can use traditional Chinese characters to test whether some systems only support gb2312 encoding. The encoding range of gb2312 is 0xa1a1-0 × 7e7e. After undefined areas are removed, the actual encoding range is 0xa1a1-0xf7fe.
The EUC-CN can be understood as an alias for gb2312, which is exactly the same as gb2312.
The location code should be considered as the definition of Character Set, defining the included characters and character location, while gb2312 and EUC-CN are the encoding of this character set in the actual computer environment. Hz and iso-2022-cn are two types of codes corresponding to the location code character set. They both use a 7-bit encoding space to support Chinese characters. The relationship between the location code and the gb2312 code is a bit like Unicode and UTF-8.
GBK encoding: it is a superset of gb2312 encoding and is fully compatible with gb2312. At the same time, GBK contains all the CJK Chinese characters in the basic multi-text plane of Unicode. Like gb2312, GBK also supports Greek letters, Japanese Kana letters, Russian letters, and other characters, but does not support tabulation characters (non-Chinese characters) in Korean ). GBK also contains the Chinese radical and vertical punctuation characters not included in gb2312.
The gb18030 encoding is backward compatible with GBK and gb2312. The compatibility meaning is not only compatible with characters, but also the same encoding for the same characters. Gb18030 contains all the characters in unicode3.1, including Chinese Ethnic Minorities and Korean characters not supported by GBK. It can also be said that the text symbols of most nationalities in the world are included. Both GBK and gb2312 are dual-byte width encoding. If it is considered to be single-byte compatible with ASCII, it can also be understood as a single-byte and dual-byte mixed variable-length encoding. The gb18030 encoding method is variable-length encoding, which can be single-byte, dual-byte, or four-byte.