Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication1.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <windows.h>
- #include <iostream>
- #include <cmath>
- #include <cstdlib>
- #include <fstream>
- using namespace std;
- HANDLE hSerial;
- bool connected = FALSE;
- int to_int(char a) {
- int b = a;
- if (b < 0) {
- b = 256 + b;
- }
- return b;
- }
- int LenBin(int a) {
- if (a < 2)
- return 1;
- else if (a < 4)
- return 2;
- else if (a < 8)
- return 3;
- else if (a < 16)
- return 4;
- else if (a < 32)
- return 5;
- else if (a < 64)
- return 6;
- else if (a < 128)
- return 7;
- else if (a < 256)
- return 8;
- else if (a < 512)
- return 9;
- else if (a < 1024)
- return 10;
- else if (a < 2048)
- return 11;
- else if (a < 4096)
- return 12;
- else if (a < 8192)
- return 13;
- else return 14;
- }
- int division(int a, int b) {
- bool end = FALSE;
- int ostatok;
- int c;
- int sdvig;
- while (!end) {
- if (LenBin(a) > LenBin(b)) {
- c = b << (LenBin(a) - LenBin(b));
- ostatok = a^c;
- sdvig = LenBin(a) - LenBin(b);
- a = ostatok;
- }
- else {
- ostatok = a^b;
- }
- if (LenBin(ostatok) < LenBin(b)) {
- end = TRUE;
- }
- }
- return ostatok;
- }
- int decode(int vector) {
- int polinom = 19;
- int sindrom[15][2] = {
- { 1, 1 },
- { 2, 2 },
- { 4, 4 },
- { 8, 8 },
- { 3, 16 },
- { 6, 32 },
- { 12, 64 },
- { 11, 128 },
- { 5, 256 },
- { 10, 512 },
- { 7, 1024 },
- { 14, 2048 },
- { 15, 4096 },
- { 13, 8192 },
- { 9, 16384 },
- };
- int ostatok = division(vector, polinom);
- if (ostatok) {
- for (int i = 0; i < 14; i++)
- {
- if (sindrom[i][1] = ostatok) {
- //cout << endl << sindrom[i][1];
- vector = vector^sindrom[i][2];
- vector = vector >> 4;
- return vector;
- }
- }
- return -1;
- }
- else
- return vector>>4;
- }
- int encode(int vector) {
- int polinom = 19;
- int vec1 = vector << 4;
- vec1 = vec1^division(vec1, polinom);
- return vec1;
- }
- void SendCOM(char b) {
- DCB dcbSerialParams = { 0 };
- dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
- if (!GetCommState(hSerial, &dcbSerialParams))
- {
- cout << "getting state error\n";
- }
- dcbSerialParams.BaudRate = CBR_9600;
- dcbSerialParams.ByteSize = 8;
- dcbSerialParams.StopBits = ONESTOPBIT;
- dcbSerialParams.Parity = NOPARITY;
- if (!SetCommState(hSerial, &dcbSerialParams))
- {
- cout << "error setting serial port state\n";
- }
- //cout << (LPCVOID)b << endl << sizeof(b) << endl;
- char data[] = {to_int(b)};
- DWORD dwSize = sizeof(data); // размер этой строки
- DWORD dwBytesWritten; // тут будет количество собственно переданных байт
- BOOL iRet = WriteFile(hSerial, data, dwSize, &dwBytesWritten, NULL);
- //cout << dwSize << " Bytes in string. " << dwBytesWritten << " Bytes sended. " << endl;
- }
- void frame_prepare(int vectors[], int count) {
- if (count < 254) {
- SendCOM(char(255));
- SendCOM(char(7));
- SendCOM(char(1));
- SendCOM(char(count));
- for (int i = 1; i < count+1; i++) {
- SendCOM(char((vectors[i - 1] & 32512)>>8));
- SendCOM(char(vectors[i - 1] & 255));
- }
- SendCOM(char(255));
- }
- else {
- int c = 1;
- double e = (double)count / 254;
- while (c <= ceil(e)) {
- SendCOM(char(255));
- SendCOM(char(7));
- SendCOM(char(c));
- int cnt;
- if (count - (c-1) * 254 > 254) {
- cnt = 254;
- }
- else {
- cnt = count - 254 * (c - 1);
- }
- SendCOM(char(cnt));
- int a;
- for (int i = 1; i < cnt + 1; i++) {
- SendCOM(char((vectors[255*(c-1)+i - 1] & 32512) >> 8));
- SendCOM(char(vectors[255*(c-1)+i - 1] & 255));
- }
- SendCOM(char(255));
- c++;
- }
- }
- }
- void get(int bytes[], int count) {
- int *byt = new int[count];
- for (int i = 0; i < count; i++) {
- byt[i] = encode(bytes[i]);
- }
- frame_prepare(byt, count);
- }
- void frame_release(int vectors[],int count) {
- if (count) {
- int i = 0;
- int vector[255];
- while (i<count * 2) {
- vector[i / 2] = decode((vectors[i] << 8) + vectors[i + 1]);
- cout << char(vector[i / 2]);
- i += 2;
- }
- int a = 0;
- }
- else {
- //TO DO Денис, а здесь ты будешь обрабатывать различную хрень типо Запроса соединения и прочего
- }
- int a = 0;
- //TO DO Денис, Здесь ты должен вызвать свою функцию, которая будет принимать от меня кадры
- }
- void ReadCOM()
- {
- DWORD iSize;
- char sReceivedChar;
- bool frame = FALSE;
- bool data = FALSE;
- int type = 0;
- int counter = 0;
- int length = 0;
- int data_count = 0;
- int data1[510];
- int i = 0;
- while (true)
- {
- ReadFile(hSerial, &sReceivedChar, 1, &iSize, 0); // получаем 1 байт
- if (iSize > 0) { // если что-то принято, выводим
- int byte = to_int(sReceivedChar);
- if (frame & !data & byte!=255) {
- if (!type) {
- type = byte;
- if (type != 7) {
- counter = 1;
- length = 1;
- }
- }
- else if (!counter) {
- counter = byte;
- }
- else if (!length) {
- length = byte;
- data = TRUE;
- }
- }
- else if (data) {
- if (i < length * 2) {
- data1[i] = byte;
- i++;
- }
- else {
- data = FALSE;
- if (byte = 255) {
- frame_release(data1, length);
- i = 0;
- type = 0;
- counter = 0;
- length = 0;
- data_count = 0;
- for (int c = 0; c < length * 2; c++) {
- data1[c] = 0;
- }
- frame = FALSE;
- }
- }
- }
- else if (byte = 255) {
- if (!frame & !data) {
- frame = TRUE;
- }
- else if (frame & !data) {
- i = 0;
- type = 0;
- counter = 0;
- length = 0;
- data_count = 0;
- if (type = 7) {
- frame_release(data1, length);
- }
- else {
- int cadr[1];
- cadr[0] = type;
- frame_release(cadr, 0);
- }
- for (int c = 0; c < length * 2; c++) {
- data1[c] = 0;
- }
- frame = FALSE;
- }
- }
- }
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- char* sPortName;
- sPortName = new char[6];
- cin >> sPortName;
- hSerial = ::CreateFile(sPortName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
- if (hSerial == INVALID_HANDLE_VALUE)
- {
- if (GetLastError() == ERROR_FILE_NOT_FOUND)
- {
- cout << "serial port does not exist.\n";
- }
- cout << "some other error occurred.\n";
- }
- while (1)
- {
- ReadCOM();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement