Created
July 6, 2011 12:57
-
-
Save shahpoojan/1067164 to your computer and use it in GitHub Desktop.
A code to send data on the USB port for Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <Windows.h> | |
#include <tchar.h> | |
#include <stdio.h> | |
#include <Winbase.h> | |
HANDLE hCom; | |
DWORD sendData (const char* data, DWORD size) | |
{ | |
DWORD numberOfBytesWritten; | |
WriteFile(hCom, | |
data, | |
size, | |
&numberOfBytesWritten, | |
0); | |
return numberOfBytesWritten; | |
} | |
void PrintCommState(DCB dcb) | |
{ | |
// Print some of the DCB structure values | |
_tprintf( TEXT("\nBaudRate = %d, ByteSize = %d, Parity = %d, StopBits = %d\n"), | |
dcb.BaudRate, | |
dcb.ByteSize, | |
dcb.Parity, | |
dcb.StopBits ); | |
} | |
int init_usb(){ | |
DCB dcb; | |
BOOL fSuccess; | |
TCHAR *pcCommPort = TEXT("\\\\.\\COM28"); // For COM Ports gretaer than 9 | |
//TCHAR *pcCommPort = TEXT("COM3"); // For COM Prts less than or equal to 9 | |
//Open a handle to the specified com port. | |
hCom = CreateFile( pcCommPort, | |
GENERIC_READ | GENERIC_WRITE, | |
0, // must be opened with exclusive-access | |
NULL, // default security attributes | |
OPEN_EXISTING, // must use OPEN_EXISTING | |
0, // not overlapped I/O | |
NULL ); // hTemplate must be NULL for comm devices | |
if (hCom == INVALID_HANDLE_VALUE) | |
{ | |
// Handle the error. | |
printf ("CreateFile failed with error %d.\n", GetLastError()); | |
return (1); | |
} | |
// Initialize the DCB structure. | |
ZeroMemory(&dcb, sizeof(DCB)); | |
dcb.DCBlength = sizeof(DCB); | |
// Build on the current configuration by first retrieving all current | |
// settings. | |
fSuccess = GetCommState(hCom, &dcb); | |
if (!fSuccess) | |
{ | |
// Handle the error. | |
printf ("GetCommState failed with error %d.\n", GetLastError()); | |
return (2); | |
} | |
PrintCommState(dcb); // Output to console the state of the COM Port before changing | |
// Fill in some DCB values and set the com state: | |
dcb.BaudRate = CBR_38400; // baud rate | |
dcb.ByteSize = 8; // data size, xmit and rcv | |
dcb.Parity = NOPARITY; // parity bit | |
dcb.StopBits = ONESTOPBIT; // stop bit | |
fSuccess = SetCommState(hCom, &dcb); | |
if (!fSuccess) | |
{ | |
// Handle the error. | |
printf ("SetCommState failed with error %d.\n", GetLastError()); | |
return (3); | |
} | |
// Get the comm config again. | |
fSuccess = GetCommState(hCom, &dcb); | |
if (!fSuccess) | |
{ | |
// Handle the error. | |
printf ("GetCommState failed with error %d.\n", GetLastError()); | |
return (2); | |
} | |
PrintCommState(dcb); // Output to console the state of the COM Port after making the desired changes to the COM Port | |
_tprintf (TEXT("Serial port %s successfully reconfigured.\n"), pcCommPort); | |
} | |
int main(int argc, char** argv) { | |
init_usb(); // initialize the usb | |
char *ch = "MD2X1Y4N"; | |
int size = sendData(ch , sizeof("MD2X1Y4N") ); | |
if(size != sizeof("MD2X1Y4N")) printf("Error in sending\n"); | |
else printf("%d sent .Sent succeeded\n",size); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment