Jump to content
  • Advertisement
Sign in to follow this  
mrmrcoleman

RS 232 programming?

This topic is 4845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have been working on a vending machine application for the last 18 months and it is now finished although I have a few loose ends to tie up. The biggest of these is that I now have to interface with a coin acceptor through an RS232 port(via a USB->Serial card). I have never done any serial port programming before and I don't really know what I am doing. Here is all I know. 1. The coin acceptor has a 17 pin connector on, of which only 16 are present. 2. This connects to the USB->Serial controller that I have which is installed on my machine. I need two things really, 1. What do I need to know from the manfacturer about the various pins? 2. Could anybody point me in the direction of a good turorial, or just give me a brief overview to get me started? Thanks in advance for any help. Mark

Share this post


Link to post
Share on other sites
Advertisement
If the coin acceptor is a peripheral device then you will need to know what information it sends back to, you will need to know the interface. If it is simple enough then you could probably just open a port and just sit back waiting for data to come in.

This is some code i wrote for an assignment at uni, it is a chat client that sits on a RS232 COM serial port. If you can rip it apart you will see how com operations works.


#include <stdio.h>
#include <sys/types.h>
#include <conio.h>
#include <ctype.h>
#include <windows.h>
#include <winbase.h>
char LocalUserID;

char OnlineUserArray[26];

// Some global flags to keep track of various non-state operations

bool PacketToSend = false;
bool LocalUserLoggedIn = false;
bool SendLoggedInMessage = false;
bool SendLogInAcknowledgement = false;
bool SendLoggedOutMessage = false;
bool SendDataMessage = false;
bool SendMessageAcknowledgement = false;
bool SendGenericPacket = false;
bool DisplayFullPacketContents = false;
bool ExtendedMessaging = false;

HANDLE hComPort;
DWORD dwError;

char RecipientOfAck;
char RecipientOfNextMsg;
char MessageToSendArray[10];
// A buffer of 10 DPacket copies
char LastDPacketSent[10][16];



// Define some global states and a state tracking variable
//KeyBoard States
#define KEYLOGGEDIN 1
#define KEYLOGGEDOUT 2

//Transmit States
#define TRANSMITTING 1
#define NOTTRANSMITTING 2

//Recieve States
#define RECIEVING 1
#define NOTRECIEVING 2

int KeyBoardStateTracker = KEYLOGGEDOUT;
int TransmitStateTracker = TRANSMITTING;
int RecieveStateTracker = RECIEVING;




bool DrawOnMenu = true;
bool DrawOffMenu = true;

char InPacket[16];
char OutPacket[16];
int iPacketArrayMember;


int InitiatePort(char *device)
{
COMMTIMEOUTS noblock;
DCB dcb;
int fSuccess;

hComPort=CreateFile(device,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);

if (hComPort == INVALID_HANDLE_VALUE)
{
dwError = GetLastError();
printf("INVALID_HANDLE_VALUE()");
}

fSuccess = GetCommTimeouts(hComPort, &noblock);
noblock.ReadTotalTimeoutConstant = 1;
noblock.ReadTotalTimeoutMultiplier = MAXDWORD;
noblock.ReadIntervalTimeout = MAXDWORD;
fSuccess=SetCommTimeouts(hComPort,&noblock);

fSuccess = GetCommState(hComPort, &dcb);
if(!fSuccess)
{
printf("GetCommState Error!\n");
}

dcb.BaudRate = 9600;
dcb.ByteSize = 7;
dcb.fParity = TRUE;
dcb.Parity = EVENPARITY;
dcb.StopBits = TWOSTOPBITS;
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
dcb.fOutxCtsFlow = TRUE;
fSuccess = SetCommState(hComPort, &dcb);

if(!fSuccess)
{
printf("SetCommState Error!\n");
}
return 1;
}

int ClearPacketArrays()
{
for (int count = 0; count < 17; count++)
{
InPacket[count] = ' ';
OutPacket[count] = ' ';
}
return 1;
}

int ReadCom()
{
char item;
unsigned long ni;
BOOL fsuccess;
fsuccess = ReadFile(hComPort,&item,1,&ni,NULL);
if (ni == 1) return item;
else return 0;
}

