> 技术文档 > SIMCOM蜂窝模组 TCP测试

SIMCOM蜂窝模组 TCP测试

#使用ttyUSB2 测试 TCP 服务器 45.79.112.203 4242

#!/usr/bin/pythonimport serialimport timeart_text = \"\"\" __ __  ________ _____ _____ \\ \\ / /  | |  |__ __/ ____| __ \\ \\ \\ /\\ / /_ ___ _____ ___| |__ __ _ _ __ ___ | | | | | |__) | \\ \\/ \\/ / _` \\ \\ / / _ \\/ __| \'_ \\ / _` | \'__/ _ \\ | | | | | ___/ \\ /\\ / (_| |\\ V / __/\\__ \\ | | | (_| | | | __/ | | | |____| | \\/ \\/ \\__,_| \\_/ \\___||___/_| |_|\\__,_|_| \\___| |_| \\_____|_| \"\"\"print(art_text)ser = serial.Serial(\'/dev/ttyUSB2\',115200)ser.flushInput()rec_buff = \'\'APN = \'CMNET\'ServerIP = \'45.79.112.203\'Port = \'4242\'Message = \'Waveshare\'def send_at(command,back,timeout): rec_buff = \'\' ser.write((command+\'\\r\\n\').encode()) time.sleep(timeout) if ser.inWaiting(): time.sleep(0.1 ) rec_buff = ser.read(ser.inWaiting()) if rec_buff != \'\': if back not in rec_buff.decode(): print(command + \' ERROR\') print(command + \' back:\\t\' + rec_buff.decode()) return 0 else: print(rec_buff.decode()) return 1 else: print(command + \' no responce\')try: send_at(\'ATE1\',\'OK\',1) send_at(\'AT+CSQ\',\'OK\',1) send_at(\'AT+CPSI?\',\'OK\',1) send_at(\'AT+CGREG?\',\'+CGREG: 0,1\',0.5) send_at(\'AT+CIPMODE=0\', \'OK\', 1) send_at(\'AT+NETOPEN\', \'+NETOPEN: 0\',5) send_at(\'AT+IPADDR\', \'+IPADDR:\', 1) send_at(\'AT+CIPOPEN=0,\\\"TCP\\\",\\\"\'+ServerIP+\'\\\",\'+Port,\'+CIPOPEN: 0,0\', 5) send_at(\'AT+CIPSEND=0,\', \'>\', 2)#If not sure the message number,write the command like this: AT+CIPSEND=0, (end with 1A(hex)) ser.write(Message.encode()) if 1 == send_at(b\'\\x1a\'.decode(),\'OK\',5): print(\'send message successfully!\') send_at(\'AT+CIPCLOSE=0\',\'+CIPCLOSE: 0,0\',15) send_at(\'AT+NETCLOSE\', \'+NETCLOSE: 0\', 1)except: print(\"except\")

SIMCOM蜂窝模组 TCP测试