Here is some of code :
//**************************************
//****** Receive Code in Linux *********
bool setSerial(char* portName,int parity)
{
struct termios options;
//open port
portNumber = open(portName,O_RDWR | O_NOCTTY | O_NDELAY);
if(portNumber == -1)
printf("port openning error\n");
//get the current setting of the serial port
tcgetattr(portNumber,&options);
//---------set the setting
fcntl(portNumber, F_SETFL, FNDELAY);
options.c_lflag &= ~(ICANON | ECHO | ISIG);
cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
if(parity==0) //no parity
{
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
}
else if(parity==1) //odd parity
{
options.c_cflag |= PARENB;
options.c_cflag |= PARODD;
options.c_cflag |= ~CSTOPB;
options.c_cflag |= ~CSIZE;
options.c_cflag |= CS7;
}
else //even parity
{
options.c_cflag |= PARENB;
options.c_cflag |= ~PARODD;
options.c_cflag |= ~CSTOPB;
options.c_cflag |= ~CSIZE;
options.c_cflag |= CS7;
}
options.c_cflag |= (CLOCAL | CREAD);
//apply the setting to the serial port
if(tcsetattr(portNumber, TCSANOW, &options)!= 0)
printf("port setting error\n");
return true;
}
void readData(void)
{
for(j=0 ; j < readNo ; j++)
{
switch(FRAMEFLAG)
{
case 0 :if( buffer[j] == '$' )
FRAMEFLAG = 1;
break;
case 1 :Number = buffer[j];
FRAMEFLAG = 2;
break;
case 2 :if( buffer[j] == ~Number )
FRAMEFLAG = 3;
else
FRAMEFLAG = 0;
break;
case 3 :High = buffer[j];
FRAMEFLAG = 4;
break;
case 4 :Low = buffer[j];
FRAMEFLAG = 5;
break;
case 5 :if( buffer[j] == '*' )
frameEnd = true;
FRAMEFLAG = 0;
break;
}
if( frameEnd )
{
frameEnd = false;
OrderNumber = charToInt(Number);
Value = charToInt(High) * 256 + charToInt(Low);
printf("Number %d Data is : %d \n", OrderNumber, Value);
}
}
}
void* readThreadFunc(void* arg)
{
while(READON)
{
readNo = read(portNumber, buffer, bufSize);
if( readNo > 0 )
{
readData();
}
}
return NULL;
}
int main(int argc, char **argv)
{
char portNo[12] = "/dev/ttyS0";
if( !setSerial(portNo,0) ) return -1;
pthread_create(&readThread, NULL, &readThreadFunc, NULL);
//...........
//close serial port
if(close(portNumber) == -1) return -1;
return 1;
}
//**************************************
//****** Send Code in Windows **********
void CSendDlg:
oExecute()
{
while(Run)
{
for( i=0 ; num < 250 ; i++ )
{
transmit=MainArray.GetAt(i);
if(transmit!=0)
{
SendArray.Format("%c",transmit);
MySerial.Write(SendArray);
}
//windows don't send 0x00 (it's NULL)
else
{
SendArray="";
MySerial.Write(SendArray,1);
}
}
i=0;
}
}