void DisplayMessageFromPacket()
{
printf("\n%c Says : ", InPacket[2]);
if (DisplayFullPacketContents == true)
{
for (int i = 0; i < 16; i++)
{
printf("%c", InPacket);
}
}
else
for (int i = 4; i < 14; i++)
{
printf("%c", InPacket);
}

printf("\n");
}

void DisassemblePacket()
{
if ( InPacket[3] == 'L')
{
if (LocalUserLoggedIn == true)
{
if (ExtendedMessaging == true)
DisplayMessageFromPacket();

if (InPacket[1] == LocalUserID)
{
}
else
{
int converted;
// convert to ascii value
converted = (int) toupper(InPacket[1]);
// Fill user log in table for appropriate user
OnlineUserArray[(converted-65)] = InPacket[1];

SendLogInAcknowledgement = true;
}
}
}
else if( InPacket[3] == 'R')
{
if (LocalUserLoggedIn == true)
{
if (ExtendedMessaging == true)
DisplayMessageFromPacket();
if (InPacket[1] == LocalUserID)
{
}
else
{
int converted;
// convert to ascii value
converted = (int) toupper(InPacket[1]);
// Fill user log in table for appropriate user
OnlineUserArray[(converted-65)] = InPacket[2];

}
}

}
else if( InPacket[3] == 'X')
{
if (LocalUserLoggedIn == true)
{
if (ExtendedMessaging == true)
DisplayMessageFromPacket();
if (InPacket[1] != LocalUserID)
{
int converted;
// convert to ascii value
converted = (int) toupper(InPacket[1]);
// Fill user log in table for appropriate user
OnlineUserArray[(converted-65)] = '_';
}
}

}
else if( InPacket[3] == 'D')
{
if (LocalUserLoggedIn == true)
{
// Check to see if i'm the recipient
if (InPacket[1] == LocalUserID)
{
// Data payload packet ie a message to be displayed to the screen
// Then send an acknowledgement packet to the author
DisplayMessageFromPacket();
// Set the global variable that will hold the recipient of the acknowledgemnt packet
RecipientOfAck = InPacket[2];
SendMessageAcknowledgement = true;
}
else
{
// Construct a copy of the recieved packet to forward on
OutPacket[0] = '{';
OutPacket[1] = InPacket[1];
OutPacket[2] = InPacket[2];
OutPacket[3] = InPacket[3];
OutPacket[4] = InPacket[4];
OutPacket[5] = InPacket[5];
OutPacket[6] = InPacket[6];
OutPacket[7] = InPacket[7];
OutPacket[8] = InPacket[8];
OutPacket[9] = InPacket[9];
OutPacket[10] = InPacket[10];
OutPacket[11] = InPacket[11];
OutPacket[12] = InPacket[12];
OutPacket[13] = InPacket[13];
OutPacket[14] = InPacket[14];
OutPacket[15] = '}';
// Tell the transmitting loop to send it on
SendGenericPacket = true;
}
}
}
}

bool DealWithRecievedCharacter(char iInCharacter, bool *RunFlag)
{
if (iInCharacter != 0)
{
if (iInCharacter == '{')
{
iPacketArrayMember = 0;
InPacket[iPacketArrayMember] = iInCharacter;
iPacketArrayMember++;
}
else
{
InPacket[iPacketArrayMember] = iInCharacter;
iPacketArrayMember++;
if (iInCharacter == '}')
return true;
}

}
else
{
}
return false;
}



