The software that goes with my atari2600 cartreader only works with real com ports, but I want it to be able to work with usb to ttl converters too. Other software can use this usb to ttl converter with no problem. How do I send data to a virtual com port?
This is my source code:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <tchar.h>
#include <string.h>
#include <conio.h>
//Define all parameters
#define PORT_ argv[1]
#define MODE_ argv[2]
#define FILE_ argv[3]
#define AUTORUN_ argv[4]
typedef unsigned char BYTE;
//GLOBAL VARS:
FILE *fp;
//Delay function
void delay(unsigned int time)
{
long d;
while(time--)
for ( d = 0 ; d <= 400000 ; d++ )
{}
}
int sw(int addr,HANDLE comHandle){
BYTE config[] = {0,0,addr/256,addr%256};
DWORD btsIO;
static short buff[1] = {0};
WriteFile(comHandle,config,4,&btsIO,NULL);
ReadFile(comHandle,buff,2,&btsIO,NULL); //Wait for ACK
return 0;
}
int tranceive(int mode, int start, int end,HANDLE comHandle){
BYTE config[4];
DWORD btsIO;
static short buff[65535] = {0};
int address = start;
int size = end - start + 1;
int bytesLeft = size;
if (bytesLeft < 0) {return 1;}
printf("\n");
do{
config[0] = mode;
config[2] = address/256;
config[3] = address%256;
if(bytesLeft - 256 >= 0){
config[1] = 255;
bytesLeft -= 256;
}else{
config[1] = bytesLeft - 1;
bytesLeft = 0;
//printf("Oops there is an error!");
}
printf("%d bytes ready...\r",size - bytesLeft);
WriteFile(comHandle,config,4,&btsIO,NULL);
//while(btsIO!=1)
ReadFile(comHandle/*hDevice*/,buff,1,&btsIO,NULL); //Wait for ACK
switch(mode)
{
case 0:{
ReadFile(comHandle,buff,config[1]+1,&btsIO,NULL);
if(btsIO!=config[1]+1){
printf("Error:Read %d bytes over serial instead of %d.\n", btsIO,config[1]+1);
}
fwrite(buff,1,config[1]+1,fp);
break;
}
case 1:{
//getch();
fread(buff,1,config[1]+1,fp);
WriteFile(comHandle,buff,config[1]+1,&btsIO,NULL);
//while(btsIO!=1)
ReadFile(comHandle,buff,1,&btsIO,NULL); //Wait for ACK
break;
}
}
address += 256;
} while(bytesLeft);
printf("\n");
return 0;
}
//On error:Print usage of program.
void usage(char* name)
{
printf("Usage: romclient2 filename port r/w startaddr size\n");
}
////////////////////////////////////////////////////////////////////////////////
/////MAIN PROGRAM /////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
if (argc < 4)
{
usage(argv[0]);
return 0;
}
int address = 0;
int dataDirection = atoi(MODE_);
int switchMethod = 0;
int autorun = 0;
int fileSize;
HANDLE hDevice =
CreateFile(PORT_,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
0);
DCB lpTest;
// Setup Serial COM port
GetCommState(hDevice,&lpTest);
lpTest.BaudRate = CBR_115200;
lpTest.ByteSize = 8;
lpTest.Parity = NOPARITY;
lpTest.StopBits = ONESTOPBIT;
lpTest.fDtrControl = DTR_CONTROL_ENABLE;
// instance an object of COMMTIMEOUTS.
COMMTIMEOUTS comTimeOut;
// Specify time-out between charactor for receiving.
comTimeOut.ReadIntervalTimeout = 80;
// Specify value that is multiplied
// by the requested number of bytes to be read.
comTimeOut.ReadTotalTimeoutMultiplier = 30;
// Specify value is added to the product of the
// ReadTotalTimeoutMultiplier member
comTimeOut.ReadTotalTimeoutConstant = 20;
// Specify value that is multiplied
// by the requested number of bytes to be sent.
comTimeOut.WriteTotalTimeoutMultiplier = 3;
// Specify value is added to the product of the
// WriteTotalTimeoutMultiplier member
comTimeOut.WriteTotalTimeoutConstant = 2;
// set the time-out parameter into device control.
SetCommTimeouts(hDevice,&comTimeOut);
SetCommState(hDevice,&lpTest);
lpTest.fDtrControl = DTR_CONTROL_DISABLE;
Sleep(100);
SetCommState(hDevice,&lpTest);
Sleep(100);
if (argc >=5)
autorun = atoi(AUTORUN_);
system("cls");
printf(
"~~~~~~RomClient V2~~~~~~~~\n"
" by Koen van Vliet \n"
);
switch(MODE_[0]){
case '1':
case 'w':
case 'W':{
printf("MODE 1: WRITE to cartridge\n");
dataDirection = 1;
break;
}
default:
case '0':
case 'r':
case 'R':{
printf("MODE 0: READ from cartridge\n");
dataDirection = 0;
break;
}
}
//Get source/destination file handle + size
fp = fopen(FILE_,"r");
if (fclose(fp) != -1 && dataDirection == 0){
printf("Target file exists. Overwrite? Y/n\n");
switch (getch())
{
case 'y':
case 'Y':{
printf("Overwriting target file!\n");
break;
}
case 'n':
case 'N':{
printf("Quitting Program\n");
return 0;
break;
}
default: {
printf("That is not a valid option.\n Quitting Program\n");
return 0;
break;
}
}
};
if (hDevice == INVALID_HANDLE_VALUE)
{
printf("--Error: Could not open port %s.--\n", PORT_);
switch (GetLastError())
{
case ERROR_ACCESS_DENIED: {
printf("Port %s is used by another application.\n", PORT_);
break;
}
case ERROR_FILE_NOT_FOUND:{
printf("Port %s does not exist\n", PORT_);
break;
}
}
printf("----------------------------------\n");
return 0;
}
else
printf("Created handle %d for port %s.\n", hDevice, PORT_);
printf("Port %s opened.\n", PORT_);
if (dataDirection == 0){
remove(FILE_);
}
if (dataDirection == 0){
fp = fopen(FILE_, "ab");
}
else
{
fp = fopen(FILE_, "rb");
}
fseek(fp, 0, SEEK_END);
fileSize = ftell(fp);
printf("File Size = %d\n",fileSize);
fseek(fp, 0, SEEK_SET);
printf("*Bankswitching Method:");
switchMethod = 8;
switch(switchMethod){
case 8:{
printf("F8");
sw(0xFF8,hDevice);
tranceive(dataDirection,0x000,0xFF9,hDevice);
sw(0xFF8,hDevice);
tranceive(dataDirection,0xFFA,0xFFF,hDevice);
sw(0xFF9,hDevice);
if (fileSize == 4096){
fseek(fp,0,SEEK_SET); //Jump back the the beginning of the file.
}
tranceive(dataDirection,0x000,0xFFF,hDevice);
break;
}
default:{
printf("-");
tranceive(dataDirection,0x0000,0x0FFF,hDevice);
}
}
if (fclose(fp))
{
printf("Error:Closed file with errors.\n");
}
CloseHandle(hDevice);
return 0;
}