Waveshare SIM7600X can't work concurrently with other python scripts

Hello, wanted to ask for the python script of LED-SMS. Based on Guide by TIM It worked well when i ran it alone but when i try to import it to another python script by just simply using import LED-SMS in the same directory, it wont work. Below is the output. Don’t know why it can’t run together with other scripts, even if i tried opening two terminals and running them the LED-SMS script will just output the below after the second script starts to run. Looking for any help kinda urgent on this matter

AT+CMGF=1 back:

AT+CMGL=“REC UNREAD” back:

AT+CMGL=“REC UNREAD” back:
No New text

Hi @ZiSong270037, welcome to the forums!

I’m reposting this from the other forum post where you mentioned this problem.

If you could post your code for context I’m sure that would be helpful.

I’m not sure I could suggest any solutions just based of the error message you provided.

Thanks,
Sam

sms.pdf (35.1 KB)
doorlock.pdf (42.8 KB)

So if I were to run the sms.pdf which is sms.py alone, it will execute perfectly. But when i try to import sms.py into doorlock.py in the doorlock.pdf. It will output the output mentioned below. Or if i try to run sms.py and doorlock.py in different terminals sms.py will not output the OK which indicates SMS is set up and ready to receive and read messages. Instead the output mentioned in the original question will be prompt

Thanks for your time @Samuel

Hi @ZiSong270037,

Thanks for the code. From what I can see the error message is saying that both scripts are trying to access the serial port “/dev/ttyAMA0” at the same time which can lead to port conflicts and undesired behaviour.

In the Python context, an import just runs the imported script like your doing manually in your terminal, so “import sms” in doorlock.py will run the sms.py script. This means that both sms.py and doorlock.py are attempting to open, write to, and read from the same Serial port which is not ideal and is not allowed by the operating system.

Combine the Codes: The simplest way would be to combine the two scripts into a single script that accesses the serial port. You can combine them by creating functions for their differing parts and calling these functions within a main() function. (I would recommend this if possible, multithreading with python is not my favourite method)

Multithreading: Python has a threading module, which allows you to run separate parts of your program concurrently which you seem to have picked up on. You could do this in your case but you would need to go through both code files and ensure there is no conflicts.

The serial port is the current one that is flagging but you will need to make sure you don’t have any other potential problems like this in your code.

I would personally recommend chopping up the sms.py code into a function that you can leave inside your doorlock.py file to avoid these issues.

Hope this helps!
Sam

Thanks for your reply Samuel, below I have tried combining the codes but the error still persists. Could it be any lines of codes that still has conflict with using the serial port?

doorlock.pdf (51.1 KB)

Thanks @Samuel

Hey @ZiSong270037,

Good job with combining that code. I can’t see anything obvious in it that should be causing you errors. Would you be able to send a screenshot of the error message you are getting in its entirety?

Maybe its something else in your setup that’s causing this.