📄 cserialport.c
字号:
/**************************************** Ready to send ****************************************/BEGIN_PROPERTY ( CSERIALPORT_RTS ) int ist; if (READ_PROPERTY) { if ( !THIS->iStatus ) { GB.ReturnBoolean(0); return; } Serial_Signal_Status(&THIS->ser_status,THIS->Port); GB.ReturnBoolean(THIS->ser_status.s_RTS); return; } if (!THIS->iStatus ) { GB.Error ("Port is Closed."); return; } ioctl(THIS->Port,TIOCMGET,&ist); if(!VPROP(GB_BOOLEAN)) ist &= ~TIOCM_RTS; else ist = ist | TIOCM_RTS; ioctl(THIS->Port,TIOCMSET,&ist);END_PROPERTY/**************************************** Clear to send ****************************************/BEGIN_PROPERTY ( CSERIALPORT_CTS ) if ( !THIS->iStatus ) { GB.ReturnBoolean(0); return; } Serial_Signal_Status(&THIS->ser_status,THIS->Port); GB.ReturnBoolean(THIS->ser_status.s_CTS);END_PROPERTY/*************************************** Data Carrier Detect ***************************************/BEGIN_PROPERTY ( CSERIALPORT_DCD ) if ( !THIS->iStatus ) { GB.ReturnBoolean(0); return; } Serial_Signal_Status(&THIS->ser_status,THIS->Port); GB.ReturnBoolean(THIS->ser_status.s_DCD);END_PROPERTY/*************************************** Ring ***************************************/BEGIN_PROPERTY ( CSERIALPORT_RNG ) if ( !THIS->iStatus ) { GB.ReturnBoolean(0); return; } Serial_Signal_Status(&THIS->ser_status,THIS->Port); GB.ReturnBoolean(THIS->ser_status.s_RNG);END_PROPERTY/********************************************************************* Gets / Sets serial port name (on Linux /dev/ttyS0, etc)**********************************************************************/BEGIN_PROPERTY ( CSERIALPORT_Port ) if (READ_PROPERTY) { GB.ReturnString(THIS->sPort); return; } if ( THIS->iStatus ) { GB.Error("Current port must be closed first."); return; } GB.StoreString(PROP(GB_STRING), &THIS->sPort);END_PROPERTY/******************************************************************************* FlowControl : 1->CRTSCTS , 2-> XON/XOFF , 3-> XON/OFF plus CRTSCTS, 4 --> NONE********************************************************************************/BEGIN_PROPERTY ( CSERIALPORT_FlowControl ) if (READ_PROPERTY) { GB.ReturnInteger(THIS->iFlow); return; } if ( THIS->iStatus ) { GB.Error("Current port must be closed first."); return; } if ( (VPROP(GB_INTEGER)<0) || (VPROP(GB_INTEGER)>3) ) { GB.Error("Invalid flow control value."); return; } THIS->iFlow=VPROP(GB_INTEGER);END_PROPERTY/********************************************************************* Gets / Sets serial parity (E,O,N)**********************************************************************/BEGIN_PROPERTY ( CSERIALPORT_Parity ) int parity; if (READ_PROPERTY) { GB.ReturnInteger(THIS->Parity); } else { if ( THIS->iStatus ) GB.Error("Current port must be closed first."); else { parity = VPROP(GB_INTEGER); if (parity < 0 || parity > 2) GB.Error("Invalid parity"); else THIS->Parity = parity; /* GB.StoreString(PROP(GB_STRING), &tmpstring); if ( GB.StringLength (tmpstring) != 1 ) { myok=0; } else { if (ConvertParity(tmpstring[0]) == -1) myok=0; } GB.FreeString(&tmpstring); if (!myok) GB.Error("Invalid parity value."); else GB.StoreString(PROP(GB_STRING), &THIS->Parity); */ } }END_PROPERTY/********************************************************************* Gets / Sets serial port Speed**********************************************************************/BEGIN_PROPERTY ( CSERIALPORT_Speed ) int myok=1; if (READ_PROPERTY) { GB.ReturnInteger(THIS->Speed); return; } if ( THIS->iStatus ){ GB.Error("Current port must be closed first.");return; } if ( !VPROP(GB_INTEGER) ) myok=0; if ( ConvertBaudRate(VPROP(GB_INTEGER)) == -1) myok=0; if (!myok) GB.Error("Invalid speed value."); else THIS->Speed=VPROP(GB_INTEGER);END_PROPERTY/********************************************************************* Gets / Sets serial port Data Bits**********************************************************************/BEGIN_PROPERTY ( CSERIALPORT_DataBits ) int myok=1; if (READ_PROPERTY) { GB.ReturnInteger(THIS->DataBits); return; } if ( THIS->iStatus ) { GB.Error("Current port must be closed first."); return; } if ( ConvertDataBits(VPROP(GB_INTEGER)) == -1) myok=0; if (!myok) GB.Error("Invalid data bits value."); else THIS->DataBits=VPROP(GB_INTEGER);END_PROPERTY/********************************************************************* Gets / Sets serial port Stop Bits**********************************************************************/BEGIN_PROPERTY ( CSERIALPORT_StopBits ) int myok=1; if (READ_PROPERTY) { GB.ReturnInteger(THIS->StopBits); return; } if ( THIS->iStatus ){GB.Error("Current port must be closed first.");return;} if ( ConvertStopBits(VPROP(GB_INTEGER)) == -1) myok=0; if (!myok) GB.Error("Invalid stop bits value."); else THIS->StopBits=VPROP(GB_INTEGER);END_PROPERTY/************************************************* Gambas object "Constructor" *************************************************/BEGIN_METHOD_VOID(CSERIALPORT_new) THIS->stream.desc=NULL; THIS->Port=0; THIS->iStatus=0; THIS->sPort=NULL; GB.NewString(&THIS->sPort,"/dev/ttyS0",10); THIS->Speed=19200; THIS->Parity=0; THIS->DataBits=8; THIS->StopBits=1; THIS->iFlow=1;END_METHOD/************************************************* Gambas object "Destructor" *************************************************/BEGIN_METHOD_VOID(CSERIALPORT_free) if (THIS->iStatus) { CSerialPort_FreeCallBack((long)THIS); GB.Stream.Init(&THIS->stream,-1); CloseSerialPort(THIS->Port,&THIS->oldtio); THIS->iStatus=0; } GB.FreeString(&THIS->sPort);END_METHOD/************************************************* To open the port *************************************************/BEGIN_METHOD_VOID(CSERIALPORT_Open) if (THIS->iStatus) { GB.Error("Port is already opened."); return; } if(OpenSerialPort(&THIS->Port,THIS->iFlow,&THIS->oldtio,THIS->sPort, \ THIS->Speed,THIS->Parity,THIS->DataBits,THIS->StopBits)) { GB.Error("Error opening serial port."); return; } THIS->e_DTR.nevent=0; THIS->e_DSR.nevent=1; THIS->e_RTS.nevent=2; THIS->e_CTS.nevent=3; THIS->e_DCD.nevent=4; THIS->e_RNG.nevent=5; THIS->e_DTR.obj=THIS; THIS->e_DSR.obj=THIS; THIS->e_RTS.obj=THIS; THIS->e_CTS.obj=THIS; THIS->e_DCD.obj=THIS; THIS->e_RNG.obj=THIS; Serial_Signal_Status(&THIS->ser_status,THIS->Port); CSerialPort_AssignCallBack((long)THIS,THIS->Port); //CSerialPort_stream_init(&THIS->stream,THIS->Port); THIS->stream.desc=&SerialStream; THIS->stream._free[0]=(long)THIS; THIS->iStatus=1;END_METHOD/*************************************************************** Here we declare the public interface of SerialPort class ***************************************************************/GB_DESC CSerialPortDesc[] ={ GB_DECLARE("SerialPort", sizeof(CSERIALPORT)), GB_INHERITS(".Stream"), GB_CONSTANT("None", "i", 0), GB_CONSTANT("Hardware", "i", 1), GB_CONSTANT("Software", "i", 2), GB_CONSTANT("Both", "i", 3), GB_CONSTANT("Even", "i", 1), GB_CONSTANT("Odd", "i", 2), GB_CONSTANT("Bits1", "i", 1), GB_CONSTANT("Bits2", "i", 2), GB_CONSTANT("Bits5", "i", 5), GB_CONSTANT("Bits6", "i", 6), GB_CONSTANT("Bits7", "i", 7), GB_CONSTANT("Bits8", "i", 8), GB_EVENT("Read", NULL, NULL, &Serial_Read), GB_EVENT("DTRChange", NULL, "(CurrentValue)b", &Serial_DTR), GB_EVENT("DSRChange", NULL, "(CurrentValue)b", &Serial_DSR), GB_EVENT("RTSChange", NULL, "(CurrentValue)b", &Serial_RTS), GB_EVENT("CTSChange", NULL, "(CurrentValue)b", &Serial_CTS), GB_EVENT("DCDChange", NULL, "(CurrentValue)b", &Serial_DCD), GB_EVENT("RNGChange", NULL, "(CurrentValue)b", &Serial_RNG), GB_METHOD("_new", NULL, CSERIALPORT_new, NULL), GB_METHOD("_free", NULL, CSERIALPORT_free, NULL), GB_METHOD("Open", NULL, CSERIALPORT_Open, NULL), //GB_METHOD("Close", NULL, CSERIALPORT_Close, NULL), GB_PROPERTY("FlowControl","i<SerialPort,Hardware,Software,Both,None>",CSERIALPORT_FlowControl), GB_PROPERTY("PortName", "s", CSERIALPORT_Port), GB_PROPERTY("Parity", "i<SerialPort,None,Even,Odd>", CSERIALPORT_Parity), GB_PROPERTY("Speed", "i", CSERIALPORT_Speed), GB_PROPERTY("DataBits", "i<SerialPort,Bits8,Bits7,Bits6,Bits5>", CSERIALPORT_DataBits), GB_PROPERTY("StopBits", "i<SerialPort,Bits1,Bits2>", CSERIALPORT_StopBits), GB_PROPERTY("DTR", "b", CSERIALPORT_DTR), GB_PROPERTY("RTS", "b", CSERIALPORT_RTS), GB_PROPERTY_READ("Status", "i", CSERIALPORT_Status), GB_PROPERTY_READ("DSR", "b", CSERIALPORT_DSR), GB_PROPERTY_READ("CTS", "b", CSERIALPORT_CTS), GB_PROPERTY_READ("DCD", "b", CSERIALPORT_DCD), GB_PROPERTY_READ("RNG", "b", CSERIALPORT_RNG), GB_CONSTANT("_Properties", "s", "FlowControl=1,PortName,Parity=0,Speed=19200,DataBits=8,StopBits=1"), GB_CONSTANT("_DefaultEvent", "s", "Read"), GB_END_DECLARE};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -