john78304
Junior Member level 1
Hi everybody,
In rencent I am doing a project where i have to transmit data 200 metter far from center, therefore i need to convert 232 to 485 RS.I have found a schedule in this
https://www.edaboard.com/threads/246665/#post1058115
https://obrazki.elektroda.pl/77_1334266235.jpg
In this schedule have two parts:
RS232 to rs485 and the other is rs485 to rs232
I have wire up in breadboard and use a computer to stimulate it.TX of com port is attached to TX of max232 of one circuit and output of max232 in other circuit is connected to RX of com port.Two wires A and B of 2 circuits is connected to each other.But when i run it I received data even when the power for circuit is not connected. and when power is connected then it is not work!I have checked it many times but I can see anything wrong in the circuit.Anyone can give me how to check it and have anything go wrong in this circuit ?
this is the code i used:
In rencent I am doing a project where i have to transmit data 200 metter far from center, therefore i need to convert 232 to 485 RS.I have found a schedule in this
https://www.edaboard.com/threads/246665/#post1058115
https://obrazki.elektroda.pl/77_1334266235.jpg
In this schedule have two parts:
RS232 to rs485 and the other is rs485 to rs232
I have wire up in breadboard and use a computer to stimulate it.TX of com port is attached to TX of max232 of one circuit and output of max232 in other circuit is connected to RX of com port.Two wires A and B of 2 circuits is connected to each other.But when i run it I received data even when the power for circuit is not connected. and when power is connected then it is not work!I have checked it many times but I can see anything wrong in the circuit.Anyone can give me how to check it and have anything go wrong in this circuit ?
this is the code i used:
Code:
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define TIMEOUT_CONSTANT 50
#define ESC 27
int main(int argc, char* argv[])
{
char*s="This is a test";
int key_pressed = 0,i=0;
char outchar = 0;
char inchar = 0;
DWORD bytes_written = 0; // Number of bytes written to port
DWORD bytes_read = 0; // Number of bytes read from port
COMMTIMEOUTS tempComTimeouts; // Our temporary time-outs for COM1
COMMTIMEOUTS savedComTimeouts; // Stores the original time-outs
HANDLE comport = NULL; // Handle for COM port
DCB comSettings; // Contains various port settings
char buf[15];
printf("Sample UART Loopback Terminal Program v1.00.\n");
printf("COM1 PROGRAM\n\n");
// Open COM port
if ((comport =
CreateFile("COM1",
GENERIC_READ | GENERIC_WRITE, // for reading and writing
0, // exclusive access
NULL, // no security attributes
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL)) == INVALID_HANDLE_VALUE)
{
printf("Unable to open COM1.\n\n");
printf("Press any key to exit.");
getch();
return(1);
}
printf("COM1 opened.\n\n");
// Save time-out parameters for COM1
GetCommTimeouts(comport,&savedComTimeouts);
// Set our time-outs
tempComTimeouts.ReadIntervalTimeout = TIMEOUT_CONSTANT;
tempComTimeouts.ReadTotalTimeoutMultiplier = TIMEOUT_CONSTANT;
tempComTimeouts.ReadTotalTimeoutConstant = TIMEOUT_CONSTANT;
tempComTimeouts.WriteTotalTimeoutMultiplier = TIMEOUT_CONSTANT;
tempComTimeouts.WriteTotalTimeoutConstant = TIMEOUT_CONSTANT;
SetCommTimeouts(comport,&tempComTimeouts);
// Set Port parameters.
// We make a call to GetCommState() first in order to fill
// the comSettings structure with all the necessary values.
// Then we change the ones we want and call SetCommState().
GetCommState(comport, &comSettings);
comSettings.BaudRate = CBR_9600;
comSettings.StopBits = ONESTOPBIT;
comSettings.ByteSize = 8;
comSettings.Parity = NOPARITY;
comSettings.fParity = FALSE;
SetCommState(comport, &comSettings);
while(1)
{
i=0;
while(s[i]!=0)
{
// Send data. if succesful, WriteFile() returns non-zero
WriteFile(comport, // Handle
&s[i], // Outgoing data
1, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL); i++;
}
// Read data. if succesful, ReadFile() returns non-zero
ReadFile(comport, // Handle
buf, // Incoming data
sizeof(buf), // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
DWORD j;
printf("\n%s", buf);
for(i=0;i<15;i++)
buf[i]='\0';
}
// Restore time-out parameters
SetCommTimeouts(comport,&savedComTimeouts);
CloseHandle(comport);
getch();
return(0);
}