int CompilePacketToSend(char PacketType)
{
if (PacketType == 'L')
{
OutPacket[0] = '{';
OutPacket[1] = LocalUserID;
OutPacket[2] = LocalUserID;
OutPacket[3] = PacketType;
OutPacket[4] = '*';
OutPacket[5] = 'O';
OutPacket[6] = 'n';
OutPacket[7] = 'l';
OutPacket[8] = 'i';
OutPacket[9] = 'n';
OutPacket[10] = 'e';
OutPacket[11] = ' ';
OutPacket[12] = ' ';
OutPacket[13] = ' ';
OutPacket[14] = '#';
OutPacket[15] = '}';
}
else if (PacketType == 'X')
{
OutPacket[0] = '{';
OutPacket[1] = LocalUserID;
OutPacket[2] = LocalUserID;
OutPacket[3] = PacketType;
OutPacket[4] = '*';
OutPacket[5] = 'O';
OutPacket[6] = 'f';
OutPacket[7] = 'f';
OutPacket[8] = 'l';
OutPacket[9] = 'i';
OutPacket[10] = 'n';
OutPacket[11] = 'e';
OutPacket[12] = ' ';
OutPacket[13] = ' ';
OutPacket[14] = '#';
OutPacket[15] = '}';
}
else if (PacketType == 'R')
{
OutPacket[0] = '{';
OutPacket[1] = RecipientOfNextMsg;
OutPacket[2] = LocalUserID;
OutPacket[3] = PacketType;
OutPacket[4] = 'L';
OutPacket[5] = 'o';
OutPacket[6] = 'g';
OutPacket[7] = 'i';
OutPacket[8] = 'n';
OutPacket[9] = ' ';
OutPacket[10] = 'A';
OutPacket[11] = 'c';
OutPacket[12] = 'k';
OutPacket[13] = ' ';
OutPacket[14] = '#';
OutPacket[15] = '}';
}
else if (PacketType == 'D')
{
OutPacket[0] = '{';
OutPacket[1] = RecipientOfNextMsg;
OutPacket[2] = LocalUserID;
OutPacket[3] = PacketType;
OutPacket[4] = MessageToSendArray[0];
OutPacket[5] = MessageToSendArray[1];
OutPacket[6] = MessageToSendArray[2];
OutPacket[7] = MessageToSendArray[3];
OutPacket[8] = MessageToSendArray[4];
OutPacket[9] = MessageToSendArray[5];
OutPacket[10] = MessageToSendArray[6];
OutPacket[11] = MessageToSendArray[7];
OutPacket[12] = MessageToSendArray[8];
OutPacket[13] = MessageToSendArray[9];
OutPacket[14] = '#';
OutPacket[15] = '}';

}
else if (PacketType == 'Y')
{
OutPacket[0] = '{';
OutPacket[1] = InPacket[2];
OutPacket[2] = LocalUserID;
OutPacket[3] = PacketType;
OutPacket[4] = '+';
OutPacket[5] = ' ';
OutPacket[6] = 'A';
OutPacket[7] = 'C';
OutPacket[8] = 'K';
OutPacket[9] = ' ';
OutPacket[10] = ' ';
OutPacket[11] = ' ';
OutPacket[12] = ' ';
OutPacket[13] = ' ';
OutPacket[14] = '#';
OutPacket[15] = '}';
}
else if (PacketType == 'N')
{
OutPacket[0] = '{';
OutPacket[1] = RecipientOfNextMsg;
OutPacket[2] = LocalUserID;
OutPacket[3] = PacketType;
OutPacket[4] = '-';
OutPacket[5] = ' ';
OutPacket[6] = 'A';
OutPacket[7] = 'C';
OutPacket[8] = 'K';
OutPacket[9] = ' ';
OutPacket[10] = ' ';
OutPacket[11] = ' ';
OutPacket[12] = ' ';
OutPacket[13] = ' ';
OutPacket[14] = '#';
OutPacket[15] = '}';
}

return 1;
}

int SendPacket()
{
unsigned long ni;
BOOL fSuccess;

fSuccess = WriteFile(hComPort,OutPacket,16,&ni,NULL);

if (ni == 16 )
return 0;
else
return 1;
}
void LogIn()
{
}

void LogOut(bool *pRunFlag)
{
int converted;
converted = (int) LocalUserID;
OnlineUserArray[(converted-65)] = 1;
LocalUserID = ' ';

PacketToSend = false;
LocalUserLoggedIn = false;
SendLoggedInMessage = true;
SendLogInAcknowledgement = false;

KeyBoardStateTracker = KEYLOGGEDOUT;
}

//void GeneralInitiation

void menuLoggedOut()
{
system("cls");
printf("\t=======================================\n");
printf("\t====== Not Logged In ======\n");
printf("\t=======================================\n");
printf("\t=======================================\n");
printf("\t 1. Log In\n");
printf("\n");
printf("\t 2. Exit\n");
printf("\n");
printf("\n");
printf("\t Enter Choice: ");
}

