Can somebody getme a code to send and receive data to serial port? C++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can somebody getme a code to send and receive data to serial port? C++

I need a simple program on c++ i need send a bit and receive one. Is for connect a pc to microcontroler pic16f84a.

17th Feb 2020, 4:33 PM
Edwin Bg
Edwin Bg - avatar
10 Answers
+ 3
//Do you have your attempts?? please attach then
17th Feb 2020, 5:07 PM
Sudarshan Rai
Sudarshan Rai - avatar
+ 3
Yes, you can use that code for opening comport on Windows. but you must append a check of serialHandle for successful opening. Use WriteFile function to write number to comport and ReadFile to read number.
17th Feb 2020, 7:18 PM
andriy kan
andriy kan - avatar
+ 2
The code is platform specific. The general algorithm is as follows: 1. Get handle by Opening the com port as regular file using its OS-specific name (COM1 on windows or /dev/ttyS1 on linux...). 2. Setup com port (baudrate, parity...) by calling OS-specific API on the handle. 3. Read and write to comport handle using file read/write functions. 4. Close the handle. Usually work with comport is performed in a separate thread
17th Feb 2020, 6:04 PM
andriy kan
andriy kan - avatar
+ 2
A web search may give you a solution.
17th Feb 2020, 10:10 PM
Sonic
Sonic - avatar
0
No man
17th Feb 2020, 5:10 PM
Edwin Bg
Edwin Bg - avatar
0
I dont have then
17th Feb 2020, 5:10 PM
Edwin Bg
Edwin Bg - avatar
0
Could i use this code? #include <windows.h> /* ... */ // Open serial port HANDLE serialHandle; serialHandle = CreateFile("\\\\.\\COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); // Do some basic settings DCB serialParams = { 0 }; serialParams.DCBlength = sizeof(serialParams); GetCommState(serialHandle, &serialParams); serialParams.BaudRate = baudrate; serialParams.ByteSize = byteSize; serialParams.StopBits = stopBits; serialParams.Parity = parity; SetCommState(serialHandle, &serialParams);
17th Feb 2020, 6:28 PM
Edwin Bg
Edwin Bg - avatar
0
For example I need send number one and receive 2
17th Feb 2020, 6:29 PM
Edwin Bg
Edwin Bg - avatar
0
Helpme please
17th Feb 2020, 6:30 PM
Edwin Bg
Edwin Bg - avatar
0
I need to open the serial or com port and send a number one and then receive a number 2 from that same port
17th Feb 2020, 6:34 PM
Edwin Bg
Edwin Bg - avatar