Наши проекты:
Журнал · Discuz!ML · Wiki · DRKB · Помощь проекту |
||
ПРАВИЛА | FAQ | Помощь | Поиск | Участники | Календарь | Избранное | RSS |
[3.128.199.88] |
|
Сообщ.
#1
,
|
|
|
Небольшой проект в C++ Builder6. Принимаются данные по com (~4800 байт) Сразу после приема посылаются 128 байт. Затем сразу после посылки нужно принять 128 байт. Все по очереди отображается в окнах Memo. первые 2 операции выполняются нормально. Проблемка в том, что не врублюсь как принять и отобразить последние 128 байт в DWORD WINAPI Input_thread
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "CSPIN" #pragma resource "*.dfm" #include <iostream> TForm1 *Form1; DWORD cb, rb; unsigned char buf_in[6000]; AnsiString qwe, qweRead; using namespace std; bool NbRead = false; char buf_r = 128; //--------------------------------------------------------------------------- void hexdumpstrRead ( char src[], size_t size) { static const char hex[] = "0123456789abcdef"; char *result = (char *)malloc((size > 0)? size*3: 1), *p = result; if (result) { while (size-- > 0) { *p++ = hex[(*src >> 4) & 0x0f]; *p++ = hex[*src++ & 0x0f]; *p++ = ' '; } p[(p > result)? -1: 0] = 0; } Form1->Memo2->Clear(); Form1->Memo2->Lines->Add(result); } //--------------------------------------------------------------------------- void hexdumpstrWrite ( char src[], size_t size) { static const char hex[] = "0123456789abcdef"; char *result = (char *)malloc((size > 0)? size*3: 1), *p = result; if (result) { while (size-- > 0) { *p++ = hex[(*src >> 4) & 0x0f]; *p++ = hex[*src++ & 0x0f]; *p++ = ' '; } p[(p > result)? -1: 0] = 0; } Form1->Memo1->Clear(); Form1->Memo1->Lines->Add(result); } //--------------------------------------------------------------------------- DWORD WINAPI Input_thread( LPVOID lpParam ) { while(1) { if(!NbRead) { ReadFile( Form1->hSerial, buf_in, Form1->buf_rez, &cb, NULL ); if(cb) { hexdumpstrRead(buf_in, Form1->buf_rez ); Form1->BufferSendCom[1] = Form1->bit1; Form1->BufferSendCom[2] = Form1->bit2; Form1->BufferSendCom[3] = Form1->bit3; int n_i = WriteFile( Form1->hSerial, &Form1->BufferSendCom, sizeof(Form1->BufferSendCom), &Form1->bytesWritten, NULL ); if( n_i == 1 ) Form1->Label1->Caption = "Write"; else Form1->Label1->Caption = "NoWrite"; hexdumpstrWrite(Form1->BufferSendCom,128); NbRead = true; } } // else if(NbRead) { ReadFile( Form1->hSerial, buf_in, buf_r, &rb, NULL ); if(rb) { hexdumpstrRead(buf_in, buf_r); NbRead = false; } } ::Sleep(1); } return 0; } //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { buf_rez = 4800; BaudR = CBR_115200; Handle_input = CreateThread( NULL, 0,Input_thread, &Handle_thread_input, CREATE_SUSPENDED, NULL); ::Sleep(10); } //--------------------------------------------------------------------------- void TForm1::Com_Setting(){ com_port += "\\\\.\\"; // com_port.c_str() com_port += ComboBox1->Text; hSerial = CreateFile(ComboBox1->Text.c_str(),GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); //SetupComm(hSerial, 999, 999); if(hSerial==INVALID_HANDLE_VALUE) { if(GetLastError()==ERROR_FILE_NOT_FOUND) { MessageBox(NULL,"serial port does not exist.","",MB_OK); } MessageBox(NULL,"serial port error","", MB_OK); } DCB dcbSerialParams = {0}; dcbSerialParams.DCBlength=sizeof(dcbSerialParams); if (!GetCommState(hSerial, &dcbSerialParams)) { MessageBox(NULL,"getting state error\n","",MB_OK); } dcbSerialParams.BaudRate=BaudR; dcbSerialParams.ByteSize=8; dcbSerialParams.StopBits=ONESTOPBIT; dcbSerialParams.Parity=NOPARITY; if(!SetCommState(hSerial, &dcbSerialParams)) { MessageBox(NULL,"error setting serial port state\n","",MB_OK); } com_port = ""; } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { for(int i = 0; i > 128; i++){ BufferSendCom[i] = 0; } bit1 = 0x45; bit2 = 0x00; bit3 = 0x13; Com_Setting(); ResumeThread(Handle_input); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { Timer1->Enabled = false; //CloseHandle(hSerial); } //--------------------------------------------------------------------------- void __fastcall TForm1::CSpinEdit1Change(TObject *Sender) { bit1 = CSpinEdit1->Value; } //--------------------------------------------------------------------------- void __fastcall TForm1::CSpinEdit2Change(TObject *Sender) { bit2 = CSpinEdit2->Value; } //--------------------------------------------------------------------------- void __fastcall TForm1::CSpinEdit3Change(TObject *Sender) { bit3 = CSpinEdit3->Value; } //--------------------------------------------------------------------------- void __fastcall TForm1::CSpinEdit4Change(TObject *Sender) { bit4 = CSpinEdit4->Value; for(int i=3; i<119; i++) BufferSendCom[i] = bit4; } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender) { // Form1->Memo1->Lines->Add("ff"); /* int e = ReadFile( Form1->hSerial, buf_in, 10 , &cb, NULL ); if(cb) { Form1->BufferSendCom[1] = Form1->bit1; Form1->BufferSendCom[2] = Form1->bit2; Form1->BufferSendCom[3] = Form1->bit3; int n_i = WriteFile( Form1->hSerial, &Form1->BufferSendCom, sizeof(Form1->BufferSendCom), &Form1->bytesWritten, NULL ); if( n_i == 1 ) Form1->Label1->Caption = "Write"; else Form1->Label1->Caption = "NoWrite"; } */ } //--------------------------------------------------------------------------- void __fastcall TForm1::Edit1KeyDown(TObject *Sender, WORD &Key, TShiftState Shift) { if(Key == 13) { buf_rez = Edit1->Text.ToInt();/* Memo1->Lines->Add(buf_rez);*/} } //--------------------------------------------------------------------------- void __fastcall TForm1::ComboBox2Click(TObject *Sender) { if( ComboBox2->Text == "110" ) BaudR = CBR_110; else if( ComboBox2->Text == "300" ) BaudR = CBR_300; else if( ComboBox2->Text == "600" ) BaudR = CBR_600; else if( ComboBox2->Text == "1200" ) BaudR = CBR_1200; else if( ComboBox2->Text == "2400" ) BaudR = CBR_2400; else if( ComboBox2->Text == "4800" ) BaudR = CBR_4800; else if( ComboBox2->Text == "9600" ) BaudR = CBR_9600; else if( ComboBox2->Text == "19200" ) BaudR = CBR_19200; else if( ComboBox2->Text == "38400" ) BaudR = CBR_38400; else if( ComboBox2->Text == "57500" ) BaudR = CBR_57600; else if( ComboBox2->Text == "115200" ) BaudR = CBR_115200; else if( ComboBox2->Text == "128000" ) BaudR = CBR_128000; else if( ComboBox2->Text == "256000" ) BaudR = CBR_256000; } //--------------------------------------------------------------------------- |
Сообщ.
#2
,
|
|
|
Цитата Acvarif @ что не врублюсь как принять Также как и в любом другом месте. Цитата Acvarif @ и отобразить Отсылай оконное сообщение gui-потоку. |
Сообщ.
#3
,
|
|
|
Как правильно будет выглядеть этот кусок приема
if(!NbRead) { ReadFile( Form1->hSerial, buf_in, Form1->buf_rez, &cb, NULL ); if(cb) { hexdumpstrRead(buf_in, Form1->buf_rez ); Form1->BufferSendCom[1] = Form1->bit1; Form1->BufferSendCom[2] = Form1->bit2; Form1->BufferSendCom[3] = Form1->bit3; int n_i = WriteFile( Form1->hSerial, &Form1->BufferSendCom, sizeof(Form1->BufferSendCom), &Form1->bytesWritten, NULL ); if( n_i == 1 ) Form1->Label1->Caption = "Write"; else Form1->Label1->Caption = "NoWrite"; hexdumpstrWrite(Form1->BufferSendCom,128); NbRead = true; } } if(NbRead) { ReadFile( Form1->hSerial, buf_in, buf_r, &rb, NULL ); if(rb) { hexdumpstrRead(buf_in, buf_r); NbRead = false; } } На деле отображается только первый пакет информации с buf_rez (4800 байт) Второй пакет (128 байт) который следует за ним через 0.2 сек не отображается (или не принимается) Может я делаю не так. Нужно в потоке одним ReadFile принять 4800 + 128 байт, а потом их распределить по своим Memo? |