Hi there i have a big problem i am tring to send asski code out via the serial port. what my code does is look through a saved file and picks out text information then sends it to the serial port. My problem is a wierd one!! it works ok after i open up hyperterminal and write to the lcd but it will not work if i just run the code sraight from visual C++ it outputs a lot of strange charectures im stuck any help will be great my code is attached. I am running windows xp and my compiller is visual C++ cheers
ontrix 0 Newbie Poster
//print file on screen
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRICT
#include <tchar.h>
#include <windows.h>
#define THREADCOUNT 4
DWORD dwtlsIndex;
VOID ErrorExit (LPTSTR);
VOID commonFunc (VOID);
//write to the port
BOOL WriteABuffer(HANDLE hComm, char * lpBuf, DWORD dwToWrite)
{
OVERLAPPED osWrite = {0};
DWORD dwWritten;
BOOL fRes;
// Create this writes OVERLAPPED structure hEvent.
osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (osWrite.hEvent == NULL)
// Error creating overlapped event handle.
return FALSE;
// Issue write.
if (!WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite))
{
if (GetLastError() != ERROR_IO_PENDING) {
// WriteFile failed, but it isn't delayed. Report error and abort.
fRes = FALSE;
}
else {
// Write is pending.
if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, TRUE))
fRes = FALSE;
else
// Write operation completed successfully.
fRes = TRUE;
}
Sleep (100);
}
else
// WriteFile completed immediately.
fRes = TRUE;
CloseHandle(osWrite.hEvent);
return fRes;
}
int main () {
FILE * pFile;
long lSize;
char * buffer;
pFile = fopen ( "LCD.aup" , "rb" );
if (pFile==NULL) exit (1);
// obtain file size.
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
// allocate memory to contain the whole file.
buffer = (char*) malloc (lSize);
if (buffer == NULL) exit (2);
// copy the file into the buffer.
fread (buffer,1,lSize,pFile);
char *pdest = NULL;
char *tmp;
char Trackname[80];
char *wavetrackname;
tmp = buffer;
memset(&Trackname[0],32,80);
// open the port
char gszPort[18];
memset(&gszPort[0], 32,18);
HANDLE hComm;
strcpy(gszPort, "COM1");
hComm = CreateFile( gszPort,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if (hComm == INVALID_HANDLE_VALUE)
{
// error opening port; abort
}
// find wavetrack name in file
do
{
pdest = strstr( tmp, "wavetrack name" );
if(pdest != NULL)
{
wavetrackname = pdest + 16;
tmp = pdest + 1;
// find file name up to"
int pos;
pos = strcspn( wavetrackname, "\"" );
//copy track name to new string
strncpy( Trackname, wavetrackname, pos );
char *ttmp = &Trackname[0];
strncpy( ttmp + pos, "\0", 1 );
strncpy( ttmp + pos + 1, " ", 18);
WriteABuffer(hComm, ttmp, 18);
//print results on different lines
// printf("\n\n\n");
// printf(Trackname);
// printf("\n");
}
}
while(pdest != NULL);
// terminate
fclose (pFile);
free (buffer);
int c = getchar();
return 0;
}
Ancient Dragon 5,243 Achieved Level 70 Team Colleague Featured Poster
you probably need to set the baud rate, stop bits, data bits, and parity. See MSDN about how to configure the port.
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.