-
February 27th, 2013, 03:57 AM
#16
Re: Sending Byte to serial port
Originally Posted by Bfrancesco
The errors/warnings you showed are NOT the "compile" ones. They are runtime errors/warnings.
Victor Nijegorodov
-
February 27th, 2013, 04:04 AM
#17
Re: Sending Byte to serial port
-
February 27th, 2013, 09:25 AM
#18
Re: Sending Byte to serial port
The connection between bluetooth device and PC is working.
In the code of http://www.naughter.com/serialport.html there is the app.cpp code. When it runs the port is opened and the connection is made but after some seconds appears this error:
CSerialPort::Write, Failed in call to WriteFile, Error:997
This is a part of app.cpp code:
PHP Code:
CSerialPort port2;
port2.Open(6, 9600, CSerialPort::NoParity, 8, CSerialPort::OneStopBit, CSerialPort::XonXoffFlowControl, TRUE);
CEvent event(FALSE, TRUE);
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(overlapped));
overlapped.hEvent = event;
try
{
port2.Write(pBuf, 10000, overlapped);
}
catch(CSerialException* pEx)
{
if (pEx->m_dwError == ERROR_IO_PENDING)
{
DWORD dwBytesTransferred = 0;
port2.GetOverlappedResult(overlapped, dwBytesTransferred, TRUE);
pEx->Delete();
}
else
{
DWORD dwError = pEx->m_dwError;
Instead this is the part of code od SerialPort.cpp of error message:
PHP Code:
void CSerialPort::Write(const void* lpBuf, DWORD dwCount, OVERLAPPED& overlapped, DWORD* pBytesWritten)
{
//Validate our parameters
ASSERT(IsOpen());
DWORD dwBytesWritten = 0;
BOOL bSuccess = WriteFile(m_hComm, lpBuf, dwCount, &dwBytesWritten, &overlapped);
if (!bSuccess)
{
DWORD dwLastError = GetLastError();
TRACE(_T("CSerialPort::Write, Failed in call to WriteFile, Error:%d\n"), dwLastError);
ThrowSerialException(dwLastError);
}
-
February 27th, 2013, 09:42 AM
#19
Re: Sending Byte to serial port
ERROR_IO_PENDING (error 997) just means that Write operation is not completed yet! From MSDN:
... GetLastError function returns ERROR_IO_PENDING. This allows the calling process to continue processing while the write operation is being completed. The event specified in the OVERLAPPED structure is set to the signaled state upon completion of the write operation.
Please, read about Serial Communications in MSDN:
http://msdn.microsoft.com/en-us/library/ff802693.aspx
http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
Victor Nijegorodov
-
February 28th, 2013, 09:06 AM
#20
Re: Sending Byte to serial port
thank you very much Victor for your help! I'm a new programmer, but i'm improving and the communication between PC and device has been established also with C++ language. I discovered that I have problems with writefile becouse the device bluetooth use Cobs codification and now I'm trying to understand the following Cobs code:
PHP Code:
/*
* StuffData byte stuffs "length" bytes of
* data at the location pointed to by "ptr",
* writing the output to the location pointed
* to by "dst".
*/
#define FinishBlock(X) \
(*code_ptr = (X), \
code_ptr = dst++, \
code = 0x01)
void StuffData(const unsigned char *ptr,
unsigned long length, unsigned char *dst)
{
const unsigned char *end = ptr + length;
unsigned char *code_ptr = dst++;
unsigned char code = 0x01;
while (ptr < end)
{
if (*ptr == 0) FinishBlock(code);
else
{
*dst++ = *ptr;
code++;
if (code == 0xFF) FinishBlock(code);
}
ptr++;
}
FinishBlock(code);
}
this code is used to Cobs codification, but I don't understand how i can use it to codify my array of bytes.
-
February 28th, 2013, 09:28 AM
#21
Re: Sending Byte to serial port
Sorry, but I have no idea what these FinishBlock macro and StuffData function are for and where they come from...
BTW, you should use Code tags (using # toolbar button) rather than PHP or HTML ones.
Besides, your code is very bad formatted: messed indentations and more than one statement on the same line make reading/understanding this code very difficult!
Victor Nijegorodov
-
February 28th, 2013, 09:37 AM
#22
Re: Sending Byte to serial port
-
February 28th, 2013, 10:05 AM
#23
Re: Sending Byte to serial port
Well, then try to encode your byte sequence using this code according to this comments:
Code:
/*
* StuffData byte stuffs "length" bytes of
* data at the location pointed to by "ptr",
* writing the output to the location pointed
* to by "dst".
*/
Then send theough the port the encoded byte sequence pointed to by "dst"
Victor Nijegorodov
-
February 28th, 2013, 10:19 AM
#24
Re: Sending Byte to serial port
Yes, but i don't understand well how i can do it.
-
February 28th, 2013, 10:27 AM
#25
Re: Sending Byte to serial port
You have to declare a destination buffer long enough to contain your encoded byte sequence, then call this StuffData function passing in your original byte array, its length and destination buffer.
Then send what destination buffer will get after this function returns to the serial port.
Victor Nijegorodov
-
March 2nd, 2013, 02:33 PM
#26
Re: Sending Byte to serial port
I have some problems...I'm a new programmer and all codes are not simple for me now. Are there some example code to understand how i can do it?
Thank you and I'm sorry.
-
March 2nd, 2013, 02:40 PM
#27
Re: Sending Byte to serial port
Originally Posted by Bfrancesco
I have some problems...I'm a new programmer and all codes are not simple for me now. Are there some example code to understand how i can do it?
Thank you and I'm sorry.
Well, did you read some books about progamming with C++ for Windows?
Did you try some simple sample applications like "Hello Worlds"?
Some others a little more complicated?
Note, that you cannot from the level "zero" jump to a level for programming serial port communications...
So...
Victor Nijegorodov
-
March 2nd, 2013, 02:55 PM
#28
Re: Sending Byte to serial port
I'm reading and I'm trying many applications.
-
March 2nd, 2013, 03:23 PM
#29
Re: Sending Byte to serial port
Originally Posted by Bfrancesco
I'm reading and I'm trying many applications.
And???
Victor Nijegorodov
-
March 2nd, 2013, 03:38 PM
#30
Re: Sending Byte to serial port
I'm improving and now i'm trying and I'd like to estabilish a connection between 2 devices also if it's not very simple for me.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|