15,918,889 members
Sign in
Sign in
Email
Password
Forgot your password?
Sign in with
home
articles
Browse Topics
>
Latest Articles
Top Articles
Posting/Update Guidelines
Article Help Forum
Submit an article or tip
Import GitHub Project
Import your Blog
quick answers
Q&A
Ask a Question
View Unanswered Questions
View All Questions
View C# questions
View C++ questions
View Javascript questions
View Visual Basic questions
View Python questions
discussions
forums
CodeProject.AI Server
All Message Boards...
Application Lifecycle
>
Running a Business
Sales / Marketing
Collaboration / Beta Testing
Work Issues
Design and Architecture
Artificial Intelligence
ASP.NET
JavaScript
Internet of Things
C / C++ / MFC
>
ATL / WTL / STL
Managed C++/CLI
C#
Free Tools
Objective-C and Swift
Database
Hardware & Devices
>
System Admin
Hosting and Servers
Java
Linux Programming
Python
.NET (Core and Framework)
Android
iOS
Mobile
WPF
Visual Basic
Web Development
Site Bugs / Suggestions
Spam and Abuse Watch
features
features
Competitions
News
The Insider Newsletter
The Daily Build Newsletter
Newsletter archive
Surveys
CodeProject Stuff
community
lounge
Who's Who
Most Valuable Professionals
The Lounge
The CodeProject Blog
Where I Am: Member Photos
The Insider News
The Weird & The Wonderful
help
?
What is 'CodeProject'?
General FAQ
Ask a Question
Bugs and Suggestions
Article Help Forum
About Us
Search within:
Articles
Quick Answers
Messages
Comments by vinod b v (Top 29 by date)
vinod b v
24-Apr-15 10:18am
View
No it doesnt respond as required, but when i just transfer the OF at 200bps alone without sending the rest of the bytes it works just perfect, when i try to send all together i dont get my 25ms low and high pattern.
vinod b v
24-Apr-15 9:54am
View
I am connected to an oscilloscope, where i can view whats happening. When i send F0 @ 200bps i get a low and high for 25ms respectively.next i need to send the 5 bytes @10400.
vinod b v
24-Apr-15 9:30am
View
I want to send data to USB to serial converter and implement a wakeup pattern.send F0 at 200 bps and then send rest 5 bytes of data. i need to loop it continously.
vinod b v
24-Apr-15 6:36am
View
yes i am having a problem with this code now. I need just a loop so that the data moves continously i tried while but with no success.i am totally confused with this now
vinod b v
24-Apr-15 6:13am
View
Yes i will modify my question and post.
vinod b v
22-Apr-15 5:47am
View
I did change it, but only F0 at 200 baud rate is being transferred, but unable to view the next 5 bytes at 10400 baud rate, i have updated the question.
vinod b v
22-Apr-15 5:37am
View
Deleted
@phil.o As per solution 1 and your solution i changed the code but i am able to see only F0 being transferred but not the rest of the bytes, i am attaching my code please have a look.
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>
#include<process.h>
#define true 1
int main()
{
// int _kbhit();
// int _getch();
// Defining hexadecimal bytes to be sent a 200 bps for generation of low high sequence//
unsigned char bytes_to_send[1];
bytes_to_send[0] = 0xF0; /* 11110000b */
unsigned char databytes_send[5];
databytes_send[0] = 0x81;
databytes_send[1] = 0x11;
databytes_send[2] = 0xF1;
databytes_send[3] = 0x11;
databytes_send[4] = 0x04;
/* int GetPressedKey()
{
int ret = 0;
if (_kbhit())
{
ret = _getch();
}
return ret;
} */
// Declare variables and structures//
HANDLE hSerial;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};
// Open the available serial port number//
fprintf(stderr, "Opening serial port...");
hSerial = CreateFile(
"COM4", GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if (hSerial == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Check for port connections\n");
return 1;
}
else
fprintf(stderr, "OK\n");
// Set device parameters (200 baud, 1 start bit,
// 1 stop bit, no parity)
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (GetCommState(hSerial, &dcbSerialParams) == 0)
{
fprintf(stderr, "Error getting device state\n");
CloseHandle(hSerial);
return 1;
}
// Set baudrate to send bytes at 200 bps//
dcbSerialParams.BaudRate = 200;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if(SetCommState(hSerial, &dcbSerialParams) == 0)
{
fprintf(stderr, "Error setting device parameters\n");
CloseHandle(hSerial);
return 1;
}
// Set COM port timeout settings//
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if(SetCommTimeouts(hSerial, &timeouts) == 0)
{
fprintf(stderr, "Error setting timeouts\n");
CloseHandle(hSerial);
return 1;
}
while(true)
{
// Send specified text (remaining command line arguments)
DWORD bytes_written = 0;
fprintf(stderr, "sending bytes...\n");
while(1)
{
//Transmit slow initialization data
if (!WriteFile(hSerial, bytes_to_send, 1,&bytes_written, NULL))
// break;
fprintf(stderr, "%d bytes written\n", bytes_written);
//check for key press event
/* if (GetPressedKey()) */
// break;
}
// Open the available serial port number//
// Set device parameters (200 baud, 1 start bit,
// 1 stop bit, no parity)
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (GetCommState(hSerial, &dcbSerialParams) == 0)
{
fprintf(stderr, "Error getting device state\n");
CloseHandle(hSerial);
return 1;
}
// Set baudrate to send bytes at 10400 bps 8 N 1//
dcbSerialParams.BaudRate = 10400;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if(SetCommState(hSerial, &dcbSerialParams) == 0)
{
fprintf(stderr, "Error setting device parameters\n");
CloseHandle(hSerial);
return 1;
}
// Set COM port timeout settings//
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.Wri
vinod b v
22-Apr-15 5:31am
View
@ Richard MacCutchan I tried it but unfortunately the second part of the code does not run. I mean the i can only see F0 being displayed in my waveform and repeats continously
vinod b v
15-Apr-15 7:04am
View
@Cpallini Accordingly at 200 baud i should get 25ms and 25ms high,when i measure i am not able to get the required wave i am getting like 10ms low and 40ms high.
But when i just send of at 200 baud without sending other part of the code. i am able to get it, but when i want to do it simultaneously i am failing.
vinod b v
15-Apr-15 6:01am
View
I am supposed to get 25ms low and 25ms high square wave upon sending byte 0xF0 at 200 baud, and next my 5 databytes should be sent at 10400, but i am not able to see the exact waveform in my oscilloscope. is there something wrong in the code??
vinod b v
9-Apr-15 7:12am
View
@ OriginalGriff Could you give me an example of how i do it in macro scale?
vinod b v
24-Mar-15 7:44am
View
I want to first do a wakeup pattern before talking to ECU, so i am using fast intialisation which uses 10400bps, but if i send all data at once i dont find the required timing diagram in my oscilloscope.The requirement for KWP 2000 is as follows
It operates at 10400 Baud.
Initial state – Tidle .
Tlow for 25ms and Thigh for 25ms
Start communication request from Tester to ECU
Start communication response from ECU to Tester.
I am able to generate 25ms low and 25ms high, at 200bps. and when i change the baudrate at 10400 for rest of bytes it does behave strange. I tried i call a function for the second part.
vinod b v
24-Mar-15 6:30am
View
i am using USB to serial Converter for OBD Diagnostics. And the problem occurs when i try to send the other set of bytes at higher baudrate, i am trying to intitialise with KWP2000 protocol
vinod b v
24-Mar-15 5:27am
View
Dear Jochen Arndt, How do i make that the data arent on the line when changing baudrate. As per your advice i inserted another block the dcbSerialParams structure. but when i run all the data are overlapping. COuld you please help me on this issue.
vinod b v
23-Mar-15 11:22am
View
Dear JOchen, is this write?
#include <windows.h>
#include <stdio.h>
#include <conio.h>
// INITIALISATION FOR 25MS LOW AND 25MS HIGH WITH PREDEFINED BAUDRATE OF 200 Bps,DATA 0XF0//
int main()
{
int _kbhit();
int _getch();
// Defining hexadecimal bytes to be sent a 200 bps//
int bytes_to_send[1];
bytes_to_send[0] = 0xF0;
// Bytes to be send at 10400 bps//
int databytes_send[5];
databytes_send[0] = 0x81;
databytes_send[1] = 0x11;
databytes_send[2] = 0xF1;
databytes_send[3] = 0x11;
databytes_send[4] = 0x04;
int GetPressedKey()
{
int ret = 0;
if (_kbhit())
{
ret = _getch();
}
return ret;
}
// Declare variables and structures//
HANDLE hSerial;
DCB dcbSerialParams = {0};
COMMTIMEOUTS timeouts = {0};
// Open the available serial port number//
fprintf(stderr, "Opening serial port...");
hSerial = CreateFile(
"COM3", GENERIC_READ|GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
if (hSerial == INVALID_HANDLE_VALUE)
{
fprintf(stderr, "Error\n");
return 1;
}
else
fprintf(stderr, "OK\n");
// Set device parameters (10400 baud, 1 start bit,
// 1 stop bit, no parity)
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
if (GetCommState(hSerial, &dcbSerialParams) == 0)
{
fprintf(stderr, "Error getting device state\n");
CloseHandle(hSerial);
return 1;
}
dcbSerialParams.BaudRate = 200;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if(SetCommState(hSerial, &dcbSerialParams) == 0)
{
fprintf(stderr, "Error setting device parameters\n");
CloseHandle(hSerial);
return 1;
}
// Calling DCB again //
dcbSerialParams.BaudRate = 10400;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONESTOPBIT;
dcbSerialParams.Parity = NOPARITY;
if(SetCommState(hSerial, &dcbSerialParams) == 0)
{
fprintf(stderr, "Error setting device parameters\n");
CloseHandle(hSerial);
return 1;
}
// Set COM port timeout settings
timeouts.ReadIntervalTimeout = 50;
timeouts.ReadTotalTimeoutConstant = 50;
timeouts.ReadTotalTimeoutMultiplier = 10;
timeouts.WriteTotalTimeoutConstant = 50;
timeouts.WriteTotalTimeoutMultiplier = 10;
if(SetCommTimeouts(hSerial, &timeouts) == 0)
{
fprintf(stderr, "Error setting timeouts\n");
CloseHandle(hSerial);
return 1;
}
// Send specified text (remaining command line arguments)
DWORD bytes_written, total_bytes_written = 0;
fprintf(stderr, "sending bytes...");
//fprintf("%d%%d%d%d%d",bytes_to_send[0],bytes_to_send[1],bytes_to_send[2],bytes_to_send[3],bytes_to_send[4]);//
while(1)
{
if (!WriteFile(hSerial, bytes_to_send, 1, &bytes_written, NULL))
break;
fprintf(stderr, "%d bytes written\n", bytes_written);
if (GetPressedKey())
break;
// Read data here
if (!WriteFile(hSerial, databytes_send, 5, &bytes_written, NULL))
break;
fprintf(stderr, "%d bytes written\n", bytes_written);
if (GetPressedKey())
break;
if (GetPressedKey())
break;
}
CloseHandle(hSerial);
if(!WriteFile(hSerial, databytes_send, 6, &bytes_written, NULL))
{
fprintf(stderr, "End of transmission\n");
CloseHandle(hSerial);
return 1;
}
fprintf(stderr, "%d bytes written\n", bytes_written);
// Close serial port
fprintf(stderr, "Closing serial port...");
if (CloseHandle(hSerial) == 0)
{
fprintf(stderr, "Error\n");
return 1;
}
fprintf(stderr, "OK\n");
// exit normally
return 0;
}
vinod b v
23-Mar-15 10:42am
View
yes i shall do it, thank you.
vinod b v
23-Mar-15 10:37am
View
Could you help me with this part of as n how can i call the Setcomm state, and send the bytes of data with different baud rate could you give me an example?
vinod b v
23-Mar-15 10:35am
View
I am not understanding the way how to do it. first part of code works as i want, but once i send 1 byte of data at certain baudrate, later i need to change baudrate and send some more bytes of data to talk to ECU
vinod b v
23-Mar-15 10:32am
View
for initialisation purpose
vinod b v
19-Mar-15 10:51am
View
@ Jochen I could not follow what you meant? My main aim atlast is a GUI, but at first place i need to work on this part, sending bytes across the port and reading back and looping the data continously to observe the waveform with oscilloscope. ALthough not an expert at C.But i have created applications before with Labview.
vinod b v
19-Mar-15 6:39am
View
When i use standard baud rate CBR_9600 or CBR_10400, it works fine but upon using CBR_10400 it gives me an error telling undeclared. Could you tell me whats wrong in setting a desired baud rate of my own as it is required for KWP 2000 Fast Initialization
vinod b v
18-Mar-15 11:57am
View
I checked everything and i still dont know whats the error.
HANDLE hSerial; seems fine.
vinod b v
18-Mar-15 11:23am
View
Yes i am using windows.h but still unsucessful.
vinod b v
18-Mar-15 10:52am
View
Yes , thank you
vinod b v
18-Mar-15 10:39am
View
No i did compile it as .CPP. And have used all the included the windows header file.
vinod b v
18-Mar-15 10:17am
View
@jochen Arndt, yes i did, even this its giving these errors 5 and 12
error at 5. hSerial does not name a type.
error at 12. expected unqualified error before 'if'
5. HANDLE hSerial; // VARIABLE IS OF TYPE HANDLE AND INITILIZE WITH CALL TO CREATE FILE//
hSerial = createFile ("COM4",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
12. if(hSerial==INVALID_HANDLE_VALUE)
vinod b v
18-Mar-15 10:08am
View
I found out my system is using COM4, so i have changed it from COM1 to COM4, but still i get that error, and even did give entry like this:
HANDLE hSerial; // VARIABLE IS OF TYPE HANDLE AND INITILIZE WITH CALL TO CREATE FILE//
hSerial = createFile ("USB serial port(COM4)",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if(hSerial==INVALID_HANDLE_VALUE)
{
if(GetLastError()=ERROR_FILE_NOT_FOUND){
printf("//SERIAL PORT DOES NOT EXIST// ");
}
printf("//SOME OTHER ERROR OCCURED.//");
}
// SETTING PARAMETERS//
DCB dcbSerialParams = {0};
dcbSerial.DCBlength=Sizeof(dcbSerialParams);
if
(!GetCommState(hSerial, &dcbSerialParams))
{
// error getting state //
Printf("Error in getting state");
}
//SETTING PARAMETERS//
dcbSerialParams.BaudRate=CBR_9600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
if(!SetCommState(hSerial, &dcbSerialParams)){
//error setting serial port state
printf("Error");
}
//SETTING TIMEOUTS //
COMMTIMEOUTS timeouts={0};
timeouts.ReadIntervalTimeout=50;
timeouts.ReadTotalTimeoutConstant=50;
timeouts.ReadTotalTimeoutMultiplier=10;
timeouts.WriteTotalTimeoutConstant=50;
timeouts.WriteTotalTimeoutMultiplier=10;
if(!SetCommTimeouts(hSerial, &timeouts)){
//error occureed. Inform user
printf("Error occuring in timeouts");
}
char szBuff[n + 1] = {0};
DWORD dwBytesRead = 0;
if(!ReadFile(hSerial, szBuff, n, &dwBytesRead, NULL)){
//error occurred. Report to user.
printf("Error occured in read/write byte");
}
// Close handle //
CloseHandle(hSerial);
vinod b v
18-Mar-15 9:27am
View
@ Jan Bakker, no i am using C language to write it although i am new to serial programming, I am using Codeblocks IDE
vinod b v
18-Mar-15 9:09am
View
@ Jochen Arndt, i have a question here i am using code blocks IDE console application and when i write this piece of code i am getting error invalid handle
#include<windows.h>
// For opening serial port//
HANDLE hSerial;
hSerial = CreateFile("COM1",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if(hSerial==INVALID_HANDLE_VALUE){
if(GetLastError()==ERROR_FILE_NOT_FOUND){
//serial port does not exist. Inform user.
}
Show More