做过COM口编程的大哥们帮帮忙啊
1、每字节数据由10位组成,一个起始位,一个停止位, 8个数据位。
2、数据采用ASCII码格式。
3、数据传输速率可调(1200-115200)。
4、数据格式:0x02 ST ID WT 0x0d XOR,共计18字节,除起始、结束和校验字节外均为ASCII码
0x02:起始字节
ST: 1位状态,0x31
ID: 7位编号
WT: 6位重量
DN: 1位小数点位置
0x0d:结束字节
XOR: 前17字节的异或值
如:02 31 33 34 35 36 37 38 39 30 30 30 35 38 32 31 0d 32,代表ID号为3456789,重量为58.2kg
这是一个电子称的协议,我该怎么写啊?
------解决方案-------------------- /*------------------------------*
GSERIAL.CPP
For asynchronous serial port communications
适用于DOS环境下异步串口通信编程
ATTENTION: Compile this program with Test Stack Overflow OFF.
在Turbo C++3.0中选项设置 Options/Compiler/Entry中关闭Test Stack Overflow
*------------------------------*/
#include "GSerial.h "
char inbuf[IBUF_LEN]; // in buffer
char outbuf[OBUF_LEN]; // out buffer
unsigned int startbuf = 0;
unsigned int endbuf = 0;
unsigned int inhead = 0;
unsigned int intail = 0;
unsigned int outhead = 0;
unsigned int outtail = 0;
unsigned int PortBase = 0;
GSerial::GSerial()
{
}
GSerial::~GSerial()
{
}
//* get status of the port */
int GSerial::ReadStatus(void)
{
return(inp(m_unPortBase+5));
}
/* send one valid char from the port */
void GSerial::SendChar(unsigned char unCh)
{
while ((ReadStatus() & 0x40) == 0);
outportb(m_unPortBase,unCh);
}
/* send one string from the port */
void GSerial::SendString(int nStrlen, unsigned char *unChBuf)
{
int k=0;
do {
SendChar(*(unChBuf + k));
k++;
} while ((k < nStrlen));
}
/* Install our functions to handle communications */
void GSerial::SetVects(void interrupt(*New_Int)(...))
{
disable();
OldVects = getvect(InterruptNo[m_unPortNo-1]);
setvect(InterruptNo[m_unPortNo-1], New_Int);
enable();
}
/* Uninstall our vectors before exiting the program */
void GSerial::ResetVects(void)
{
setvect(InterruptNo[m_unPortNo-1], OldVects);
}
/* Tell modem that we 're ready to go */
void GSerial::CommOn(void)
{
int temp;
disable();
//temp = inportb(m_unPortBase + MCR) | MCR_INT;
//outportb(m_unPortBase + MCR, temp);
outportb(m_unPortBase + MCR, MCR_INT);
//temp = inportb(m_unPortBase + MCR) | MCR_DTR | MCR_RTS;
//outportb(m_unPortBase + MCR, temp);
temp = (inportb(m_unPortBase + IER)) | IER_RX_INT;//|IER_TX_INT;
outportb(m_unPortBase + IER, temp);
temp = inportb(PIC8259_IMR) & ComIRQ[m_unPortNo-1];
outportb(PIC8259_IMR, temp);
enable();
}
/* Go off-line */
void GSerial::CommOff(void)
{
int temp;
disable();
temp = inportb(PIC8259_IMR) | ~ComIRQ[m_unPortNo-1];
outportb(PIC8259_IMR, temp);
outportb(m_unPortBase + IER, 0);
outportb(m_unPortBase + MCR, 0);
enable();
}
/* Set the port number to use */
int GSerial::SetPortBaseAddr(int Port)