Что дано: есть устройство (ublox 6 GPS engine), которое создает виртуальный COM-порт и общается по нему. По идее, если мы хотим изменить его поведение по умолчанию, данному агрегату можно посылать команды. Формат команд описан в файлах от производителя:
Every Message starts with 2 Bytes: 0xB5 0x62
A 1 Byte Class Field follows. The Class defines the basic subset of the message
A 1 Byte ID Field defines the message that is to follow
A 2 Byte Length Field is following. Length is defined as being the length of the payload, only. It does not include Sync Chars, Length Field, Class, ID or CRC fields. The number format of the length field is an unsigned 16-Bit integer in Little Endian Format.
The Payload is a variable length field.
CK_A and CK_B is a 16 Bit checksum whose calculation is defined below.
The two CK_ values are 8-Bit Unsigned Integers, only! If you implement it with larger-sized integer values, make sure to Mask both CK_A and CK_B with 0xFF after both operations in the loop.
CK_A = 0, CK_B = 0
For(I=0;I<N;I++)
{
CK_A = CK_A + Buffer[I]
CK_B = CK_B + CK_A
}
Никак не могу заставить этот девайс реагировать на команды. Код такой:
Собственно, функция:
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| void set_parameters(void* serial, unsigned char u_class, unsigned char ID) {
unsigned char message_to_device[8];
message_to_device[0] = '0xB5';
message_to_device[1] = '0x62';
message_to_device[4] = '0x00';
message_to_device[5] = '0x00';
unsigned long bytes_sended;
BOOL stream_parameter;
message_to_device[2] = u_class;
message_to_device[3] = ID;
unsigned int CK_A = 0, CK_B = 0;
for (int i = 2; i < 6; i++) {
CK_A += message_to_device[i];
CK_B += CK_A;
}
message_to_device[6] = CK_A;
message_to_device[7] = CK_B;
stream_parameter = WriteFile(serial, message_to_device, sizeof(message_to_device), &bytes_sended, NULL);
if (stream_parameter == TRUE)
cout << "Set parameters... " << endl;
else
cout << "Can't set parameter " << endl;
if (sizeof(message_to_device) != bytes_sended)
cout << "Can't response buffer" << endl;
} |
|
И где она вызывается:
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| void base_init() {
p_serial_base = ::CreateFileA(port_name_base, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if (p_serial_base == INVALID_HANDLE_VALUE) {
if(GetLastError() == ERROR_FILE_NOT_FOUND)
cout << "Serial port does not exist." << endl;
cout << "Some error occurred" << endl;
}
else
cout << "Es geht!" << endl;
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(p_serial_base, &dcbSerialParams)) {
cout << "getting state error" << endl;
}
dcbSerialParams.BaudRate=CBR_9600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
if(!SetCommState(p_serial_base, &dcbSerialParams)) {
cout << "error setting serial port state" << endl;
}
else
cout << "Weiter..." << endl << endl;
set_parameters(p_serial_base, '0x02', '0x31');
listen_COM_port(p_serial_base);
CloseHandle(p_serial_base);
} |
|
Что здесь не слава богу? Все параметры взяты опять же из мануала производителя, проверить их в других условиях не могу.
Добавлено через 1 час 41 минуту
Проблема решена. Мною же.
Переменные типа unsigned char писать без кавычек.
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
| void set_parameters(void* serial, unsigned char u_class, unsigned char ID) {
unsigned char message_to_device[8];
message_to_device[0] = 0xB5;
message_to_device[1] = 0x62;
message_to_device[4] = 0x00;
message_to_device[5] = 0x00;
unsigned long bytes_sended;
BOOL stream_parameter;
message_to_device[2] = u_class;
message_to_device[3] = ID;
unsigned int CK_A = 0, CK_B = 0;
for (int i = 2; i < 6; i++) {
CK_A += message_to_device[i];
CK_B += CK_A;
}
message_to_device[6] = CK_A;
message_to_device[7] = CK_B;
stream_parameter = WriteFile(serial, message_to_device, sizeof(message_to_device), &bytes_sended, NULL);
if (stream_parameter == TRUE)
cout << "Set parameters... " << endl;
else
cout << "Can't set parameter " << endl;
if (sizeof(message_to_device) != bytes_sended)
cout << "Can't response buffer" << endl;
} |
|
И соответственно при вызове
C++ |
1
| set_parameters(p_serial_base, 0x02, 0x31); |
|