C++ 2021 RAD XE7 COM PORT not sending HEX values - Page 1
Forum section: electronics / general

author: karadev
author_rank: Newbie
timestamp: February 21, 2021, 11:18:59 am
content: i try to make a simple program in C++ EMBARCADERO XE7 for sending a one or more HEX byte/s to my PCB BOARD with pic micro controller 18F47K42. COM port is open fine but no success with sending right data. thanks in forward for help. i read many of posts here but no success from 3 days trying to make it right. here is some code, or i can send/upload to someone to say what is need to do, best i can leard from example. best regards to allHERE IS CODE:void __fastcall TForm1::Button3Click(TObject *Sender){ bool portstatus;bool fSuccess;unsigned int TxBufer = 64;unsigned int KolByte = 1;unsigned long dwSize;unsigned char null_Byte = 0x00;DCB dcb;OVERLAPPED COMWrite = {0};COMWrite.Offset = 0;// Fill in some DCB values and set the com state:// 57,600 bps, 8 data bits, no parity, and 1 stop bit.dcb.BaudRate = CBR_9600; // baud ratedcb.ByteSize = 8; // data size, xmit and rcvdcb.Parity = NOPARITY; // parity bitdcb.StopBits = ONESTOPBIT; // stop bit//dcb.fBinary = 1;COMWrite.hEvent = CreateEvent(NULL,FALSE,FALSE,NULL);assert(COMWrite.hEvent);FlushFileBuffers(port);// Panel1->Caption = 255;port = CreateFile(TEXT("COM3"), GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);//WriteFile(port, &null_Byte , 1, &dwSize, NULL); //&send_byte//unsigned char dst = {0};///unsigned long size = sizeof(dst);//if(port!= INVALID_HANDLE_VALUE)// if(ReadFile(port,dst,size, &size,0))// Panel1->Caption = 100;fSuccess = SetCommState(hCom, &dcb);if(!fSuccess){Panel1->Caption = "PORT OK";}else{Panel1->Caption = "NO PORT";}portstatus = GetCommState(port,&dcb);if(!portstatus){Panel1->Caption = "NO PORT";}// TxBufer++;}//---------------------------------------------------------------------------void __fastcall TForm1::Button4Click(TObject *Sender){ unsigned long dwSize;unsigned char send_byte_aray;int pointer_send_byte = 0;int send_byte = 0;unsigned char send_byte1 = 0;unsigned char send_end = 0;// for(unsigned char m=0;m>4;m++)//{send_byte_aray = StrToInt(Edit4->Text.Length()); }// send_byte = StrToInt(Edit4->Text.Length());// IntToHex(Byte(Edit4->Text.Length()),2);// IntToHex(Byte(Edit4->Text),2);// IntToHex(Byte(Edit4->Text),1);//IntToHex(Byte(Edit4->Text),1);send_byte = Edit4->Text.ToInt();for (int i = 1; i <= Edit4->Text.Length(); i++){Label6->Caption = Label6->Caption +IntToHex(Byte(Edit4->Text),2) + ' ';//WriteFile(port, &send_byte , 2, &dwSize, NULL);}//send_byte1 = Text.IntToHex(Edit4->Text.ToInt(),2);send_byte1 = '5C';WriteFile(port, '$'+ &Text , 2, &dwSize, NULL);// WriteFile(port, '$'+&send_byte , 1, &dwSize, NULL);//send_byte = StrToInt(Edit4->Text.IntToHex(send_byte,2));Memo1->Lines->Add(send_byte);// WriteFile(port, &send_byte , 2, &dwSize, NULL); //&send_byte//WriteFile(port, &send_byte1 , 1, &dwSize, NULL); //&send_byte//WriteFile(port, &send_end , 1, &dwSize, NULL); //&send_byte/*WriteFile(port, &send_byte_aray , 1, &dwSize, NULL); //&send_byteWriteFile(port, &send_byte_aray , 1, &dwSize, NULL); //&send_byteWriteFile(port, &send_byte_aray , 1, &dwSize, NULL); //&send_byteWriteFile(port, &send_byte_aray , 1, &dwSize, NULL); //&send_byte//WriteFile(port, &send_end , 1, &dwSize, NULL); //&send_byte//WriteFile(port, &send_end, 1, &dwSize, NULL);Memo1->Lines->Add(send_byte);Memo1->Lines->Add(send_byte);Memo1->Lines->Add(send_byte);Memo1->Lines->Add(send_byte);*/// Memo1->Lines->Add(IntToHex(Byte(Edit4->Text),1));//Memo1->Lines->Add(IntToHex(Byte(Edit4->Text),1));//Memo1->Lines->Add(IntToHex(Byte(Edit4->Text),1));}

author: PlainName
author_rank: Super Contributor
timestamp: February 21, 2021, 02:29:07 pm
content: Don't know the answer you seek, sorry, but if I may make two suggestions...1. It is bad form to have functional code in a form event function. Essentially, you've written it so that clicking Button3 is the only way to perform this function. Instead, the click event should call a separate function that does the actual deed - then you could call it from anywhere for any reason (even if you don't have a GUI).2. You may want to consider that some people that could answer your query won't because the bright blue is just too tiring on the eyes to see what it says.

author: ebclr
author_rank: Super Contributor
timestamp: February 23, 2021, 07:41:30 am
content: This will make you project easierhttps://winsoft.sk/protsuite.htm

author: ace1903
author_rank: Regular Contributor
timestamp: February 23, 2021, 10:07:14 am
content: When asking for help always provide clean and formated code. No one can see what the problem is if you add million commented lines with // but also with /* */Tried to clean the code and found only one relevant line to your issue:WriteFile(port, '$'+ &Text , 2, &dwSize, NULL);Now, key information is missing.How Text is defined?In C and C++ depending on how you defined Text you will get different results of operation '$'+ &Text.But most of the time it will not be string append like in Python or some web languages.So, if you are getting different characters and garbage you need to fix that line.C++ compiler warned you about that line but you decided to ignore that warning.How it should look like depends on what you are trying to achieve with that line.

author: Psi
author_rank: Super Contributor
timestamp: February 23, 2021, 10:37:18 am
content: Issues that caught me out when doing comms stuff and win apps.- Unicode characters being used and multiple bytes being sent instead of 1- Comm buffers not sending until they are full or until you manually make a call to flush/send them.- Not being aware of inverted signal between RS232 and 5V serial logic- Age-old wiring issue of Tx going to Tx when it should go to RxI recommend getting the app working with a logic analyzer on the output instead of your device.If you don't have one you can short Tx and Rx and then confirm you can send and receive the same info.There are also serial spy tools you can use to watch the Tx and Rx byte streams of an active serial port in windows.