c++ - setting up serial port and writing to following online tutorials -
i reading online tutorials on how send few bytes via com port. solved problems, stuck on:
writefile(hserial, bytes_to_send, 5, &bytes_written, null); fprintf(stderr, "%d bytes written\n", bytes_written); the code output: - opening serial port...ok - sending bytes...0 bytes written
i send bytes via com port uart on micro controller. code looks useful need do.
the code i'm using:
// // serial.c / serial.cpp // simple serial port writing example // written ted burke - last updated 13-2-2013 // // compile mingw: // // gcc -o serial.exe serial.c // // compile cl, microsoft compiler: // // cl serial.cpp // // run: // // serial.exe // #include <windows.h> #include <iostream> #include <stdio.h> using namespace std; int main() { // define 5 bytes send ("hello") char bytes_to_send[5]; bytes_to_send[0] = 104; bytes_to_send[1] = 101; bytes_to_send[2] = 108; bytes_to_send[3] = 108; bytes_to_send[4] = 111; // declare variables , structures handle hserial; dcb dcbserialparams = {0}; commtimeouts timeouts = {0}; tchar *pccommport = text("com1"); // open highest available serial port number fprintf(stderr, "opening serial port..."); //hserial = createfile( comportname, // generic_read | generic_write, // 0, // null, // open_existing, // file_attribute_normal, // null); hserial = createfile( pccommport, generic_read|generic_write, 0, null, open_existing, file_attribute_normal, null ); if (hserial == invalid_handle_value) { fprintf(stderr, "error\n"); return 1; } else fprintf(stderr, "ok\n"); // set device parameters (38400 baud, 1 start bit, // 1 stop bit, no parity) dcbserialparams.dcblength = sizeof(dcbserialparams); if (getcommstate(hserial, &dcbserialparams) == 0) { fprintf(stderr, "error getting device state\n"); closehandle(hserial); return 1; } dcbserialparams.baudrate = cbr_38400; dcbserialparams.bytesize = 8; dcbserialparams.stopbits = onestopbit; dcbserialparams.parity = noparity; if(setcommstate(hserial, &dcbserialparams) == 0) { fprintf(stderr, "error setting device parameters\n"); closehandle(hserial); return 1; } // set com port timeout settings timeouts.readintervaltimeout = 50; timeouts.readtotaltimeoutconstant = 50; timeouts.readtotaltimeoutmultiplier = 10; timeouts.writetotaltimeoutconstant = 50; timeouts.writetotaltimeoutmultiplier = 10; if(setcommtimeouts(hserial, &timeouts) == 0) { fprintf(stderr, "error setting timeouts\n"); closehandle(hserial); return 1; } // send specified text (remaining command line arguments) dword bytes_written, total_bytes_written = 0; fprintf(stderr, "sending bytes..."); if(!writefile(hserial, bytes_to_send, 5, &bytes_written, null)) { fprintf(stderr, "error\n"); closehandle(hserial); return 1; } fprintf(stderr, "%d bytes written\n", bytes_written); // close serial port fprintf(stderr, "closing serial port..."); if (closehandle(hserial) == 0) { fprintf(stderr, "error\n"); return 1; } fprintf(stderr, "ok\n"); // exit std::cin.get(); return 0; } further editing code using getlasterror() results in:
overlapped o; std::memset(&o, 0, sizeof(o)); o.offset = file_write_to_end_of_file; o.offsethigh = -1; if(!writefile(hserial, bytes_to_send, 5, &bytes_written, &o)) { //dword winapi getlasterror(void); //wprintf(l"format message failed 0x%x\n", getlasterror()); // 0x3e3 printf("error number: %ld\n", getlasterror()); 995 (0x3e3) // i/o operation has been aborted because of either thread exit or application request. fprintf(stderr, "error\n"); closehandle(hserial); return 1; } fprintf(stderr, "%d bytes written\n", bytes_written); hyperterminal, can detect app , want me close it.
regards , thank you.
Comments
Post a Comment