首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++通讯连接,该如何处理

2012-03-25 
C++通讯连接通讯格式:字节传输为11位模式,具有1个起始位、8个数据位、1个奇偶校验位(1表示此字节为地址,0表

C++通讯连接
通讯格式:
    字节传输为11位模式,具有1个起始位、8个数据位、1个奇偶校验位(1表示此字节为地址,0表示此字节为数据)和1个终止位。半双工模式的异步串行传输,所有的字节从最低位开始发送。在C++中如何实现这样的通讯协议????

[解决办法]
查看MSDN的关于DCB结构的帮助。
[解决办法]
设置好波特率,配置好寄存器,它就会按照这个格式发送了。

[解决办法]
定长模式发送数据
send_n()
拼数据

具有1个起始位(0XFF)、8个数据位(...)、1个奇偶校验位(0X00)(1表示此字节为地址,0表示此字节为数据)和1个终止位(0X00)
0x00 0x00 ... 0xFF 反向拼接数据,然后发送出去。


收数据的时侯可以采用定长的方式收数据recv_n()


不过现在通信上还这样写,万一丢失1个字节,所有的数据都错了,当然前提是我们认为TCP/IP不会丢数据包。 UDP 就不说了。
最好的解决方式是,定义数据包,接受数据,截取数据包,然后分析截取后的数据包,抛弃错误的数据。
[解决办法]
首先,我理解lz是想从纯软件来解决这几个问题,我的建议如下:
1。 组建包内容是否可以考虑使用内存位拷贝的方式,这样你可控制一个字节内所有位的任意顺序,你想让谁在前谁在后都可控。 这可是C语言的强项。就是纯粹玩指针和位运算,参考代码如下,强烈建议你根据需要自己更改一下底稿:
。。。
#define BITS_IN_BYTE 8
...
BYTE copy_element(BYTE **pos, unsignned long element, BYTE size, BYTE bits_left_in_byte, BYTE *msg_len)
{
unsignned long dummy;
long rem_bits_in_element;
long shift;
byte msb_dummy;
byte mask_val = 0xff;
byte temp_mask;

element = ( element & (LONG_BLANK_MASK> > (BITS_IN_LONG - size)) );
dummy = element;
rem_bits_in_element = size;

while (rem_bits_in_element > 0)
{
/*
* if the number of bits left in byte is more than the size of element
* then shift the element to get to the right positon so that it can
* be inserted into the buffer directly
*/

if(bits_left_in_byte > = rem_bits_in_element)
{
/* calculate the number of bit positons the element needs to be shifted */
/* in this branch ,
rem_bits_in_element > 0 and bits_left_in_byte > = rem_bits_in_element so
the value of the following subtraction is always > = 0
and < 255 which is the max value of the UBYTE bits_left_in_byte;
but to avoid loss of sign/precision warnings we use a LONG to hold
the subtraction result
*/
shift = bits_left_in_byte - rem_bits_in_element;

/* mask bits in buffer where we insert the new element bits */
temp_mask = ((mask_val < < bits_left_in_byte) | (mask_val > > (BITS_IN_BYTE - shift)));
*(*(pos)) &= temp_mask;

/* insert the element in the buffer */
*(*(pos)) |= (UBYTE)(element < < shift);

/* in this branch,
rem_bits_in_element > 0 and bits_left_in_byte > = rem_bits_in_element
so
the value of the following subtraction is always > = 0
and < 255 which is the max value of the UBYTE bits_left_in_byte;
don 't want to change the function 's interface right now so we 'll
just cast the result of the subtraction to a UBYTE */
bits_left_in_byte = (UBYTE)(bits_left_in_byte - rem_bits_in_element);

/* set the rem_bits_in_element to 0 */
rem_bits_in_element = 0;

/*
* increment the byte position of the buffer and the message length also if
* the current byte is completely filled
*/
if (bits_left_in_byte == 0)
{
(*pos)++;
*msg_len = *msg_len + 1;
bits_left_in_byte = BITS_IN_BYTE;
}
}
else
{
/*
* if the number of bits left in byte is less than the size of element
* then split the element into a LSB part and MSB part. Move the MSB part


* to the right position and insert it into the current byte.
*/
dummy = element;

msb_dummy = (UBYTE)(dummy > > (rem_bits_in_element - bits_left_in_byte));

rem_bits_in_element -= bits_left_in_byte;

/* mask bits in buffer where we insert the new element bits */
*(*(pos)) &= (UBYTE) (mask_val < < bits_left_in_byte);

/* insert msb of the element into the current byte */
*(*(pos)) = *(*(pos)) | msb_dummy;

/* increment the byte position and message length */
(*pos)++;
*msg_len = *msg_len + 1;

bits_left_in_byte = BITS_IN_BYTE;
}
}
/* return bits left in byte */
return bits_left_in_byte;
}
2。 半双工问题,能不能把你的平台说一下,以及用什么操作系统?

热点排行