void menuLoggedIn()
{
system("cls");
printf("\t=======================================\n");
printf("\t====== Logged in as %c ======\n", LocalUserID);
printf("\t=======================================\n");
printf("\t=======================================\n");
printf("\t 1. Log Out\n");
printf("\n");
printf("\t 2. SendMessage\n");
printf("\n");
printf("\t 3. View Online Contacts\n");
printf("\n");
printf("\t 4. Display Full Packet Contents -");

if (DisplayFullPacketContents == true)
{
printf(" ON\n");
}
else
{
printf(" OFF\n");
}
printf("\n");
printf("\t 5. Use Extended Messaging -");
if (ExtendedMessaging == true)
{
printf(" ON\n");
}
else
{
printf(" OFF\n");
}
printf("\n");
printf("\t 6. Exit\n");
printf("\n");
printf("\n");
printf("\t Enter Choice: ");
}

int main()
{
int hr;
char CharacterRead;
bool RunFlag = 1;
bool *pRunFlag = &RunFlag;

for (int i = 0; i < 16; i++)
{
MessageToSendArray = 'B';
}

for (i = 0; i < 26; i++)
{
OnlineUserArray = '_';
}

// When the app starts no user will be logged in
if ( ( hr = InitiatePort("COM1") ) == 0 )
{
printf("Initialising The Port FAILED!\n");
}
else
{
printf("Initialising The Port SUCCEEDED!\n");
}


printf("Clearing Packet Arrays...\n");
if ( ( hr = ClearPacketArrays() )== 0 )
{
printf("Clearing Packet Arrays FAILED!\n");
}
else
{
printf("Clearing Packet Arrays SUCCEEDED!\n");
}
// Draw menu
menuLoggedOut();


while(RunFlag)
{
//KEYBOARD
switch (KeyBoardStateTracker)
{
case KEYLOGGEDOUT: //If not logged in locally just track network users and pass on packets
{
// Check of user logging in
if ( kbhit() )
{
// Allocate memory for user selection
char CharacterRead;

while ( ( CharacterRead = getchar() ) == '\n' )
{
menuLoggedOut();
}
if (CharacterRead == '1')
{
//printf("Log In Here: ");
while ( ( CharacterRead = getchar() ) == '\n')
{
menuLoggedOut();
printf("\n\n\t Log In Here: ");
}
int converted;
// convert to ascii value
converted = (int) toupper(CharacterRead);
// Declare that im logged on in my log table
OnlineUserArray[(converted-65)] = toupper(CharacterRead);
// Establish that im logged in
LocalUserLoggedIn = true;
// Set Local User ID to newly signed in user
LocalUserID = toupper(CharacterRead);
// Switch to log in routines
KeyBoardStateTracker = KEYLOGGEDIN;
// Display the logged in menu
menuLoggedIn();
// Dont send again
SendLoggedInMessage = true;
}
else if (CharacterRead == '2')
{
RunFlag = 0;
}
}
break;
}
case KEYLOGGEDIN:
{
if ( kbhit() )
{
// Allocate memory for user selection
char CharacterRead;

while ( ( CharacterRead = getchar() ) == '\n' )
{
menuLoggedIn();
}
if (CharacterRead == '1')
{
int ConvertedValue;
// convert to ascii value
ConvertedValue = (int) toupper(LocalUserID);
// Declare that im logged on in my log table
OnlineUserArray[(ConvertedValue-65)] = 0;
// Establish that im logged in
LocalUserLoggedIn = false;
// Switch to log out routines
KeyBoardStateTracker = KEYLOGGEDOUT;
// Tell the transmitter to send a logged out packet
SendLoggedOutMessage = true;
// Display the logged out menu
menuLoggedOut();
}

else if (CharacterRead == '2')
{
int ItemInMessageArray = 0;

printf("\n\n\t Recipient ID: ");

while ( ( CharacterRead = getchar() ) == '\n' )
{
menuLoggedIn();
printf("\n\n\t Recipient ID: ");
}

RecipientOfNextMsg = CharacterRead;

RecipientOfNextMsg = toupper(RecipientOfNextMsg);

printf("\n\t 10 Char Message: ");
for (ItemInMessageArray; ItemInMessageArray < 10; ItemInMessageArray++)
{
while ( ( CharacterRead = getchar() ) == '\n' )
{
menuLoggedIn();
printf("\n\n\t Recipient ID: %c", RecipientOfNextMsg);
printf("\n\t 10 Char Message: ");
}

MessageToSendArray[ItemInMessageArray] = CharacterRead;
}

SendDataMessage = true;

menuLoggedIn();
}
else if (CharacterRead == '3')
{
printf("\nOnline Contacts: ");
for (i = 0; i <26; i++)
{
//if (OnlineUserArray != '0')
//{

printf("%c", OnlineUserArray);
//}
}
}
else if (CharacterRead == '4')
{
if (DisplayFullPacketContents == true)
DisplayFullPacketContents = false;
else
DisplayFullPacketContents = true;
menuLoggedIn();
}
else if (CharacterRead == '5')
{
if (ExtendedMessaging == true)
ExtendedMessaging = false;
else
ExtendedMessaging = true;
menuLoggedIn();
}
else if (CharacterRead == '6')
{
RunFlag = 0;

int ConvertedValue;
// convert to ascii value
ConvertedValue = (int) toupper(LocalUserID);
// Declare that im logged on in my log table
OnlineUserArray[(ConvertedValue-65)] = 0;
// Establish that im logged in
LocalUserLoggedIn = false;
// Switch to log out routines
KeyBoardStateTracker = KEYLOGGEDOUT;
// Tell the transmitter to send a logged out packet
SendLoggedOutMessage = true;
// Display the logged out menu
menuLoggedOut();
}
}
}
}

switch (TransmitStateTracker)
{
case TRANSMITTING:
{
if (LocalUserLoggedIn == true)
{
if (SendLoggedInMessage == true)
{
// Pass the packet type of L to create a log in packet
CompilePacketToSend('L');
// Transmitt the log in packet
SendPacket();
SendLoggedInMessage = false;


}
else if (SendLogInAcknowledgement == true)
{
// Send Aknowledgement packet back to sender only if the
// sender isnt myself DO THIS BIT LATER

CompilePacketToSend('R');
SendPacket();
SendLogInAcknowledgement = false;
}
else if (SendMessageAcknowledgement == true)
{
CompilePacketToSend('Y');
SendPacket();
SendMessageAcknowledgement = false;
}
else if (SendDataMessage == true)
{
CompilePacketToSend('D');
SendPacket();
SendDataMessage = false;
}
else if (SendGenericPacket == true)
{
SendPacket();
SendGenericPacket = false;
}
}
if (SendLoggedOutMessage == true)
{
CompilePacketToSend('X');
SendPacket();
SendLoggedOutMessage = false;
}


break;
}
case NOTTRANSMITTING:
{
}
}

switch (RecieveStateTracker)
{
case RECIEVING:
{
bool bResult;
CharacterRead = ReadCom();
bResult = DealWithRecievedCharacter(CharacterRead, pRunFlag);
if (bResult == true)
{
DisassemblePacket();
}
break;
}
case NOTRECIEVING:
{
}
}


}
return 0;
}



ace

Share this post


Link to post
Share on other sites
Ok, I have read lots about serial comms now and a couple of questions arise.

1. Most of the sources I have seen talk about 22/15/9 way connectors, but I have a 17?

2. All sources deal with sending characters to/from the port. In this application it would appear that rather than sending characters I have to read/write the states of individual pins.

If any one has time, the pin assignments are on page 12 of this file:

http://www.namco.co.uk/Namco%20new/Page%204/PDF/MARS%20Manuals/126&129_User_Guide.pdf

If anyone knows how to insert this table here, I would be extremely appreciative.

Thanks for any help on this one.

Mark

Share this post


Link to post
Share on other sites
Ok, turns out I was wrong, there is simple instruction set for talking to the interface card. Bingo!

Cheers for the code, Ace, it was a HUGE help. The MSDN docs are not so good.

Mark

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!