Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

serial

Status
Not open for further replies.

danielsmusic

New Member
i am going to transfer files down 1 line (serial) this is a project.
first im going to create a program in c++ to send and receve the data.
it will convert each char to it number eg:
Ä = 186
c = 60
then seperate each digit eg:
186 = 1 8 6
60 = 6 0
then convert to my own binary table eg:
1 = 0001
8 = 0011
6 = 1100

6 = 1100
0 = 0000
then send it
the reciver will decode it and put it together.


is there any better way to do this? does anyone know any functions in c++ to convert numbers and chars to binary
 
The simplest way is to use ASCII coding.I dont know how can you do that in C becose i only know how to use Visual Basic.

ASCII is used by keyboards and its an 8 bit format.(so it contains 256 chars)
The Key_down and simalar events return decimal numbers.There decimal numbers decode to keys on the keyborad.

Good luck in you project
 
This is what i came up with, its written in a hurry and therefore maybe not the most beautifull code but it works

Code:
typedef struct
{
	unsigned char Asc[4];		 //this will hold the ASCII representation of the converted char
	unsigned char Bin[13];		//and this will hold an the binary 
}	CDecodeCharStruct, *pCDecodeCharStruct;	

void	DecodeChar(unsigned char CharToConvert, pCDecodeCharStruct pDecodeCharStruct)
{
int			        Loop;
unsigned char 		AndVal;			
	
	//first seperate the digits, no loop for something this simple
	pDecodeCharStruct->Asc[0] = CharToConvert / 100;	//first get the hundreds
	CharToConvert %= 100;								//and strip them off our Temp variable
	pDecodeCharStruct->Asc[1] = CharToConvert / 10;		//now get the tens
	CharToConvert %= 10;								//and strip them off
	pDecodeCharStruct->Asc[2] = CharToConvert;			//the ones remain

	//now convert this into Binary
	for (Loop = 0; Loop < 12; Loop++)					//12 binary digits
	{
		if (!(Loop % 4))								//if we are at the beginning of a new ascii byte
			AndVal = 8;									//then reset andval
		pDecodeCharStruct->Bin[Loop] = !(pDecodeCharStruct->Asc[Loop / 4] & AndVal) ? 0 : 1;
		AndVal >>= 1;									//ponder this one for a while :)
	}

	//now convert our Asc into real ASCII numbers
	for (Loop = 0; Loop < 3; Loop++)
		pDecodeCharStruct->Asc[Loop] += 48;

	//and put terminator on Asc so it can be printed 
	pDecodeCharStruct->Asc[3] = '\0';
}
 
thanks!, but c++ is my most used language. but thanks for the code anyway.
your code, was it in visual basic as it would not work in a c++ compiler thats for sure. if it is is the first function wrong
typedef struct
in c++ it would be: "typedef struct( void );"

this is off topic but did you read the topic called "Who is the funniest person on Electro tech" that was very suprising adults swearing, insulting.
even i was suprised and im in early teens! :eek:
 
it is c++...

typedef struct defines a structure, it isnt a function ... :roll:

i compiled and tested it in MSVC++ 5 minutes ago

to be honest, nothing up there actually uses any of the c++ extensions, a plain C compiler would understand it too...
 
Visual Basic looks like this:
Code:
If A > 10 then
    B = trure
    Form1.Print A
Else:
    B = false
end if

Even i know that is C (Trough i have no clue on this language)
 
Well, in C, you never need them, but they make things so much better to understand and read. They help cut namespace pollution and keeps data that belongs to each other together, this in turn helps the program being clearer so other ppl can understand it, (and you yourself if you need to change it after a year or so)...

If you write real C++ programs you HAVE to use classes (structures that encapsulate both data AND the code to manipulate that data - better known as objects)... That's what C++ is all about, Object oriented programming in stead of procedural programming as in C...
But i think you better train at 'C' programming for a while longer before switching to C++

Have you tried the code btw? it works like a charm :)
 
sorry i have not tryed the code but read it through a few times, im not sure how to use it. if i tryed to compile it it would come up with errors.
while we are on the subject. i would like to know if i use a pointer, can i specify an address to write or read to.

programs that are supposed to run on any windows below nt.
need to create a "virtual" port maby com 3(if it does not exist) to communicate between 2 programs. windows nt does not let the programs read/write from this port stopping the program working properly. is this true?

i wrote some drivers to let my use the parralel port i used it transfer words and letters between to programs i wrote.
 
danielsmusic said:
i am going to transfer files down 1 line (serial) this is a project.
first im going to create a program in c++ to send and receve the data.
it will convert each char to it number eg:
� = 186
c = 60
then seperate each digit eg:
186 = 1 8 6
60 = 6 0
then convert to my own binary table eg:
1 = 0001
8 = 0011
6 = 1100

6 = 1100
0 = 0000
then send it
the reciver will decode it and put it together.


is there any better way to do this? does anyone know any functions in c++ to convert numbers and chars to binary

Why use this encoding?

Why not just send the data character for character?

For example, if you send the letter "A" down the line with any program, the serial port might output something like this:

start bit(s)... 0001001 ...Stop bit(s)

If you want to scramble the data, you should use a microcontroller for that. Have it receive the original data, and then make some code for the uC that scrambles it. It will not slow down the PC.
 
danielsmusic said:
sorry i have not tryed the code but read it through a few times, im not sure how to use it. if i tryed to compile it it would come up with errors.
Code:
int main(int argc, char* argv[])
{
CDecodeCharStruct	DCS;						//make a CDecodeCharStruct Variable
int					Loop;	

												//and call it with the letter 'A' for example
	DecodeChar('A', &DCS);

	printf("%s\n",DCS.Asc);						//The ASCII can now be printed

	for (Loop = 0; Loop < 12; Loop++)			//make a loop to send (or print in this case) the binary
	{
		if (DCS.Bin[Loop])						//is it a '1' ?
			printf("1");						//then print a one - you could send the '1' over serial here
		else
			printf("0");						//otherwise it's a zero - again you could send the '0' over serial here
	}
	printf("\n");
	return 0;
}

danielsmusic said:
programs that are supposed to run on any windows below nt.
need to create a "virtual" port maby com 3(if it does not exist) to communicate between 2 programs. windows nt does not let the programs read/write from this port stopping the program working properly. is this true?

do you mean 2 programs running on 1 computer ?
You need to create a 'pipe' to let them communicate together, but i think that's only possible with MFC applications. (Real windows programs based on the Microsoft Foundation Classes)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top