: : Hi
: : I hope you can follow this, the text tends to line return makign the code hard to follow.
:
:
Use code tags.
:
: [code]
: printf("Hello world");
: [/code]
:
:
: : Basicly. i
: : initialize 9x unsigned chars in array.
: : Calcultate the checksum
: : TX 9x bytes using writefile, thia takes aprox 10msec
: : sleep 35msec
: : purge com port 2
: : loop back until 8000 loops done.
: :
:
:
:
: : DWORD dwNumBytesWritten;
: :
: : unsigned char PROGRAMMERMID = 182;
: : unsigned char ESCAPEPID = 254;
: : unsigned char GAUGEMID = 41;
: : unsigned char CONFIGWRITEPID = 192;
: : unsigned char NUMBERDATABYTES = 3;
: : unsigned char EEPROM_ADDRESS_LSB = 0;
: : unsigned char EEPROM_ADDRESS_MSB = 0;
: : unsigned char EEPROM_DATA = 0;
: : unsigned char CHECKSUM = 0;
: :
: : //unsigned char TX_string[10]; //hold data to be txed.
: : TX_string[0] = PROGRAMMERMID;
: : TX_string[1] = ESCAPEPID;
: : TX_string[2] = GAUGEMID;
: : TX_string[3] = CONFIGWRITEPID;
: : TX_string[4] = NUMBERDATABYTES;
: : TX_string[5] = EEPROM_ADDRESS_LSB;
: : TX_string[6] = EEPROM_ADDRESS_MSB;
: : TX_string[7] = EEPROM_DATA;
: : TX_string[8] = CHECKSUM;
: :
: :
: :
: : for(i = 0; i < 8191; i++) //loop and write all bytes to the eeprom
: : {
: :
: : EEPROM_DATA = 0;
: : TX_string[7] = EEPROM_DATA;
: :
: :
: :
: :
: : //Calculate the checksum
: : CHECKSUM = (-1 -(PROGRAMMERMID + ESCAPEPID + GAUGEMID + CONFIGWRITEPID + NUMBERDATABYTES + EEPROM_ADDRESS_LSB + EEPROM_ADDRESS_MSB + EEPROM_DATA)) +1;
: : TX_string[8] = CHECKSUM;
: :
: :
: :
: :
: : //write 9 bytes to COM 2 @9600bd
: : result = WriteFile(hCom_eeprom_write, TX_string, 9, &dwNumBytesWritten, NULL);
: : if( (!result) || (dwNumBytesWritten != 9) )
: : {
: : //WriteFile fialed.
: : MessageBox(NULL, "WriteFile failed\nTX to eeprom ERROR", "Error .EC1033: command_write_16bit_EEPROM()", MB_OK);
: : }
: :
: :
: : Sleep(35);
: :
: :
: :
: : //Clear the RS232 TX buffer and report any errors
: : if(!PurgeComm(hCom_eeprom_write,PURGE_TXCLEAR)) //clears rx buffer. Purgcom can also clear tx buff etc.
: : {
: : CHAR szBuf[80];
: : LPSTR lpszFunction;
: : DWORD dw = GetLastError();
: :
: : sprintf(szBuf, "%s PurgeComm hCom_2 TX Failed at 1001: GetLastError returned %u\n", lpszFunction, dw);
: : MessageBox(NULL, szBuf, "Error .EC0033", MB_OK);
: : Error_Code_Logger(szBuf);
: : ExitProcess(dw);
: : //MessageBox(hwnd, "PurgeComm TX Failed", "Test Mode", MB_OK);
: : }
: :
: :
: : //Clear the RS232 TRX buffer and report any errors
: : if(!PurgeComm(hCom_eeprom_write,PURGE_RXCLEAR)) //clears rx buffer. Purgcom can also clear tx buff etc.
: : {
: : CHAR szBuf[80];
: : LPSTR lpszFunction;
: : DWORD dw = GetLastError();
: :
: : sprintf(szBuf, "%s PurgeComm hCom_2 RX Failed: GetLastError returned %u\n", lpszFunction, dw);
: : MessageBox(NULL, szBuf, "Error .EC0034", MB_OK);
: : Error_Code_Logger(szBuf);
: : ExitProcess(dw);
: : //MessageBox(hwnd, "PurgeComm TX Failed", "Test Mode", MB_OK);
: : }
: :
: :
: :
: :
: : } //close... for(i = 0; i 8121; i++) //loop and write all bytes to the eeprom
: :
: :
: :
:
:
:
:
:
: I can't see any bugs in the code, but since you say that the PC freezes... do you get the same behaviour on another PC?
:
: Also, there might be problems in the code that you didn't post, that is opening the com port. I know that using WriteFile() on an invalid HANDLE can give you strange errors.
:
:
Thanks
here is the com2 set up code i use. I have tried changing the times out to 1000 and the inQueue and outQueue to 200,000. It still freezes.
I havent tried it on another pc, i wish to have it work on all pcs.
So far, all i can do is write up to 1000 packets (9 bytes each pkt) reliably. I am considering just calling the writefile loop 8x to get the 8000 packets (80KByte)i require.
void set_com_port_2(void)
{
/* set up a serial port. */
DCB dcb;
//HANDLE hCom_2; is global scope
BOOL fSuccess;
char pcCommPort[6] = "COM2"; //DEFAULT SETTING
char a = 0x00;
DWORD dwInQueue = 2000; //was 2000
DWORD dwOutQueue = 2000; //was 2000
hCom_2 = CreateFile( pcCommPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING, // must use OPEN_EXISTING
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom_2 == INVALID_HANDLE_VALUE)
{
CHAR szBuf[80];
LPSTR lpszFunction;
DWORD dw = GetLastError();
sprintf(szBuf, "%x CreateFile failed Failed COM2: GetLastError returned %u\n", lpszFunction, dw);
MessageBox(NULL, szBuf, "Error. EC0016", MB_OK);
ExitProcess(dw);
}
//set up the RX and TX buffer size
fSuccess = SetupComm(hCom_2, dwInQueue, dwOutQueue);
if (!fSuccess)
{
CHAR szBuf[80];
LPSTR lpszFunction;
DWORD dw = GetLastError();
sprintf(szBuf, "%s SetupComm failed COM2: GetLastError returned %u\n", lpszFunction, dw);
MessageBox(NULL, szBuf, "Error. EC0017", MB_OK);
ExitProcess(dw);
}
// Build on the current configuration, and skip setting the size
// of the input and output buffers with SetupComm.
fSuccess = GetCommState(hCom_2, &dcb);
if (!fSuccess)
{
CHAR szBuf[80];
LPSTR lpszFunction;
DWORD dw = GetLastError();
sprintf(szBuf, "%s GetCommState failed COM2: GetLastError returned %u\n", lpszFunction, dw);
MessageBox(NULL, szBuf, "Error, EC0018", MB_OK);
ExitProcess(dw);
}
// Fill in DCB: 9,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR_9600; //CBR_9600; //CBR_9600; // set the baud rate
dcb.ByteSize = 8; // data size, xmit, and rcv
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
dcb.fBinary = TRUE;
//dcb.fParity = TRUE;
fSuccess = SetCommState(hCom_2, &dcb);
if (!fSuccess)
{
CHAR szBuf[80];
LPSTR lpszFunction;
DWORD dw = GetLastError();
sprintf(szBuf, "%s SetCommState failed COM2: GetLastError returned %u\n", lpszFunction, dw);
MessageBox(NULL, szBuf, "Error EC0019", MB_OK);
ExitProcess(dw);
}
//*********reconfigur com1 time outs for shorter time out period***********//
//set up time outs for com port.
//shorten time outs to prevent pc hanging up waiting for a message from gauge that will never come.
//rx will time out in 1 millisecond
GetCommTimeouts(hCom_2, &cto);
cto.ReadIntervalTimeout= 0; //was 0
cto.ReadTotalTimeoutMultiplier= 0; //was 0.
cto.ReadTotalTimeoutConstant= 1; //was 1
cto.WriteTotalTimeoutMultiplier= 0; //was 0
cto.WriteTotalTimeoutConstant= 0; //was 0
SetCommTimeouts(hCom_2, &cto);
//CloseHandle(hCom);
//Clear the RS232 TX buffer and report any errors
if(!PurgeComm(hCom_2,PURGE_TXCLEAR)) //clears rx buffer. Purgcom can also clear tx buff etc.
{
CHAR szBuf[80];
LPSTR lpszFunction;
DWORD dw = GetLastError();
sprintf(szBuf, "%s PurgeComm TX Failed: GetLastError returned %u\nSet Com Port 2", lpszFunction, dw);
MessageBox(NULL, szBuf, "Error EC0020", MB_OK);
ExitProcess(dw);
}
//Clear the RS232 TRX buffer and report any errors
if(!PurgeComm(hCom_2,PURGE_RXCLEAR)) //clears rx buffer. Purgcom can also clear tx buff etc.
{
CHAR szBuf[80];
LPSTR lpszFunction;
DWORD dw = GetLastError();
sprintf(szBuf, "%s PurgeComm RX Failed: GetLastError returned %u\n", lpszFunction, dw);
MessageBox(NULL, szBuf, "Error. EC0021", MB_OK);
ExitProcess(dw);
}
return;
}
Any thoughts?
Thanks for all you help.
cheers
kev