Boas tardes, in recent times I've dedicated myself a little programming in FreeBasic (http://www.freebasic.net) and, one of the programs I did was to send messages through a GSM modem (mobile phone, Broadband stick, etc), but it had always a problem, If by chance the COM port that the program caused error, and as in most cases they use virtual ports, is a problem too often… I decided to try to solve the problem and there was light 🙂 !
EntĂŁo aqui vai o programa:
Dim Shared RS232_MUTEX As Any Ptr Dim Shared RS232_IN As String Dim Shared RS232_OUT As String Dim Shared RS232_COMM As String Dim Shared RS232_STATUS As UByte Dim Shared RS232_FILE As Integer Dim Shared RS232_IN_SUB As Any Ptr Dim Shared RS232_OUT_SUB As Any Ptr Private Dim Shared done As UByte Private Sub rs232insub Dim temp As String = "" RS232_MUTEX = Mutexcreate Sleep 20,1 While 1 If RS232_STATUS = 0 Then Exit Sub Line Input #RS232_FILE, temp If temp <> "" Then Mutexlock RS232_MUTEX RS232_IN = RS232_IN + temp Mutexunlock RS232_MUTEX temp = "" End If Wend End Sub Private Sub rs232outsub Print #RS232_FILE, RS232_OUT done = 1 End Sub Function RS232_ERR_STR(err_n As UByte) As String Select Case err_n Case 0 Return "No error" Case 1 Return "Can't Open - Its Open" Case 2 Return "Invalid COMM Port - No COMM" Case 3 Return "Invalid COMM Port - Can't Open" Case 4 Return "Nothing to send" Case 5 Return "Send Time-OUT" Case 5 Return "Receive Time-OUT - Specific String" Case 6 Return "Receive Specific String Found" Case Else Return "Unknown Error" End Select End Function Function RS232_OPEN As UByte If RS232_STATUS = 1 Then Return 1 If RS232_COMM = "" Then Return 2 RS232_FILE = Freefile Open Com (RS232_COMM)For Binary As #RS232_FILE If Err <> 0 Then Return 3 RS232_STATUS = 1 Rem start Input Sub RS232_IN_SUB = Threadcreate(@rs232insub) End Function Sub RS232_CLOSE RS232_STATUS = 0 Threadwait ( RS232_IN_SUB ) RS232_IN = "" RS232_OUT = "" RS232_COMM = "" delete RS232_IN_SUB delete RS232_OUT_SUB Mutexdestroy RS232_MUTEX End Sub Function RS232_SEND (timeout As Double) As UByte done = 0 If RS232_STATUS = 0 Then Return 1 If RS232_OUT = "" Then Return 4 RS232_OUT_SUB = Threadcreate(@rs232outsub) Dim start As Double start = Timer Sleep 10,1 Do Sleep 1, 1 If done = 1 Then Rem delete RS232_OUT_SUB Return 0 End If Loop Until (Timer - Start) > timeout delete RS232_OUT_SUB Return 5 End Function Function RS232_GET As String Dim temp As String Mutexlock RS232_MUTEX temp = RS232_IN RS232_IN = "" Mutexunlock RS232_MUTEX Return temp End Function Function RS232_GET_STR(fSTR As String,timeout As Double) As UByte If RS232_STATUS = 0 Then Return 1 If RS232_OUT = "" Then Return 4 Dim start As Double start = Timer Do If Instr(RS232_IN,fSTR) <> 0 Then Return 6 Sleep 50, 1 Loop Until (Timer - Start) > timeout Return 5 End Function Function RS232_GET_STR2(fSTR As String,fSTR1 As String,timeout As Double) As UByte If RS232_STATUS = 0 Then Return 1 If RS232_OUT = "" Then Return 4 Dim start As Double start = Timer Do If Instr(RS232_IN,fSTR) <> 0 Then Return 6 If Instr(RS232_IN,fSTR1) <> 0 Then Return 6 Sleep 50, 1 Loop Until (Timer - Start) > timeout Return 5 End Function
E a parte de teste:
RS232_COMM = "COM11: 57600,N,8,1,BIN,DS0" Print "a tentar abrir a com11" Print RS232_ERR_STR ( RS232_GET_STR (" OK ",1)) Print done Print RS232_GET RS232_OUT = " ATZ0 " Chr (13,10) Print RS232_ERR_STR ( RS232_SEND (20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",1)) Print done Print RS232_GET Print " desliga o usb " Sleep RS232_OUT = "AT" Chr (10,13) Print RS232_ERR_STR ( RS232_SEND (20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",10)) Sleep rs232_close Sleep Skriv RS232_ERR_STR (RS232_GET_STR ("OK", 1)) Skriv utförs ut RS232_GET RS232_OUT = "ATZ0" Chr (13,10) Skriv ut RS232_ERR_STR (RS232_SEND (20)) Print RS232_ERR_STR (RS232_GET_STR ("OK", 1)) Skriv ut görs Skriv RS232_GET Skriv ut "desliga o usb" Sleep RS232_OUT = "AT" Chr (10,13) Skriv ut RS232_ERR_STR (RS232_SEND (20)) Print RS232_ERR_STR (RS232_GET_STR ("OK", 10)) Sleep rs232_close Sleep(RS232_OPEN) RS232_OUT = "AT" + Chr(10,13) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",1)) Print done Print RS232_GET RS232_OUT = " ATZ0 " Chr (13,10) Print RS232_ERR_STR ( RS232_SEND (20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",1)) Print done Print RS232_GET Print " desliga o usb " Sleep RS232_OUT = "AT" Chr (10,13) Print RS232_ERR_STR ( RS232_SEND (20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",10)) Sleep rs232_close Sleep Skriv RS232_ERR_STR (RS232_GET_STR ("OK", 1)) Skriv utförs ut RS232_GET RS232_OUT = "ATZ0" Chr (13,10) Skriv ut RS232_ERR_STR (RS232_SEND (20)) Print RS232_ERR_STR (RS232_GET_STR ("OK", 1)) Skriv ut görs Skriv RS232_GET Skriv ut "desliga o usb" Sleep RS232_OUT = "AT" Chr (10,13) Skriv ut RS232_ERR_STR (RS232_SEND (20)) Print RS232_ERR_STR (RS232_GET_STR ("OK", 10)) Sleep rs232_close Sleep(RS232_SEND(20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",1)) Print done Print RS232_GET RS232_OUT = " ATZ0 " Chr (13,10) Print RS232_ERR_STR ( RS232_SEND (20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",1)) Print done Print RS232_GET Print " desliga o usb " Sleep RS232_OUT = "AT" Chr (10,13) Print RS232_ERR_STR ( RS232_SEND (20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",10)) Sleep rs232_close Sleep Skriv RS232_ERR_STR (RS232_GET_STR ("OK", 1)) Skriv utförs ut RS232_GET RS232_OUT = "ATZ0" Chr (13,10) Skriv ut RS232_ERR_STR (RS232_SEND (20)) Print RS232_ERR_STR (RS232_GET_STR ("OK", 1)) Skriv ut görs Skriv RS232_GET Skriv ut "desliga o usb" Sleep RS232_OUT = "AT" Chr (10,13) Skriv ut RS232_ERR_STR (RS232_SEND (20)) Print RS232_ERR_STR (RS232_GET_STR ("OK", 10)) Sleep rs232_close Sleep(RS232_GET_STR("OK",1)) Print done Print RS232_OUT = RS232_GET "ATZ0" + Chr(13,10) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",1)) Print done Print RS232_GET RS232_OUT = " ATZ0 " Chr (13,10) Print RS232_ERR_STR ( RS232_SEND (20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",1)) Print done Print RS232_GET Print " desliga o usb " Sleep RS232_OUT = "AT" Chr (10,13) Print RS232_ERR_STR ( RS232_SEND (20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",10)) Sleep rs232_close Sleep Skriv RS232_ERR_STR (RS232_GET_STR ("OK", 1)) Skriv utförs ut RS232_GET RS232_OUT = "ATZ0" Chr (13,10) Skriv ut RS232_ERR_STR (RS232_SEND (20)) Print RS232_ERR_STR (RS232_GET_STR ("OK", 1)) Skriv ut görs Skriv RS232_GET Skriv ut "desliga o usb" Sleep RS232_OUT = "AT" Chr (10,13) Skriv ut RS232_ERR_STR (RS232_SEND (20)) Print RS232_ERR_STR (RS232_GET_STR ("OK", 10)) Sleep rs232_close Sleep(RS232_SEND(20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",1)) Print done Print RS232_GET RS232_OUT = " ATZ0 " Chr (13,10) Print RS232_ERR_STR ( RS232_SEND (20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",1)) Print done Print RS232_GET Print " desliga o usb " Sleep RS232_OUT = "AT" Chr (10,13) Print RS232_ERR_STR ( RS232_SEND (20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",10)) Sleep rs232_close Sleep Skriv RS232_ERR_STR (RS232_GET_STR ("OK", 1)) Skriv utförs ut RS232_GET RS232_OUT = "ATZ0" Chr (13,10) Skriv ut RS232_ERR_STR (RS232_SEND (20)) Print RS232_ERR_STR (RS232_GET_STR ("OK", 1)) Skriv ut görs Skriv RS232_GET Skriv ut "desliga o usb" Sleep RS232_OUT = "AT" Chr (10,13) Skriv ut RS232_ERR_STR (RS232_SEND (20)) Print RS232_ERR_STR (RS232_GET_STR ("OK", 10)) Sleep rs232_close Sleep(RS232_GET_STR("OK",1)) Print done Print RS232_GET Print "turn off usb" Sleep RS232_OUT = "AT" + Chr(10,13) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",1)) Print done Print RS232_GET RS232_OUT = " ATZ0 " Chr (13,10) Print RS232_ERR_STR ( RS232_SEND (20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",1)) Print done Print RS232_GET Print " desliga o usb " Sleep RS232_OUT = "AT" Chr (10,13) Print RS232_ERR_STR ( RS232_SEND (20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",10)) Sleep rs232_close Sleep Skriv RS232_ERR_STR (RS232_GET_STR ("OK", 1)) Skriv utförs ut RS232_GET RS232_OUT = "ATZ0" Chr (13,10) Skriv ut RS232_ERR_STR (RS232_SEND (20)) Print RS232_ERR_STR (RS232_GET_STR ("OK", 1)) Skriv ut görs Skriv RS232_GET Skriv ut "desliga o usb" Sleep RS232_OUT = "AT" Chr (10,13) Skriv ut RS232_ERR_STR (RS232_SEND (20)) Print RS232_ERR_STR (RS232_GET_STR ("OK", 10)) Sleep rs232_close Sleep(RS232_SEND(20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",1)) Print done Print RS232_GET RS232_OUT = " ATZ0 " Chr (13,10) Print RS232_ERR_STR ( RS232_SEND (20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",1)) Print done Print RS232_GET Print " desliga o usb " Sleep RS232_OUT = "AT" Chr (10,13) Print RS232_ERR_STR ( RS232_SEND (20)) Print RS232_ERR_STR ( RS232_GET_STR (" OK ",10)) Sleep rs232_close Sleep Skriv RS232_ERR_STR (RS232_GET_STR ("OK", 1)) Skriv utförs ut RS232_GET RS232_OUT = "ATZ0" Chr (13,10) Skriv ut RS232_ERR_STR (RS232_SEND (20)) Print RS232_ERR_STR (RS232_GET_STR ("OK", 1)) Skriv ut görs Skriv RS232_GET Skriv ut "desliga o usb" Sleep RS232_OUT = "AT" Chr (10,13) Skriv ut RS232_ERR_STR (RS232_SEND (20)) Print RS232_ERR_STR (RS232_GET_STR ("OK", 10)) Sleep rs232_close Sleep(RS232_GET_STR("OK",10)) Sleep rs232_close Sleep
Also are here: http://www.freebasic.net/forum/viewtopic.php?t=15449
And both works in windows as in linux.
Any questions do not hesitate to ask 🙂
FreeBasic and RS232
This is that language? It is similar to C.
Do you like working with the FreeBasic?
Are you thinking of updating your site?
You already have content!
Port-te.
http://www.freebasic.net/ <-- Ă© o site oficial... Ă© basic... Ă© rapido pratico e tanto compila em windows como linux (para as coisas que quero Ă© optimo) Sabes como Ă© o site vai sendo actualizado com calma, ele nĂŁo Ă© a minha fonte de rendimento